简体   繁体   English

如何从org.w3c.dom.Document中删除特定的xml属性

[英]How to remove an specific xml attribute from org.w3c.dom.Document

I have this XML: 我有这个XML:

<Body xmlns:wsu="http://mynamespace">           
 <Ticket xmlns="http://othernamespace">
  <Customer xlmns="">Robert</Customer>
  <Products xmlns="">
   <Product>a product</>
  </Products>               
 </Ticket>
 <Delivered xmlns="" />
 <Payment xlmns="">cash</Payment>
</Body>

I am using Java to read it as a DOM document. 我正在使用Java将其作为DOM文档读取。 I want remove the empty namespace attributes (ie, xmlns="" ). 我要删除空的名称空间属性(即xmlns="" )。 Is there any way to do that? 有什么办法吗?

You need to understand that xmlns is a very special attribute. 您需要了解xmlns是一个非常特殊的属性。 Basically, the xmlns="" is so that your Customer element is in the "unnamed" namespace, rather than the http://othernamespace namespace (and likewise for other elements which would otherwise inherit a default namespace from their ancestors). 基本上, xmlns=""是为了使您的Customer元素位于“未命名”名称空间中,而不是在http://othernamespace命名空间中(对于其他元素,否则将从其祖先继承默认名称空间)。

If you want to get rid of the xmlns="" , you basically need to put the elements into the appropriate namespace - so it's changing the element name. 如果要摆脱xmlns="" ,则基本上需要将元素放入适当的名称空间中-因此它正在更改元素名称。 I don't think the W3C API lets you change the name of an element - you may well need to create a new element with the appropriate namespaced-name, and copy the content. 认为 W3C API不允许您更改元素的名称-您可能需要使用适当的命名空间名称创建一个新元素,然后复制内容。 Or if you're responsible for creating the document to start with, just use the right namespace. 或者,如果您负责创建文档的开头,请使用正确的名称空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM