star2014
star2014

Reputation: 183

how to solve error:A node is used in a different document than the one that created it. in java

I have an XML file like this :

<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>

I'm trying to filter those Interaction elements which have kind attribute value as "edit" and write them in new XML file like this:

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

this is my code:

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();}

    }

}

but my program has error: A node is used in a different document than the one that created it. the problem is related to this line :rootElement.insertBefore(eElement,null); I tried appendelement but it didn't work also, any help?

Upvotes: 4

Views: 5480

Answers (2)

chuonghd
chuonghd

Reputation: 103

You should import your nNode to your docnew first

Modify your code as below:

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

Cheers!

Upvotes: 3

Guffa
Guffa

Reputation: 700372

You can't get a node from one document and put it into another document. You have to create a new node for the target document using the data from the existing node.

Upvotes: 2

Related Questions