使用 Java 更改 xml-node 的节点名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1299926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing node name of xml-node with Java
提问by doro
I have following scenario: I have a XML-Document, e.g. like this
我有以下场景:我有一个 XML 文档,例如像这样
<someRootElement>
<tag1>
<tag2
someKey=someValue
someKey2=someValue2
/>
<tag3/>
<tag4
newKey=newValue
newKey2=newValue2
/>
</tag1>
</someRootElement>
Now I want the parent tag1to be called reallyCoolTagwithout losing the childnodes. I tried the following, but it unfortunately doesn't work as I wish it would (but I do know why, b/c it is missing something, I guess):
现在我希望在不丢失子节点的情况下将父tag1称为reallyCoolTag。我尝试了以下操作,但不幸的是它没有像我希望的那样工作(但我知道为什么,b/c 它缺少一些东西,我猜):
// the new element:
Element neu = doc.createElement( newValue );
// append it to the root:
root.appendChild( neu );
// get all the child nodes:
NamedNodeMap nnm = nodes.item(i).getAttributes();
for( int dg = 0; dg < nnm.getLength(); dg++ ){
neu.setAttribute( nnm.item( dg ).getNodeName(),
nnm.item( dg ).getNodeValue() );
}
//---------------------------------------------------------
// HERE I GUESS I AM MISSING THE PART WHERE THE CHILD NODES
// ARE BEING APPENDED TO THE NEW NODE?????
//---------------------------------------------------------
// nodes.item(i) := the old value (nodes := is a NodeList
root.replaceChild( neu, nodes.item(i));
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource( doc );
StreamResult result = new StreamResult( xml );
transformer.transform( source, result );
nodes.item( i ).getParentNode().removeChild( nodes.item(i) );
Now this does work to a certain extend, as I mentioned, I guess I am missing the part where the child nodes are being appened, but what I actually wanted to know is, whether there is a really short way to rename the parent node without having to copy everything and replace the whole thing?
现在这在一定程度上确实有效,正如我所提到的,我想我错过了附加子节点的部分,但我真正想知道的是,是否有一种非常短的方法来重命名父节点而无需必须复制所有内容并替换整个内容?
Thnx in advance!!!
提前谢谢!!!
采纳答案by McDowell
Using Document.renameNode:
NodeList nodes = document.getElementsByTagName("tag1");
for (Node eachNode: nodes) {
document.renameNode(eachNode, null, "reallyCoolTag");
}
回答by Aaron Digulla
Just call setName("reallyCoolTag")on the element(s) you want to rename. There is no need to copy the children around; the name attribute of an element is a mutable field.
只需调用setName("reallyCoolTag")要重命名的元素即可。无需复制身边的孩子;元素的 name 属性是一个可变字段。
回答by Moro
As you did get the attributes:
正如您确实获得了属性:
NamedNodeMap nnm = nodes.item(i).getAttributes();
and you added these attributes to the new element,
并将这些属性添加到新元素中,
You should get the children of nodes.item(i)and set them in the new node.
您应该获取 的子nodes.item(i)节点并将它们设置在新节点中。
You can use for ex.:
您可以使用例如:
neu.addContent(nodes.item(i).getChildren());
回答by ZZ Coder
Your tag1 is invalid. It doesn't have closing >. Also the attributes should be quoted. It should look like this,
您的 tag1 无效。它没有关闭 >。还应该引用属性。应该是这样的
<someRootElement>
<tag1>
<tag2
someKey="someValue"
someKey2="someValue2"
/>
<tag3/>
<tag4
newKey="newValue"
newKey2="newValue2"
/>
</tag1>
</someRootElement>
Try with the corrected XML. It should work.
尝试使用更正后的 XML。它应该工作。
回答by McDowell
You could use an XSL Transformation (XSLT) for this:
您可以为此使用 XSL 转换 (XSLT):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="*"> <!-- match anything -->
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="tag1"> <!-- switch the element name -->
<xsl:element name="reallyCoolTag">
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This can be used with the javax.xml.transformpackage (Java 1.4 and above):
这可以与javax.xml.transform包(Java 1.4 及更高版本)一起使用:
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer(new StreamSource(
new File("RenameTag.xslt")));
transformer
.transform(new DOMSource(document), new StreamResult(System.out));

