为什么在Java中元素会自动添加xmlns = “”?

huangapple go评论67阅读模式
英文:

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(&quot;http://www.myorc.com/schemas&quot;, &quot;InvConf&quot;);

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(&quot;InvList&quot;);
 root.appendChild(invList);

&lt;InvConf xmlns=&quot;http://www.myorc.com/schemas&quot;&gt;
   &lt;InvList xmlns=&quot;&quot;&gt;
   ...
   &lt;/InvList&gt;
   &lt;InvList xmlns=&quot;&quot;&gt;
   ...
   &lt;/InvList&gt;
   &lt;InvList xmlns=&quot;&quot;&gt;
   ...
   &lt;/InvList&gt;
&lt;/InvConf&gt;

How to avoid adding the namespace to child nodes ?
I want the final XML to be like the following

&lt;InvConf xmlns=&quot;http://www.myorc.com/schemas&quot;&gt;
   &lt;InvList&gt;
   ...
   &lt;/InvList&gt;
   &lt;InvList&gt;
   ...
   &lt;/InvList&gt;
   &lt;InvList&gt;
   ...
   &lt;/InvList&gt;
&lt;/InvConf&gt;

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(&quot;InvList&quot;);

To the correct namespace.

答案2

得分: 0

正如评论中指出的那样,xmlns=&quot;&quot; 表示元素没有任何命名空间。例如,从 XML 解析器的角度来看,以下两个文档是相同的:

<ns:root xmlns:ns="http://namespace.com">
    <child/>
</ns:root>


<root xmlns="http://namespace.com">
    <child xmlns=""/>
</root>

为了避免在不属于任何命名空间的元素中创建 xmlns=&quot;&quot;,您可以在上一级元素上创建前缀:

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=&quot;&quot; means that element doesn't have any namespace. E.g. from XML parser point of view following two documents are identical:

&lt;ns:root xmlns:ns=&quot;http://namespace.com&quot;&gt;
    &lt;child/&gt;
&lt;/ns:root&gt;

and

&lt;root xmlns=&quot;http://namespace.com&quot;&gt;
    &lt;child xmlns=&quot;&quot;/&gt;
&lt;/root&gt;

To avoid creation of xmlns=&quot;&quot; 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(&quot;http://namespace.com&quot;, &quot;root&quot;);
root.setPrefix(&quot;ns&quot;);
Element child = doc.createElementNS(&quot;&quot;, &quot;child&quot;);
root.appendChild(child);
doc.appendChild(root);

This code will create following XML:

&lt;ns:root xmlns:ns=&quot;http://namespace.com&quot;&gt;
    &lt;child/&gt;
&lt;/ns:root&gt;

Alternatively you can use following syntax to achieve same result:

Document doc = db.newDocument();
Element root = doc.createElementNS(&quot;http://namespace.com&quot;, &quot;ns:root&quot;);
Element child = doc.createElementNS(&quot;&quot;, &quot;child&quot;);
root.appendChild(child);
doc.appendChild(root);

When commenting out line root.setPrefix(&quot;ns&quot;); or creating element without prefix (doc.createElementNS(&quot;http://namespace.com&quot;, &quot;root&quot;);) following XML will be generated:

&lt;root xmlns=&quot;http://namespace.com&quot;&gt;
    &lt;child xmlns=&quot;&quot;/&gt;
&lt;/root&gt;

huangapple
  • 本文由 发表于 2020年10月6日 21:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64227372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定