Java+DOM:如何设置(已创建的)文档的基本命名空间?
我正在处理一个已创建 Document 对象。 我必须能够将其基本命名空间(属性名称“xmlns”)设置为特定值。 我的输入是 DOM,类似于:
<root>...some content...</root>
我需要的是 DOM,类似于:
<root xmlns="myNamespace">...some content...</root>
就是这样。很容易,不是吗? 错了!不适用于 DOM!
我尝试过以下操作:
1)使用 doc.getDocumentElement().setAttribute("xmlns","myNamespace")
我得到一个带有空 xmlns 的文档(它适用于 any其他属性名称!)
<root xmlns="">...</root>
2)使用renameNode(...)
首先克隆文档:
Document input = /*that external Document whose namespace I want to alter*/;
DocumentBuilderFactory BUILDER_FACTORY_NS = DocumentBuilderFactory.newInstance();
BUILDER_FACTORY_NS.setNamespaceAware(true);
Document output = BUILDER_NS.newDocument();
output.appendChild(output.importNode(input.getDocumentElement(), true));
我真的缺少document.clone(),但也许只是我。
现在重命名根节点:
output.renameNode(output.getDocumentElement(),"myNamespace",
output.getDocumentElement().getTagName());
现在不是那很简单吗? ;)
我现在得到的是:
<root xmlns="myNamespace">
<someElement xmlns=""/>
<someOtherElement xmlns=""/>
</root>
所以(正如我们所有人所期望的,对吧?),这仅重命名根节点的命名空间。
诅咒你,DOM!
有什么方法可以递归地执行此操作(无需编写自己的递归方法)?
请帮忙;)
请不要建议我做一些花哨的解决方法,例如将 DOM 转换为 其他的,改变那里的命名空间,然后将其转换回来。 我需要 DOM,因为它是操作 XML 的最快标准方法。
注意:我使用的是最新的 JDK。
编辑
从问题中删除了与命名空间前缀有关的错误假设。
I am dealing with an already created Document object.
I have to be able to set it's base namespace (attribute name "xmlns") to certain value.
My input is DOM and is something like:
<root>...some content...</root>
What I need is DOM which is something like:
<root xmlns="myNamespace">...some content...</root>
That's it. Easy, isn't it? Wrong! Not with DOM!
I have tried the following:
1) Using doc.getDocumentElement().setAttribute("xmlns","myNamespace")
I get a document with empty xmlns (it works on any other attribute name!)
<root xmlns="">...</root>
2) Using renameNode(...)
First clone the document:
Document input = /*that external Document whose namespace I want to alter*/;
DocumentBuilderFactory BUILDER_FACTORY_NS = DocumentBuilderFactory.newInstance();
BUILDER_FACTORY_NS.setNamespaceAware(true);
Document output = BUILDER_NS.newDocument();
output.appendChild(output.importNode(input.getDocumentElement(), true));
I'm really missing document.clone(), but perhaps it's just me.
Now rename the root node:
output.renameNode(output.getDocumentElement(),"myNamespace",
output.getDocumentElement().getTagName());
Now isn't that straightforward? ;)
What I get now is:
<root xmlns="myNamespace">
<someElement xmlns=""/>
<someOtherElement xmlns=""/>
</root>
So (as all of us have expected, right?), this renames the namespace only of the the root node.
Curse you, DOM!
Is there any way to do this recursively (without writing an own recursive method)?
Please help ;)
Please don't advice me to do some fancy workaround, such as transforming DOM to
something else, alter the namespace there, and transform it back.
I need DOM because it's the fastest standard way to manipulate XML.
Note: I'm using the latest JDK.
EDIT
Removed wrong assumptions from the question, which had to do with namespace prefix.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
好吧,这是递归“解决方案”:
(我仍然希望有人能找到更好的方法来做到这一点)
似乎可行,尽管我不知道仅重命名节点类型是否正确
ELEMENT_NODE,或者是否必须重命名其他节点类型。
Well, here goes the recursive "solution":
(I still hope that someone might find a better way to do this)
Seems to work, although I don't know if it's correct to rename only the node type
ELEMENT_NODE, or if other node types must be renamed.
我们可以使用 sax 解析器更改 xml 命名空间,试试这个
we can change the xml namespace using sax parser, try this
Ivan 的原始帖子的一个细微变化对我有用:在文档节点上设置属性。
其中
希望对某人有所帮助!
迈克·沃茨
A slight variation of Ivan's original post worked for me: setting the attribute on the document node.
where
Hope that helps someone!
Mike Watts
如果您可以使用 Xerces 类,则可以创建一个 DOMParser,用固定的 URI 替换属性和元素的 URI:
在其他地方,您可以解析
If you are ok with using the Xerces classes, you can create a DOMParser that replaces the URI of attributes and elements with your fixed up URIs:
The elsewhere, you can parse the
假设您已经有了 Document 实例..
应该可以。
Let's say you've got your Document instance..
That should do.
我使用 org.jdom.Element:
Java:
XML 解决了:
I solved using org.jdom.Element:
Java:
XML:
我今天也遇到了同样的问题。我最终使用了 @ivan_ivanovich_ivanoff 答案,但删除了递归并修复了一些错误。
非常重要:如果旧命名空间为
null
,您必须添加两个翻译,一个从null
到新的namespaceURI
,然后另一个从""
到您的新namespaceURI
。发生这种情况是因为第一次调用renameNode
会将具有null
namespaceURI
的现有节点更改为xmlns=""
。使用示例:
完整源代码如下:
I had the very same problem today. I ended up using parts of @ivan_ivanovich_ivanoff answer but removed the recursion and fixed some bugs.
Very important: if old namespace is
null
you must add two translations, one fromnull
to your newnamespaceURI
and another from""
to your newnamespaceURI
. This happens because the first call torenameNode
will change existing nodes that have anull
namespaceURI
toxmlns=""
.Example of usage:
Full source code follows:
除了设置前缀之外,您还必须在某处声明您的命名空间。
[编辑] 如果您查看包
org.w3c.dom
,您会发现除了可以使用名称空间 URI 创建文档节点之外,不支持任何名称空间:使用标准Java 5(及更高版本)的 W3C DOM API,无法修改节点的名称空间。
但 W3C DOM API 只是几个接口。因此,您应该尝试的是查看实现(即文档实例的实际类),将其转换为真实类型。这种类型应该有额外的方法,如果幸运的话,您可以使用这些方法来修改命名空间。
In addition to setting the prefix, you must also declare your namespace somewhere.
[EDIT] If you look into the package
org.w3c.dom
, you'll notice that there is no support for namespaces whatsoever except that you can create a Document node with a namespace URI:With the standard W3C DOM API of Java 5 (and up), it's not possible to modify the namespace of a node.
But the W3C DOM API is just a couple of interfaces. So what you should try is to look at the implementation (i.e. the actual class of your document instance), cast it to the real type. This type should have additional methods and if you're lucky, you can use those to modify the namespace.