英文:
Why are xmlns="" added to element in Java automatically?
问题
我正在使用Java创建一个XML文件。
我使用createElemenNS()创建了根节点以带有命名空间的方式创建根节点。
Element root = doc.createElementNS("http://www.myorc.com/schemas", "InvConf");
然后我使用createElement()创建了一个节点,并将其添加到根节点。此节点会自动添加命名空间,如下所示。
Element invList = doc.createElement("InvList");
root.appendChild(invList);
<InvConf xmlns="http://www.myorc.com/schemas">
<InvList xmlns="">
...
</InvList>
<InvList xmlns="">
...
</InvList>
<InvList xmlns="">
...
</InvList>
</InvConf>
如何避免向子节点添加命名空间?
我希望最终的XML结果如下所示。
<InvConf xmlns="http://www.myorc.com/schemas">
<InvList>
...
</InvList>
<InvList>
...
</InvList>
<InvList>
...
</InvList>
</InvConf>
发现只有在CLASSPATH中存在xmlparserv2.jar时才会出现此问题。某些部分需要该库以支持应用程序。如何解决这个问题?
英文:
I am creating an XML file using Java.
I created root node using createElemenNS() to createh root node with namespace.
Element root = doc.createElementNS("http://www.myorc.com/schemas", "InvConf");
Then I created a node using createElement() and added it to root node. This node is automatically added with namespace like below.
Element invList = doc.createElement("InvList");
root.appendChild(invList);
<InvConf xmlns="http://www.myorc.com/schemas">
<InvList xmlns="">
...
</InvList>
<InvList xmlns="">
...
</InvList>
<InvList xmlns="">
...
</InvList>
</InvConf>
How to avoid adding the namespace to child nodes ?
I want the final XML to be like the following
<InvConf xmlns="http://www.myorc.com/schemas">
<InvList>
...
</InvList>
<InvList>
...
</InvList>
<InvList>
...
</InvList>
</InvConf>
Found that issues is coming only when xmlparserv2.jar is in CLASSPATH. This is required by some parts for the application. How to resolve this ?
答案1
得分: 1
xmlns = "" 是因为您的子元素不属于命名空间,而父元素属于命名空间。要更改这一点,在创建元素的同时将其放入命名空间中。
将
createElement("InvList");
更改为正确的命名空间。
英文:
The xmlns="" were added because your child is not in a namespace, and your parent is. To change that, put it in the namespace at the time you create the element.
Change the
createElement("InvList");
To the correct namespace.
答案2
得分: 0
正如评论中指出的那样,xmlns=""
表示元素没有任何命名空间。例如,从 XML 解析器的角度来看,以下两个文档是相同的:
<ns:root xmlns:ns="http://namespace.com">
<child/>
</ns:root>
和
<root xmlns="http://namespace.com">
<child xmlns=""/>
</root>
为了避免在不属于任何命名空间的元素中创建 xmlns=""
,您可以在上一级元素上创建前缀:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element root = doc.createElementNS("http://namespace.com", "root");
root.setPrefix("ns");
Element child = doc.createElementNS("", "child");
root.appendChild(child);
doc.appendChild(root);
这段代码将创建以下 XML:
<ns:root xmlns:ns="http://namespace.com">
<child/>
</ns:root>
或者,您可以使用以下语法来实现相同的结果:
Document doc = db.newDocument();
Element root = doc.createElementNS("http://namespace.com", "ns:root");
Element child = doc.createElementNS("", "child");
root.appendChild(child);
doc.appendChild(root);
当注释掉 root.setPrefix("ns");
这行或者创建没有前缀的元素(doc.createElementNS("http://namespace.com", "root");
)时,将生成以下 XML:
<root xmlns="http://namespace.com">
<child xmlns=""/>
</root>
英文:
As pointed out in comments xmlns=""
means that element doesn't have any namespace. E.g. from XML parser point of view following two documents are identical:
<ns:root xmlns:ns="http://namespace.com">
<child/>
</ns:root>
and
<root xmlns="http://namespace.com">
<child xmlns=""/>
</root>
To avoid creation of xmlns=""
in elements not belonging to any namespace you can create prefix on upper level element:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element root = doc.createElementNS("http://namespace.com", "root");
root.setPrefix("ns");
Element child = doc.createElementNS("", "child");
root.appendChild(child);
doc.appendChild(root);
This code will create following XML:
<ns:root xmlns:ns="http://namespace.com">
<child/>
</ns:root>
Alternatively you can use following syntax to achieve same result:
Document doc = db.newDocument();
Element root = doc.createElementNS("http://namespace.com", "ns:root");
Element child = doc.createElementNS("", "child");
root.appendChild(child);
doc.appendChild(root);
When commenting out line root.setPrefix("ns");
or creating element without prefix (doc.createElementNS("http://namespace.com", "root");
) following XML will be generated:
<root xmlns="http://namespace.com">
<child xmlns=""/>
</root>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论