gpt4 book ai didi

java - DOM XML Public Doctype 未出现在结果 xml 文件中

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:04 25 4
gpt4 key购买 nike

我已经编写了生成 XML 文件的代码。我一直坚持为 XML 定义文档类型,因为它应该是公共(public)的。我能够成功获取 SYSTEM 文档类型,但不知何故无法获取用 XML 编写的公共(public)文档类型。下面的 SYSTEM doctype 代码可以正常工作,但 PUBLIC doctype 的相同代码段不起作用:

String xmldestpath = "C:/failed/tester.xml";
doctype2 = CreateDoctypeString();

StreamResult result = new StreamResult(new File(xmldestpath ));
try {
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"TEST");
transformer.transform(source, result);
// logger.debug("COMPLETED Copying xml files /....!!");

System.out.println("COMPLETED Copying xml files to bulk import....!!");

不工作的片段。它没有给出错误,但结果 xml 中没有出现文档类型:

 String xmldestpath = "C:/failed/tester.xml";
doctype2 = CreateDoctypeString();

StreamResult result = new StreamResult(new File(xmldestpath ));
try {
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"TEST");
transformer.transform(source, result);
// //logger.debug("COMPLETED Copying xml files /....!!");

System.out.println("COMPLETED Copying xml files to bulk import....!!");

最佳答案

如果您知道您需要/想要PUBLIC,也许您应该知道如果没有系统文字,公共(public)文字就不可能存在。

XML specification显示:

ExternalID  ::=  'SYSTEM' S SystemLiteral
| 'PUBLIC' S PubidLiteral S SystemLiteral

因此应该很容易得出结论,您需要指定两者才能使其正常工作,如MCVE所示:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "TEST1");
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "TEST2");
transformer.transform(new StreamSource(new StringReader("<Root></Root>")),
new StreamResult(System.out));

输出

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Root PUBLIC "TEST1" "TEST2">
<Root/>

关于java - DOM XML Public Doctype 未出现在结果 xml 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44899798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com