我可以强制JAXB不转换成"进入",例如,当编组到XML时?

Ell*_*iot 25 java jaxb xml-serialization marshalling html-entities

我有一个使用JAXB编组为XML的Object.一个元素包含一个包含引号(")的String.生成的XML具有""存在的位置.

即使这通常是首选,我需要我的输出匹配遗留系统.如何强制JAXB不转换HTML实体?

-

感谢您的答复.但是,我从未看到处理程序escape()被调用.你能看一看,看看我做错了什么吗?谢谢!

package org.dc.model;

import java.io.IOException;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.dc.generated.Shiporder;

import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;

public class PleaseWork {
    public void prettyPlease() throws JAXBException {
        Shiporder shipOrder = new Shiporder();
        shipOrder.setOrderid("Order's ID");
        shipOrder.setOrderperson("The woman said, \"How ya doin & stuff?\"");

        JAXBContext context = JAXBContext.newInstance("org.dc.generated");
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(CharacterEscapeHandler.class.getName(),
                new CharacterEscapeHandler() {
                    @Override
                    public void escape(char[] ch, int start, int length,
                            boolean isAttVal, Writer out) throws IOException {
                        out.write("Called escape for characters = " + ch.toString());
                    }
                });
        marshaller.marshal(shipOrder, System.out);
    }

    public static void main(String[] args) throws Exception {
        new PleaseWork().prettyPlease();
    }
}
Run Code Online (Sandbox Code Playgroud)

-

输出是这样的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<shiporder orderid="Order's ID">
    <orderperson>The woman said, &quot;How ya doin &amp; stuff?&quot;</orderperson>
</shiporder>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,回调永远不会显示.(一旦我调用了回调函数,我会担心它实际上会按照我想要的方式执行.)

-

Ell*_*iot 13

解决我的队友发现:

PrintWriter printWriter = new PrintWriter(new FileWriter(xmlFile));
DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", DumbEscapeHandler.theInstance);
marshaller.marshal(request, dataWriter);
Run Code Online (Sandbox Code Playgroud)

不是将xmlFile传递给marshal(),而是传递DataWriter,它知道编码和适当的转义处理程序(如果有的话).

注意:由于DataWriter和DumbEscapeHandler都在com.sun.xml.internal.bind.marshaller包中,因此必须引导javac.


Lau*_*ulo 10

我把我的自定义处理程序作为这样的类:

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import com.sun.xml.bind.marshaller.CharacterEscapeHandler;

public class XmlCharacterHandler implements CharacterEscapeHandler {

    public void escape(char[] buf, int start, int len, boolean isAttValue,
            Writer out) throws IOException {
        StringWriter buffer = new StringWriter();

        for (int i = start; i < start + len; i++) {
            buffer.write(buf[i]);
        }

        String st = buffer.toString();

        if (!st.contains("CDATA")) {
            st = buffer.toString().replace("&", "&amp;").replace("<", "&lt;")
                .replace(">", "&gt;").replace("'", "&apos;")
                .replace("\"", "&quot;");

        }
        out.write(st);
        System.out.println(st);
    }

}
Run Code Online (Sandbox Code Playgroud)

在marshaller方法中只需调用:

marshaller.setProperty(CharacterEscapeHandler.class.getName(),
                new XmlCharacterHandler());
Run Code Online (Sandbox Code Playgroud)

它工作正常.

  • 这在Java 1.7中不起作用,它是对内部结构的一种利用。 (2认同)