javaxml

Java - Put XML Source into another one



Hi,

I need to put this xml into another one. I add tag as root. Can anyone help me please?

Please, see examples:

Source:

<?xml version="1.0" encoding="UTF-8"?>
    <TBCOMPROBANTE>
       <cuitEmpresa>N11</cuitEmpresa>
       <numeroComprobante>N9</numeroComprobante>
       <nombreArchivo>A41</nombreArchivo>
       <codigoIntegridad>A50</codigoIntegridad>
       <validacionesRemitos class="list">
          <remito>
             <numeroUnico>A16</numeroUnico>
             <procesado>A2</procesado>
          </remito>
          <remito>
             <numeroUnico>A16</numeroUnico>
             <procesado>A2</procesado>
             <errores class="list">
                <error>
                   <codigo>N2</codigo>
                   <descripcion>A150</descripcion>
                </error>
             </errores>
          </remito>
       </validacionesRemitos>
    </TBCOMPROBANTE>

Target (see new Arba tag a root)

<?xml version="1.0" encoding="UTF-8"?>
<Arba>
   <TBCOMPROBANTE>
      <cuitEmpresa>N11</cuitEmpresa>
      <numeroComprobante>N9</numeroComprobante>
      <nombreArchivo>A41</nombreArchivo>
      <codigoIntegridad>A50</codigoIntegridad>
      <validacionesRemitos class="list">
         <remito>
            <numeroUnico>A16</numeroUnico>
            <procesado>A2</procesado>
         </remito>
         <remito>
            <numeroUnico>A16</numeroUnico>
            <procesado>A2</procesado>
            <errores class="list">
               <error>
                  <codigo>N2</codigo>
                  <descripcion>A150</descripcion>
               </error>
            </errores>
         </remito>
      </validacionesRemitos>
   </TBCOMPROBANTE>
</Arba>

Cerbero ---------------------------------------------------------------------- Thanks!!!!


Solution

  • You can do it with the following steps:

    1. Parse the input file using the Java DOM Parser
    2. Find the node of interest
    3. Create a new document with the new node
    4. Copy the node from the first document and add it as a child of the new node
    5. Finally, write out the new document to a file

    Here is my example code

    import java.io.File;
    
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class XMLWrapper {
    
        public static void main(final String[] args) throws Exception {
            final File inputFile = new File(
                    "/Users/richard/Documents/workspace/so/input.xml");
            final File outputFile = new File(
                    "/Users/richard/Documents/workspace/so/output.xml");
    
            // Read in the file using the DOM parser
            final DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            final Document document = factory.newDocumentBuilder().parse(inputFile);
    
            // Find the TBCOMPROBANTE node
            final NodeList childNodes = document
                    .getElementsByTagName("TBCOMPROBANTE");
            final Node chosenNode = childNodes.item(0);
    
            // Create a new document and add the extra node
            final Document newDocument = factory.newDocumentBuilder().newDocument();
            final Element createElement = newDocument.createElement("Arba");
            newDocument.appendChild(createElement);
    
            // Import the node we found and append it to the new element
            final Node importNode = newDocument.importNode(chosenNode, true);
            createElement.appendChild(importNode);
            newDocument.normalize();
    
            // Use a Transformer to write the xml to outputFile
            final TransformerFactory tFactory = TransformerFactory.newInstance();
            final Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            final DOMSource source = new DOMSource(newDocument);
            final StreamResult result = new StreamResult(outputFile);
            transformer.transform(source, result);
    
        }
    }