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:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < HUOXml VERSION = "2" ID = "HUO-DOCUMENT-ID" > |
03 | < DOCUMENT A.ID = "JAVA-012345-2012" > |
05 | < A.TITLE >My Document Title - JavaBlog.fr / Java.lu</ A.TITLE > |
06 | < A.DATE DATE.VALUE = "21.03.2012" >(21 March 2012)</ A.DATE > |
08 | < CONTENTS CONTENTSID = "HUO-CONTENTS-ID-123465" > |
09 | < A.P >Huseyin OZVEREN</ A.P > |
10 | < A.P >Java Development and Tools</ A.P > |
12 | < A.DATE DATE.VALUE = "21.03.2012" >(21 March 2012)</ A.DATE > |
13 | < A.FONT CARACT = "BUI" >This is my first shot at blogging. </ A.FONT >< A.BRK />I take this opportunity to introduce myself. |
14 | My name is Huseyin OZVEREN, I work currently like Senior Consultant J2EE in an IT services company in Europe. |
15 | Web development and Java passionate, I program with the Java Enterprise platform since 2003 by the definition |
16 | of multi-tier architecture systems and the production of high quality, readable, robust, documented and tested code.</ A.P > |
20 | < A.LIST TYPE = "ListOfType1" LEVEL = "1" > |
21 | < A.ITEM LEVEL = "1" SYMBOL = "1." > |
22 | < A.P >LAW 1. text blabla bla bla </ A.P > |
24 | < A.ITEM LEVEL = "1" SYMBOL = "2." > |
25 | < A.P >LAW 2. text blabla bla bla </ A.P > |
27 | < A.ITEM LEVEL = "1" SYMBOL = "3." > |
28 | < A.P >LAW 3. text blabla bla bla </ A.P > |
30 | < A.ITEM LEVEL = "1" SYMBOL = "4." > |
31 | < A.P >LAW 4. text blabla bla bla </ A.P > |
Creation XML
We will create in Java, a dom XML like:
1 | < COVERPAGE A.ID = "JAVA-012345-2012" > |
3 | < A.TITLE >My Document Title - JavaBlog.fr / Java.lu</ A.TITLE > |
4 | < A.DATE DATE.VALUE = "21.03.2012" >(21 March 2012)</ A.DATE > |
7 | < DOCUMENT A.ID = "JAVA-012345-2012" > |
First, we will create the text values from some values from the nodes of main XML stream:
3 | Text text_title = doc.createTextNode(documentHeaderTitle.getNextSibling().getTextContent()); |
5 | Text text_date = doc.createTextNode(documentHeaderDate.getNextSibling().getNextSibling().getAttributes().getNamedItem( "DATE.VALUE" ).getTextContent()); |
7 | Text text_date_label = doc.createTextNode(documentHeaderDate.getNextSibling().getNextSibling().getTextContent()); |
…then, we will create the element of XML to insert into main XML stream:
01 | DocumentBuilderFactory factory = null ; |
02 | DocumentBuilder builder = null ; |
05 | org.w3c.dom.Document doc = null ; |
08 | factory = DocumentBuilderFactory.newInstance(); |
09 | builder = factory.newDocumentBuilder(); |
10 | doc = builder.parse(ins); |
13 | Node root = doc.getFirstChild(); |
14 | Node whereInsert = doc.getElementsByTagName( "DOCUMENT" ).item( 0 ); |
16 | Element rootXmlToAdd = doc.createElement( "COVERPAGE" ); |
20 | Element toInsert = doc.createElement( "COVERPAGE.HEADER" ); |
21 | Element elt_title = doc.createElement( "A.TITLE" ); |
22 | Element elt_date = doc.createElement( "A.DATE" ); |
23 | elt_title.appendChild(text_title); |
24 | elt_date.setAttribute( "DATE.VALUE" , text_date.getTextContent()); |
25 | elt_date.appendChild(text_date_label); |
29 | rootXmlToAdd.setAttribute( "A.ID" , "JAVA-012345-2012" ); |
30 | rootXmlToAdd.appendChild(toInsert); |
33 | toInsert.appendChild(elt_title); |
34 | 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:
03 | root.insertBefore(rootXmlToAdd, whereInsert); |
04 | DOMSource domSource = new DOMSource(doc); |
07 | StringWriter writer = new StringWriter(); |
08 | StreamResult result = new StreamResult(writer); |
09 | TransformerFactory tf = TransformerFactory.newInstance(); |
10 | Transformer transformer = tf.newTransformer(); |
11 | transformer.transform(domSource, result); |
14 | String stringResult = writer.toString(); |
Final code and results
The final Java code is:
02 | * Add xml stream to main XML stream |
03 | * @author Huseyin OZVEREN |
05 | public class TestCreateXmlStreamAndAddXmlToOtherXml { |
08 | public static void main(String[] args) { |
11 | String xmlPathFile = "xmlFile.xml" ; |
12 | java.io.InputStream inputStreamOnFileBean = TestCreateXmlStreamAndAddXmlToOtherXml. class .getResourceAsStream(xmlPathFile); |
15 | String newXml = addXmlNoticeInfo(inputStreamOnFileBean); |
16 | System.out.println(newXml); |
19 | } catch (Exception e) { |
24 | private static String addXmlNoticeInfo(InputStream ins) throws Exception { |
25 | DocumentBuilderFactory factory = null ; |
26 | DocumentBuilder builder = null ; |
29 | org.w3c.dom.Document doc = null ; |
32 | factory = DocumentBuilderFactory.newInstance(); |
33 | builder = factory.newDocumentBuilder(); |
34 | doc = builder.parse(ins); |
37 | Node root = doc.getFirstChild(); |
38 | Node whereInsert = doc.getElementsByTagName( "DOCUMENT" ).item( 0 ); |
40 | Element rootXmlToAdd = doc.createElement( "COVERPAGE" ); |
45 | Node HUOXml = doc.getElementsByTagName( "HUOXml" ).item( 0 ); |
46 | if (HUOXml.hasAttributes()){ |
47 | version = HUOXml.getAttributes().getNamedItem( "VERSION" ).getNodeValue(); |
49 | NodeList documentHeaderChildren = doc.getElementsByTagName( "DOCUMENT.HEADER" ).item( 0 ).getChildNodes(); |
50 | Node documentHeaderTitle = documentHeaderChildren.item( 0 ); |
51 | Node documentHeaderDate = documentHeaderChildren.item( 1 ); |
54 | Text text_title = doc.createTextNode(documentHeaderTitle.getNextSibling().getTextContent()); |
55 | Text text_date = doc.createTextNode(documentHeaderDate.getNextSibling().getNextSibling().getAttributes().getNamedItem( "DATE.VALUE" ).getTextContent()); |
56 | Text text_date_label = doc.createTextNode(documentHeaderDate.getNextSibling().getNextSibling().getTextContent()); |
59 | Element toInsert = doc.createElement( "COVERPAGE.HEADER" ); |
60 | Element elt_title = doc.createElement( "A.TITLE" ); |
61 | Element elt_date = doc.createElement( "A.DATE" ); |
62 | elt_title.appendChild(text_title); |
63 | elt_date.setAttribute( "DATE.VALUE" , text_date.getTextContent()); |
64 | elt_date.appendChild(text_date_label); |
67 | rootXmlToAdd.setAttribute( "A.ID" , "JAVA-012345-2012" ); |
68 | rootXmlToAdd.appendChild(toInsert); |
70 | toInsert.appendChild(elt_title); |
71 | toInsert.appendChild(elt_date); |
75 | root.insertBefore(rootXmlToAdd, whereInsert); |
76 | DOMSource domSource = new DOMSource(doc); |
79 | StringWriter writer = new StringWriter(); |
80 | StreamResult result = new StreamResult(writer); |
81 | TransformerFactory tf = TransformerFactory.newInstance(); |
82 | Transformer transformer = tf.newTransformer(); |
83 | transformer.transform(domSource, result); |
86 | String stringResult = writer.toString(); |
…the final XML result would be:
01 | <? xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>< HUOXml ID = "HUO-DOCUMENT-ID" VERSION = "2" > |
02 | < 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" > |
04 | < A.TITLE >My Document Title - JavaBlog.fr / Java.lu</ A.TITLE > |
05 | < A.DATE DATE.VALUE = "21.03.2012" >(21 March 2012)</ A.DATE > |
07 | < CONTENTS CONTENTSID = "HUO-CONTENTS-ID-123465" > |
08 | < A.P >Huseyin OZVEREN</ A.P > |
09 | < A.P >Java Development and Tools</ A.P > |
11 | < A.DATE DATE.VALUE = "21.03.2012" >(21 March 2012)</ A.DATE > |
12 | < A.FONT CARACT = "BUI" >This is my first shot at blogging. </ A.FONT >< A.BRK />I take this opportunity to introduce myself. |
13 | My name is Huseyin OZVEREN, I work currently like Senior Consultant J2EE in an IT services company in Europe. |
14 | Web development and Java passionate, I program with the Java Enterprise platform since 2003 by the definition |
15 | of multi-tier architecture systems and the production of high quality, readable, robust, documented and tested code.</ A.P > |
19 | < A.LIST LEVEL = "1" TYPE = "ListOfType1" > |
20 | < A.ITEM LEVEL = "1" SYMBOL = "1." > |
21 | < A.P >LAW 1. text blabla bla bla </ A.P > |
23 | < A.ITEM LEVEL = "1" SYMBOL = "2." > |
24 | < A.P >LAW 2. text blabla bla bla </ A.P > |
26 | < A.ITEM LEVEL = "1" SYMBOL = "3." > |
27 | < A.P >LAW 3. text blabla bla bla </ A.P > |
29 | < A.ITEM LEVEL = "1" SYMBOL = "4." > |
30 | < A.P >LAW 4. text blabla bla bla </ A.P > |
Source: test_xml_create_add.zip
That’s all!!!
Huseyin OZVEREN
Related