Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML: to append xml document into the node of another document

Tags:

java

xml

I have to insert file1.xml elements into another file2.xml. file2.xml has several node and each node has it's node_id. is there any way to do that.

let suppose :

file1.xml :

         < root> 
            <node_1>......</node_1> 
         </root> 

file2.xml :

         < root>
            < node>
               < node_id>1'<'/node_id>
            < /node>
         < /root> 

I want ? file2.xml :

         < root>
            < node>
               <node_1>......</node_1> [here i want to append the file1.xml]
            </node>
         </root>
like image 507
Bibhaw Avatar asked Jan 06 '11 08:01

Bibhaw


People also ask

What is append in XML?

Add a new element to the document as a sibling immediately after the current element. If text is specified, the element will contain a text node that is set to the value of the text string.

Which line of code adds a new node into an XML file?

Add a Node - appendChild() The appendChild() method adds a child node to an existing node. The new node is added (appended) after any existing child nodes. Note: Use insertBefore() if the position of the node is important.

What is XML and node in XML?

Everything in an XML document is a node. For example, the entire document is the document node, and every element is an element node. Root node. The topmost node of a tree. In the case of XML documents, it is always the document node, and not the top-most element.

What is XML node in C#?

XmlNode is the base class in the . NET implementation of the DOM. It supports XPath selections and provides editing capabilities. The XmlDocument class extends XmlNode and represents an XML document. You can use XmlDocument to load and save XML data.


2 Answers

  1. Iterate over all the node_id elements in file2.
  2. For each one, look up corresponding node_x element in file1.
  3. Add node_x from file1 into file2

The following code illustrates this:

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

//build DOMs
Document doc1 = builder.parse(new File("file1.xml"));
Document doc2  = builder.parse(new File("file2.xml"));

//get all node_ids from doc2 and iterate
NodeList list = doc2.getElementsByTagName("node_id");
for(int i = 0 ; i< list.getLength() ; i++){

    Node n = list.item(i);

    //extract the id
    String id = n.getTextContent();

    //now get all node_id elements from doc1
    NodeList list2 = doc1.getElementsByTagName("node_"+id);
    for(int j = 0 ; j< list2.getLength() ; j++){

        Node m = list2.item(j);

        //import them into doc2
        Node imp = doc2.importNode(m,true);
        n.getParent().appendChild(imp);
    }
}

//write out the modified document to a new file
TransformerFactory tFactory = TransformerFactory.newInstance(); 
Transformer transformer = tFactory.newTransformer();
Source source = new DOMSource(doc2);
Result output = new StreamResult(new File("merged.xml"));
transformer.transform(source, output);        

The result would be:

<root>
  <node>
    <node_id>1</node_id>
    <node_1>This is 1</node_1>
  </node>
  <node>
    <node_id>2</node_id>
    <node_2>This is 2</node_2>
  </node>
  <node>
    <node_id>3</node_id>
    <node_3>This is 3</node_3>
  </node>
</root>
like image 177
dogbane Avatar answered Sep 23 '22 03:09

dogbane


Usual approach:

parse both documents from file1 and file2 into Document objects (SAXParser, jDom, dom4j), then import element <node_1> from the first document to the second and add it to <node>. Then delete the corresponding <node_id> element.

Importing is necessary, the Document implementations offer the correct methods for this process! Just adding an element from one document to another documents will result in DOMExceptions.

like image 44
Andreas Dolk Avatar answered Sep 23 '22 03:09

Andreas Dolk