java - 如何解决java中的错误:A node is used in a different document than the one that created it.

标签 java xml dom

我有一个像这样的 XML 文件:

<Interactions>
     <Interaction Delta="null" Kind="propagation"  StructureKind="resource"/>
     <Interaction Delta="null" Kind="edit"  StructureKind="resource"/>
     <Interaction Delta="null" Kind="select"  StructureKind="resource"/>
     <Interaction Delta="null" Kind="edit"  StructureKind="resource"/>
</Interactions>

我正在尝试过滤那些 kind 属性值为“edit”的 Interaction 元素,并将它们写入新的 XML 文件中,如下所示:

<bug>
    <Interaction Delta="null" Kind="edit"  StructureKind="resource"/>
    <Interaction Delta="null" Kind="edit"  StructureKind="resource"/>
</bug>

这是我的代码:

public class xslt{
    public static  String dirPath = "/home/";

    public static void main(String[] args) {

    try{
        File fXmlFile= new File(dirPath+"file.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName("Interaction");

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document docnew = docBuilder.newDocument();
        Element rootElement = docnew.createElement("bugid");
        docnew.appendChild(rootElement);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        StreamResult result = new StreamResult(new File(dirPath+"result2.xml"));
        for (int temp=0; temp<nList.getLength();temp++) {
            Node nNode = nList.item(temp);
            String value;
            value=nNode.getAttributes().getNamedItem("Kind").getNodeValue();

            if(value.equalsIgnoreCase("edit"))
                {
                Element eElement = (Element) nNode;

                rootElement.insertBefore(eElement,null);
                }

        }
        DOMSource source = new DOMSource(docnew);
        transformer.transform(source, result); 

        }

    catch(Exception e)
    {e.printStackTrace();}

    }

}

但我的程序有错误:节点在与创建它的文档不同的文档中使用。问题与此行有关:rootElement.insertBefore(eElement,null);我尝试了appendelement,但它也不起作用,有什么帮助吗?

最佳答案

您应该首先将 nNode 导入到 docnew

修改您的代码如下:

if(value.equalsIgnoreCase("edit"))
{   
    Node imported_node = docnew.importNode(nNode, true);
    Element eElement = (Element) imported_node;
    rootElement.insertBefore(eElement,null);
}

干杯!

关于java - 如何解决java中的错误:A node is used in a different document than the one that created it.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19992250/

相关文章:

java - 加载后是否可以获取 native 库信息

java - Jenkins 使用了错误的 Java 版本

对于相同的对象,Java 哈希码在一种情况下会发生冲突,而在另一种情况下不会发生冲突,为什么? (下面的代码)

java - persistence.xml 中的两个持久性单元 - 一个有效,另一个无效

android - 在 onClick 事件中获取 View 的 id 作为 String

javascript - IE11 未在端口上显示相同内容

php - 如何中断耗时过长的 PHP 函数?

java - CXF REST 发布 XML 的区分大小写

javascript - 遍历(获取)包裹在 <a> 标签内的元素?

javascript - 如何在 js 中获取被忽略的样式属性?