例如,在编组为 XML 时,是否可以强制 JAXB 不要将 “ 转换为 ”?
2022-09-01 17:17:52
我有一个对象,它使用 JAXB 被编组到 XML。一个元素包含包含引号 (“) 的字符串。生成的 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();
}
}
--
输出如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<shiporder orderid="Order's ID">
<orderperson>The woman said, "How ya doin & stuff?"</orderperson>
</shiporder>
如您所见,回调永远不会显示。(一旦我收到被调用的回调,我就会担心它是否真的做了我想做的事情。
--