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

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

本文介绍了将命名空间添加到已创建的XML文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用String值创建一个W3C Document对象。一旦我创建了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();
源src = new DOMSource(document);
结果dest = new StreamResult(new File(c:\\xmlFileName.xml));
aTransformer.transform(src,dest);

我用作输入:

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

输出结果应如下所示:

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

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

  NAMESPACE_ERR:尝试创建或更改一个对象关于命名空间不正确的方式。 

欣赏您的帮助!

解决方案

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



替换 setAttribute setPrefix 与此(第2,3行)

  String namespace =http:// com; 
String prefix =ns2;
//将DOM级别1升级到具有正确命名空间的级别2
元素originalDocumentElement = document.getDocumentElement();
元素newDocumentElement = document.createElementNS(namespace,originalDocumentElement.getNodeName());
//设置所需的命名空间,前缀为
newDocumentElement.setPrefix(prefix);
//复制所有子项
NodeList list = originalDocumentElement.getChildNodes();
while(list.getLength()!= 0){
newDocumentElement.appendChild(list.item(0));
}
//替换原始元素
document.replaceChild(newDocumentElement,originalDocumentElement);

在原始代码中,作者试图声明一个元素命名空间,如下所示:

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

第一个参数是属性的命名空间,由于它是一个命名空间属性,因此需要具有 URI。声明的命名空间应该进入第三个参数

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


I am creating a W3C Document object using a String value. Once I created the Document object, I want to add a namespace to the root element of this document. Here's my current code:

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);

What I use as input:

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

What the output should look like:

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

I need to add the prefix value and namespace also to the input xml string. If I try the above code I am getting this exception:

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

Appreciate your help!

解决方案

Since there is not an easy way to rename the root element, we'll have to replace it with an element that has the correct namespace and attribute, and then copy all the original children into it. Forcing the namespace declaration is not needed because by giving the element the correct namespace (URI) and setting the prefix, the declaration will be automatic.

Replace the setAttribute and setPrefix with this (line 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);

In the original code the author tried to declare an element namespace like this:

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

The first parameter is the namespace of the attribute, and since it's a namespace attribute it need to have the http://www.w3.org/2000/xmlns/ URI. The declared namespace should come into the 3rd parameter

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

这篇关于将命名空间添加到已创建的XML文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 07:31