java - 如何在使用 jaxb 进行编组时删除额外的转义字符
问题描述
原始 XML 放大器;由需要忽略的 JAXB 添加:-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<emp>
<address>7 stret & new </address>
<name>Naveenqq</name>
</emp>
预期没有 amp;(实际值需要):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<emp>
<address>7 stret & new </address>
<name>Naveenqq</name>
</emp>
我试过下面的代码:
private static void jaxbObjectToXML(Emp employee) throws IOException, SAXException, ParserConfigurationException
{
try
{
JAXBContext jaxbContext = JAXBContext.newInstance(Emp.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
//jaxbMarshaller.setProperty("jaxb.encoding", "US-ASCII");
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//jaxbMarshaller.setProperty(OutputKeys.ENCODING, "ASCII");
//jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(), new CustomCharacterEscapeHandler());
// jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
//
// @Override
// public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
// out.write( ch, start, length );
//
// }
// });
//
// StringWriter writer = new StringWriter();
File file = new File("employee1.xml");
jaxbMarshaller.marshal(employee, file);
//
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// DocumentBuilder builder = factory.newDocumentBuilder();
// InputSource is = new InputSource( new StringReader( writer.toString() ) );
// Document doc = builder.parse( is );
System.out.println("done::");
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
请帮助如何解决相同的问题,我已经尝试了所有编码类型
解决方案
问题是&
XML 无效,如果您尝试使用&
它验证 XML 将失败。JAXB
非常聪明,因此它会尝试用它们的字符实体替换特殊字符。类似的事情也发生在 HTML 中。你可以参考这里。
但是,如果您在JAXB Unmarshalling
它被替换后观察值&
而不是&
。因此您不必担心它在 XML 中的存在。我想如果你走你想要的路线,那么它会导致很多复杂性,并且你的 XML 本身将是无效的。
XML:
<emp>
<address>7 stret & new</address>
<name>Naveenqq</name>
</emp>
根:
@Data
@XmlRootElement(name = "emp")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
private String address;
private String name;
}
主要的:
public class Main {
public static void main(String[] args) throws JAXBException, XMLStreamException {
final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("test.xml");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final Unmarshaller unmarshaller = JAXBContext.newInstance(Root.class).createUnmarshaller();
final Root root = unmarshaller.unmarshal(xmlStreamReader, Root.class).getValue();
System.out.println(root.toString());
Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "US-ASCII");
//marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", new XmlCharacterHandler());
marshaller.marshal(root, System.out);
}
}
输出:
Root(address=7 stret & new, name=Naveenqq)
<?xml version="1.0" encoding="US-ASCII"?>
<emp>
<address>7 stret & new</address>
<name>Naveenqq</name>
</emp>
正如您在输出中看到的那样,Root(address=7 stret & new, name=Naveenqq)
它已被替换,&
因此您可以继续使用它。
希望解释有所帮助。
推荐阅读
- html - 为什么带有 (@media print display: none; ) 的 html 元素在打印时显示为空白以及如何删除空白?
- java - Android 不考虑 URL 中的完整 GET 参数
- jupyter-notebook - 使用 Latex 将 Juypter Notebook 导出到 Markdown 时遇到问题
- ios - iOS Simulator 如何自动化用户操作然后继续调试
- electron - 从主进程与菜单栏窗口的通信不起作用
- ansible - 任务仅在所需的目录文件夹不可用时运行,否则它将跳过该任务
- python - 打印列表中实例的属性,仅当它存在时
- asp.net-core - ASP.NET Core 服务器中如何包含 Swagger XML 注释文件?
- sql - 如何理解下面提到的 SQL 查询中的 ORDER BY 子句?
- angular - 如何在打印网页对话框本身中触发点击功能?