gpt4 book ai didi

java XMLUtils.outputDOM 输出为空

转载 作者:行者123 更新时间:2023-12-02 09:29:18 27 4
gpt4 key购买 nike

无法弄清楚为什么 XMLUtils.outputDOM 没有输出任何内容

import org.apache.xml.security.utils.XMLUtils;

DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
InputSource is = new InputSource(new StringReader("<EDoc></EDoc>"));
Document doc = dbf.newDocumentBuilder().parse(is);
Element root = doc.getDocumentElement();
System.out.println(root);
OutputStream os = new FileOutputStream("out_xs.xml");
XMLUtils.outputDOM(root, os, true);
System.out.println(XMLUtils.getFullTextChildrenFromElement(root));

输出是:

[EDoc: null]
(empty string)

out_xs.xml:

<?xml version="1.0" encoding="UTF-8"?>

我可以正常获取根元素,但只有在提供 true 时才会使用 xml 定义生成文件。到底是怎么回事?我在库使用 Document 后在其他地方使用相同的函数,所以我怀疑应该通知 Document 提交更改?使用搜索无法找到任何信息

最佳答案

你的问题有点令人困惑/不清楚。我运行了提供的代码,生成的 xml 文件“out_xs.xml”包含以下内容...

<?xml version="1.0" encoding="UTF-8"?>
<EDoc></EDoc>

...根据您指定的代码块,这是正确的。

不过,我确实必须对您的代码进行一些细微的更改,必须在 XMLUtils.outputDOM(root, os, true); 上方添加行 Init.init();。否则,我不断收到异常org.apache.xml.security.c14n.InvalidCanonicalizerException:在使用它之前必须正确初始化xml安全库

也许检查你的依赖版本,我在 Gradle 中使用了以下版本:

// https://mvnrepository.com/artifact/xml-security/xmlsec
compile group: 'xml-security', name: 'xmlsec', version: '1.3.0'

// https://mvnrepository.com/artifact/commons-logging/commons-logging
compile group: 'commons-logging', name: 'commons-logging', version: '1.1.1'

关于java XMLUtils.outputDOM 输出为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58110700/

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