Java 如何";更名为;xml元素

Java 如何";更名为;xml元素,java,dom,Java,Dom,我无法将节点内容(其所有子节点)移动到另一个节点(编辑:保留顺序): documentdocument=pathoNode.getOwnerDocument(); 元素重命名节点=document.createElement(“PATHO”); NodeList childrends=pathoNode.getChildNodes(); 对于(int i=0;i向后遍历集合: for ( int i=children.getLength() - 1; i>=0 ; i-- ) {

我无法将节点内容(其所有子节点)移动到另一个节点(编辑:保留顺序):

documentdocument=pathoNode.getOwnerDocument();
元素重命名节点=document.createElement(“PATHO”);
NodeList childrends=pathoNode.getChildNodes();

对于(int i=0;i向后遍历集合:

for ( int i=children.getLength() - 1; i>=0 ; i-- ) {
    Node nodeToAdd = children.item(i);
    renamedNode.appendChild( nodeToAdd );
}

回顾该集合:

for ( int i=children.getLength() - 1; i>=0 ; i-- ) {
    Node nodeToAdd = children.item(i);
    renamedNode.appendChild( nodeToAdd );
}
我找到了一个“复制”而不移动节点的解决方案,您只需传递一个深度复制的节点:

NodeList children = pathoNode.getChildNodes();
for ( int i=0 ; i<children.getLength() ; i++ ) {
    Node nodeToAdd = children.item(i);
    renamedNode.appendChild( nodeToAdd.cloneNode(true) );
}
nodelistchildrends=pathoNode.getChildNodes();
对于(int i=0;i我找到了一个“复制”而不移动节点的解决方案,您只需传递一个深度复制的节点:

NodeList children = pathoNode.getChildNodes();
for ( int i=0 ; i<children.getLength() ; i++ ) {
    Node nodeToAdd = children.item(i);
    renamedNode.appendChild( nodeToAdd.cloneNode(true) );
}
nodelistchildrends=pathoNode.getChildNodes();

对于(int i=0;iI需要保持顺序,我很确定这个解决方案会反转它。(我仍然给你+1,因为我一开始忘了提到这个)我需要保持顺序,我很确定这个解决方案会反转它。(我仍然给你+1,因为我一开始忘了提到这个)