我有一个对象正在使用JAXB被编组成XML。一个元素包含一个包含引号(")的字符串。
即使这通常是首选的,我需要我的输出来匹配一个遗留系统。如何强制JAXB不转换HTML实体?
--
谢谢你的答复。但是,我从来没有看到处理程序转义()被调用。你能看看我做错了什么吗?谢谢!
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();
}
}--
产出如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<shiporder orderid="Order's ID">
<orderperson>The woman said, "How ya doin & stuff?"</orderperson>
</shiporder>正如您所看到的,回调永远不会显示。(一旦我接到回调电话,我就会担心它会做我想做的事情。)
--
发布于 2009-10-05 21:01:57
我的队友发现:
PrintWriter printWriter = new PrintWriter(new FileWriter(xmlFile));
DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", DumbEscapeHandler.theInstance);
marshaller.marshal(request, dataWriter);与其将xmlFile传递给DataWriter (),不如传递知道编码和适当转义处理程序(如果有的话)的DataWriter。
注意:由于DataWriter和DumbEscapeHandler都在com.sun.xml.internal.bind.marshaller包中,所以必须引导javac。
发布于 2013-08-02 12:41:20
我刚刚将我的自定义处理程序设置为如下类:
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("&", "&").replace("<", "<")
.replace(">", ">").replace("'", "'")
.replace("\"", """);
}
out.write(st);
System.out.println(st);
}
}在封送处理方法中,只需调用:
marshaller.setProperty(CharacterEscapeHandler.class.getName(),
new XmlCharacterHandler());效果很好。
发布于 2009-10-06 08:19:39
我一直在使用您的示例并调试JAXB代码。它似乎是一些特定的UTF-8编码使用。escapeHandler属性MarshallerImpl的设置似乎是正确的。然而,它并不是在所有的上下文中都被使用。如果我搜索MarshallerImpl.createEscapeHandler()的电话,我发现:
public XmlOutput createWriter( OutputStream os, String encoding ) throws JAXBException {
// UTF8XmlOutput does buffering on its own, and
// otherwise createWriter(Writer) inserts a buffering,
// so no point in doing a buffering here.
if(encoding.equals("UTF-8")) {
Encoded[] table = context.getUTF8NameTable();
final UTF8XmlOutput out;
if(isFormattedOutput())
out = new IndentingUTF8XmlOutput(os,indent,table);
else {
if(c14nSupport)
out = new C14nXmlOutput(os,table,context.c14nSupport);
else
out = new UTF8XmlOutput(os,table);
}
if(header!=null)
out.setHeader(header);
return out;
}
try {
return createWriter(
new OutputStreamWriter(os,getJavaEncoding(encoding)),
encoding );
} catch( UnsupportedEncodingException e ) {
throw new MarshalException(
Messages.UNSUPPORTED_ENCODING.format(encoding),
e );
}
}请注意,在您的设置中,将考虑到顶部部分(...equals("UTF-8")...)。然而,这一个没有采取escapeHandler。但是,如果将编码设置为任何其他编码,则此方法的底部部分称为(createWriter(OutputStream, String)),此方法使用escapeHandler,因此EH发挥其作用。所以,加上..。
marshaller.setProperty(Marshaller.JAXB_ENCODING, "ASCII");使您的自定义CharacterEscapeHandler被调用。不太确定,但我想这是JAXB中的bug。
https://stackoverflow.com/questions/1506663
复制相似问题