Can I force JAXB not to convert quot; into amp;quot;, for example, when marshalling to XML?(我可以强制 JAXB 不转换吗?例如,当编组为 XML 时?)
问题描述
我有一个使用 JAXB 编组为 XML 的对象.一个元素包含一个包含引号 (") 的字符串.生成的 XML 包含 "
,其中存在 ".
I have an Object that is being marshalled to XML using JAXB. One element contains a String that includes quotes ("). The resulting XML has "
where the " existed.
尽管这通常是首选,但我需要我的输出匹配 legacy 系统.如何强制 JAXB 不转换 HTML 实体?
Even though this is normally preferred, I need my output to match a legacy system. How do I force JAXB to NOT convert the HTML entities?
--
感谢您的回复.但是,我从来没有看到处理程序 escape() 被调用.你能看看我做错了什么吗?谢谢!
Thank you for the replies. However, I never see the handler escape() called. Can you take a look and see what I'm doing wrong? Thanks!
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>
如您所见,回调永远不会显示.(一旦我得到回调被调用,我会担心它真的做我想做的事.)
and as you can see, the callback is never displayed. (Once I get the callback being called, I'll worry about having it actually do what I want.)
--
推荐答案
我的队友找到的解决方案:
Solution my teammate found:
PrintWriter printWriter = new PrintWriter(new FileWriter(xmlFile));
DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", DumbEscapeHandler.theInstance);
marshaller.marshal(request, dataWriter);
不要将 xmlFile 传递给 marshal(),而是传递知道编码和适当的转义处理程序(如果有)的 DataWriter.
Instead of passing the xmlFile to marshal(), pass the DataWriter which knows both the encoding and an appropriate escape handler, if any.
注意:由于 DataWriter 和 DumbEscapeHandler 都在 com.sun.xml.internal.bind.marshaller 包中,因此您必须引导 javac.
Note: Since DataWriter and DumbEscapeHandler are both within the com.sun.xml.internal.bind.marshaller package, you must bootstrap javac.
这篇关于我可以强制 JAXB 不转换吗?例如,当编组为 XML 时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以强制 JAXB 不转换吗?例如,当编组为 XML 时?
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01