After, my post concerning the recommandation XSLT of XSL, here, I would present an example of creating XML stream and adding it into another XML stream with JAXP (Java APIs for XML Processing). JAXP provides a common interface for creating, parsing and manipulating XML documents using the standard SAX, DOM and XSLTs.
Main XML stream
So, we have the following xml stream, we want create XML stream and add it before the tag DOCUMENT.HEADER:
<?xml version="1.0" encoding="UTF-8"?> <HUOXml VERSION="2" ID="HUO-DOCUMENT-ID"> <DOCUMENT A.ID="JAVA-012345-2012"> <DOCUMENT.HEADER> <A.TITLE>My Document Title - JavaBlog.fr / Java.lu</A.TITLE> <A.DATE DATE.VALUE="21.03.2012">(21 March 2012)</A.DATE> </DOCUMENT.HEADER> <CONTENTS CONTENTSID="HUO-CONTENTS-ID-123465"> <A.P>Huseyin OZVEREN</A.P> <A.P>Java Development and Tools</A.P> <A.P> <A.DATE DATE.VALUE="21.03.2012">(21 March 2012)</A.DATE> <A.FONT CARACT="BUI">This is my first shot at blogging. </A.FONT><A.BRK/>I take this opportunity to introduce myself. My name is Huseyin OZVEREN, I work currently like Senior Consultant J2EE in an IT services company in Europe. Web development and Java passionate, I program with the Java Enterprise platform since 2003 by the definition of multi-tier architecture systems and the production of high quality, readable, robust, documented and tested code.</A.P> <A.P><A.IMG SRC="http://www.w3.org/2008/site/images/logo-w3c-mobile-lg.png"/></A.P> <A.P><A.IMG SRC="http://www.w3.org/2012/08/Rome_sunset_banner-sm.jpg" HEIGHT="123" WIDTH="465"/></A.P> <A.P> <A.LIST TYPE="ListOfType1" LEVEL="1"> <A.ITEM LEVEL="1" SYMBOL="1."> <A.P>LAW 1. text blabla bla bla </A.P> </A.ITEM> <A.ITEM LEVEL="1" SYMBOL="2."> <A.P>LAW 2. text blabla bla bla </A.P> </A.ITEM> <A.ITEM LEVEL="1" SYMBOL="3."> <A.P>LAW 3. text blabla bla bla </A.P> </A.ITEM> <A.ITEM LEVEL="1" SYMBOL="4."> <A.P>LAW 4. text blabla bla bla </A.P> </A.ITEM> </A.LIST> </A.P> </CONTENTS> </DOCUMENT> </HUOXml>
Creation XML
We will create in Java, a dom XML like:
<COVERPAGE A.ID="JAVA-012345-2012"> <COVERPAGE.HEADER> <A.TITLE>My Document Title - JavaBlog.fr / Java.lu</A.TITLE> <A.DATE DATE.VALUE="21.03.2012">(21 March 2012)</A.DATE> </COVERPAGE.HEADER> </COVERPAGE> <DOCUMENT A.ID="JAVA-012345-2012">
First, we will create the text values from some values from the nodes of main XML stream:
//... //Text values Text text_title = doc.createTextNode(documentHeaderTitle.getNextSibling().getTextContent()); Text text_date = doc.createTextNode(documentHeaderDate.getNextSibling().getNextSibling().getAttributes().getNamedItem("DATE.VALUE").getTextContent()); Text text_date_label = doc.createTextNode(documentHeaderDate.getNextSibling().getNextSibling().getTextContent()); //...
…then, we will create the element of XML to insert into main XML stream:
DocumentBuilderFactory factory = null; DocumentBuilder builder = null; // The DOM document org.w3c.dom.Document doc = null; // Loading of document factory = DocumentBuilderFactory.newInstance(); builder = factory.newDocumentBuilder(); doc = builder.parse(ins); // Current XML & Node to insert the XML stream Node root = doc.getFirstChild(); Node whereInsert = doc.getElementsByTagName("DOCUMENT").item(0); // XML to add in the MAIN XML Element rootXmlToAdd = doc.createElement("COVERPAGE"); //... //Elements Element toInsert = doc.createElement("COVERPAGE.HEADER"); Element elt_title = doc.createElement("A.TITLE"); Element elt_date = doc.createElement("A.DATE"); elt_title.appendChild(text_title); elt_date.setAttribute("DATE.VALUE", text_date.getTextContent()); elt_date.appendChild(text_date_label); //... // Root node of XML stream to add to MAIN XML rootXmlToAdd.setAttribute("A.ID", "JAVA-012345-2012"); rootXmlToAdd.appendChild(toInsert); //... toInsert.appendChild(elt_title); toInsert.appendChild(elt_date); //...
Insertion XML
Finally, we will insert the XML, transform the new XML dom document and get the final result in string format:
//... // Insertion of new XML into Main XML stream root.insertBefore(rootXmlToAdd, whereInsert); DOMSource domSource = new DOMSource(doc); // Transformation StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); // Final result String stringResult = writer.toString(); return stringResult;
Final code and results
The final Java code is:
/** * Add xml stream to main XML stream * @author Huseyin OZVEREN */ public class TestCreateXmlStreamAndAddXmlToOtherXml { public static void main(String[] args) { try { // InputStream ins String xmlPathFile = "xmlFile.xml"; java.io.InputStream inputStreamOnFileBean = TestCreateXmlStreamAndAddXmlToOtherXml.class.getResourceAsStream(xmlPathFile); // Add xml stream to main XML stream String newXml = addXmlNoticeInfo(inputStreamOnFileBean); System.out.println(newXml); } catch (Exception e) { e.printStackTrace(); } } private static String addXmlNoticeInfo(InputStream ins) throws Exception { DocumentBuilderFactory factory = null; DocumentBuilder builder = null; // The DOM document org.w3c.dom.Document doc = null; // Loading of document factory = DocumentBuilderFactory.newInstance(); builder = factory.newDocumentBuilder(); doc = builder.parse(ins); // Current XML & Node to insert the XML stream Node root = doc.getFirstChild(); Node whereInsert = doc.getElementsByTagName("DOCUMENT").item(0); // XML to add in the MAIN XML Element rootXmlToAdd = doc.createElement("COVERPAGE"); { // MAIN XML String version = "1"; Node HUOXml = doc.getElementsByTagName("HUOXml").item(0); if(HUOXml.hasAttributes()){ version = HUOXml.getAttributes().getNamedItem("VERSION").getNodeValue(); } NodeList documentHeaderChildren = doc.getElementsByTagName("DOCUMENT.HEADER").item(0).getChildNodes(); Node documentHeaderTitle = documentHeaderChildren.item(0); Node documentHeaderDate = documentHeaderChildren.item(1); //Text values Text text_title = doc.createTextNode(documentHeaderTitle.getNextSibling().getTextContent()); Text text_date = doc.createTextNode(documentHeaderDate.getNextSibling().getNextSibling().getAttributes().getNamedItem("DATE.VALUE").getTextContent()); Text text_date_label = doc.createTextNode(documentHeaderDate.getNextSibling().getNextSibling().getTextContent()); //Elements Element toInsert = doc.createElement("COVERPAGE.HEADER"); Element elt_title = doc.createElement("A.TITLE"); Element elt_date = doc.createElement("A.DATE"); elt_title.appendChild(text_title); elt_date.setAttribute("DATE.VALUE", text_date.getTextContent()); elt_date.appendChild(text_date_label); // Root node of XML stream to add to MAIN XML rootXmlToAdd.setAttribute("A.ID", "JAVA-012345-2012"); rootXmlToAdd.appendChild(toInsert); // toInsert.appendChild(elt_title); toInsert.appendChild(elt_date); } // Insertion of new XML into Main XML stream root.insertBefore(rootXmlToAdd, whereInsert); DOMSource domSource = new DOMSource(doc); // Transformation StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); // Final result String stringResult = writer.toString(); return stringResult; } }
…the final XML result would be:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><HUOXml ID="HUO-DOCUMENT-ID" VERSION="2"> <COVERPAGE A.ID="JAVA-012345-2012"><COVERPAGE.HEADER><A.TITLE>My Document Title - JavaBlog.fr / Java.lu</A.TITLE><A.DATE DATE.VALUE="21.03.2012">(21 March 2012)</A.DATE></COVERPAGE.HEADER></COVERPAGE><DOCUMENT A.ID="JAVA-012345-2012"> <DOCUMENT.HEADER> <A.TITLE>My Document Title - JavaBlog.fr / Java.lu</A.TITLE> <A.DATE DATE.VALUE="21.03.2012">(21 March 2012)</A.DATE> </DOCUMENT.HEADER> <CONTENTS CONTENTSID="HUO-CONTENTS-ID-123465"> <A.P>Huseyin OZVEREN</A.P> <A.P>Java Development and Tools</A.P> <A.P> <A.DATE DATE.VALUE="21.03.2012">(21 March 2012)</A.DATE> <A.FONT CARACT="BUI">This is my first shot at blogging. </A.FONT><A.BRK/>I take this opportunity to introduce myself. My name is Huseyin OZVEREN, I work currently like Senior Consultant J2EE in an IT services company in Europe. Web development and Java passionate, I program with the Java Enterprise platform since 2003 by the definition of multi-tier architecture systems and the production of high quality, readable, robust, documented and tested code.</A.P> <A.P><A.IMG SRC="http://www.w3.org/2008/site/images/logo-w3c-mobile-lg.png"/></A.P> <A.P><A.IMG HEIGHT="123" SRC="http://www.w3.org/2012/08/Rome_sunset_banner-sm.jpg" WIDTH="465"/></A.P> <A.P> <A.LIST LEVEL="1" TYPE="ListOfType1"> <A.ITEM LEVEL="1" SYMBOL="1."> <A.P>LAW 1. text blabla bla bla </A.P> </A.ITEM> <A.ITEM LEVEL="1" SYMBOL="2."> <A.P>LAW 2. text blabla bla bla </A.P> </A.ITEM> <A.ITEM LEVEL="1" SYMBOL="3."> <A.P>LAW 3. text blabla bla bla </A.P> </A.ITEM> <A.ITEM LEVEL="1" SYMBOL="4."> <A.P>LAW 4. text blabla bla bla </A.P> </A.ITEM> </A.LIST> </A.P> </CONTENTS> </DOCUMENT> </HUOXml>
Source: test_xml_create_add.zip
That’s all!!!
Huseyin OZVEREN