将命名空间添加到已创建的 XML 文档

新手上路,请多包涵

我正在使用字符串值创建一个 W3C 文档对象。创建 Document 对象后,我想将命名空间添加到该文档的根元素。这是我当前的代码:

 Document document = builder.parse(new InputSource(new StringReader(xmlString)));
document.getDocumentElement().setAttributeNS("http://com", "xmlns:ns2", "Test");
document.setPrefix("ns2");
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(document);
Result dest = new StreamResult(new File("c:\\xmlFileName.xml"));
aTransformer.transform(src, dest);

我用作输入的内容:

 <product>
    <arg0>DDDDDD</arg0>
    <arg1>DDDD</arg1>
</product>

输出应该是这样的:

 <ns2:product xmlns:ns2="http://com">
    <arg0>DDDDDD</arg0>
    <arg1>DDDD</arg1>
</ns2:product>

我还需要将前缀值和命名空间添加到输入的 xml 字符串中。如果我尝试上面的代码,我会得到这个异常:

 NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

感谢你的帮助!

原文由 Dilip 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 382
2 个回答

由于没有一种简单的方法来重命名根元素,我们必须用具有正确命名空间和属性的元素替换它,然后将所有原始子元素复制到其中。不需要强制声明命名空间,因为通过为元素提供正确的命名空间 (URI) 并设置前缀,声明将是自动的。

用这个替换 setAttributesetPrefix (第2,3行)

 String namespace = "http://com";
String prefix = "ns2";
// Upgrade the DOM level 1 to level 2 with the correct namespace
Element originalDocumentElement = document.getDocumentElement();
Element newDocumentElement = document.createElementNS(namespace, originalDocumentElement.getNodeName());
// Set the desired namespace and prefix
newDocumentElement.setPrefix(prefix);
// Copy all children
NodeList list = originalDocumentElement.getChildNodes();
while(list.getLength()!=0) {
    newDocumentElement.appendChild(list.item(0));
}
// Replace the original element
document.replaceChild(newDocumentElement, originalDocumentElement);

在原始代码中,作者试图像这样声明一个元素命名空间:

 .setAttributeNS("http://com", "xmlns:ns2", "Test");

第一个参数是属性的命名空间,因为它是一个命名空间属性,所以它需要有 http://www.w3.org/2000/xmlns/ URI。声明的命名空间应该进入第三个参数

.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns2", "http://com");

原文由 alexvanboxel 发布,翻译遵循 CC BY-SA 3.0 许可协议

部分从 这里 收集,也从上面的评论中,我能够让它工作(转换任意 DOM 节点并为其及其所有子节点添加前缀)因此:

   private String addNamespacePrefix(Document doc, Node node) throws TransformerException {
    Element mainRootElement = doc.createElementNS(
            "http://abc.de/x/y/z", // namespace
            "my-prefix:fake-header-element" // prefix to "register" it with the DOM so we don't get exceptions later...
    );
    List<Element> descendants = nodeListToArrayRecurse(node.getChildNodes()); // for some reason we have to grab all these before doing the first "renameNode" ... no idea why ...

    mainRootElement.appendChild(node);
    doc.renameNode(node, "http://abc.de/x/y/z", "my-prefix:" + node.getNodeName());
    descendants.stream().forEach(c -> doc.renameNode(c, "http://abc.de/x/y/z", "my-prefix:" + c.getNodeName()));
  }

  private List<Element> nodeListToArrayRecurse(NodeList entryNodes) {
    List<Element> allEntries = new ArrayList<>();
    for (int i = 0; i < entryNodes.getLength(); i++) {
      Node child = entryNodes.item(i);
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        allEntries.add((Element) child);
        allEntries.addAll(nodeListToArray(child.getChildNodes())); // recurse
      } // ignore other [i.e. text] nodes https://stackoverflow.com/questions/14566596/loop-through-all-elements-in-xml-using-nodelist
    }
    return allEntries;
  }

如果它可以帮助任何人。然后我将它转换为字符串,然后手动删除额外的标题和结束行。真痛苦,我一定是做错了什么……

原文由 rogerdpack 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题