While writing an automation test script, you may encounter situations where you need to convert a string to an XML document and an XML document to a string in Java.In this article, we will learn the following things that will ease your work.
- Convert a String to XML Document Object in Java
- Convert an XML document object to String in Java
- Read an XML File and Convert it to XML Doc in Java
The conversion of String to XML Document or vice versa is often required if you work on Web Services ( Soap Request ) automation.
Java Code to Convert String to XML Document
To convert the String to XML document, we will use DocumentBuilderFactory and DocumentBuilder classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; public class StringtoXMLDocExample { public static void main(String[] args) { final String strXML = "<BookStore>" + " <book id=\"b101\">" + " <name>Java Tutorial</name>" + " <price>$5.00</price>" + " </book>" + " <book id=\"b102\">" + " <name>PHP Tutorial</name>" + " <price>$4.75</price>" + " </book>" + " <book id=\"b103\">" + " <name>Visual Bsic Tutorial</name>" + " <price>$3.50</price>" + " </book>" + "</BookStore>"; //Call method to convert XML string content to XML Document object. //Now you can perform required operations on this XML doc Document doc = convertStringToXMLDoc( strXML ); //Get the first node of XML Document to validate whether XML document is build correctly System.out.println("XML Doc First Node Value is : "+doc.getFirstChild().getNodeName()); } //Following method will to convert String to XML Document private static Document convertStringToXMLDoc(String strXMLValue) { try { //Create a new object of DocumentBuilderFactory DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); //Create an object DocumentBuilder to parse the specified XML Data DocumentBuilder builder = dbfactory.newDocumentBuilder(); //Parse the content to Document object Document doc = builder.parse(new InputSource(new StringReader(strXMLValue))); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } } |
Java Code to Convert an XML Document to String
In the previous example we saw how to convert a String to XML document.The following example first converts a String to XML document and then converts it back to String.For converting the XML Document to String, we will use TransformerFactory , Transformer and DOMSource classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
package mypack; import java.io.StringReader; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.InputSource; public class XMLDocToString { public static void main(String[] args) { final String strXML = "<BookStore>" + " <book id=\"b101\">" + " <name>Java Tutorial</name>" + " <price>$5.00</price>" + " </book>" + " <book id=\"b102\">" + " <name>PHP Tutorial</name>" + " <price>$4.75</price>" + " </book>" + " <book id=\"b103\">" + " <name>Visual Bsic Tutorial</name>" + " <price>$3.50</price>" + " </book>" + "</BookStore>"; //Call method to convert XML string content to XML Document object. //Now you can perform required operations on this XML doc Document xmlDoc = convertStringToXMLDoc( strXML ); //Get the first node of XML Document to validate whether XML document is build correctly System.out.println("XML Doc First Node Value is : "+xmlDoc.getFirstChild().getNodeName()); //Now converting XML Doc to String String xmlOutPut=convertXMLDocumentToString(xmlDoc); System.out.println("XML Doc to String: \n"+ xmlOutPut); } //Following method will to convert String to XML Document private static Document convertStringToXMLDoc(String strXMLValue) { try { //Create a new object of DocumentBuilderFactory DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); //Create an object DocumentBuilder to parse the specified XML Data DocumentBuilder builder = dbfactory.newDocumentBuilder(); //Parse the content to Document object Document doc = builder.parse(new InputSource(new StringReader(strXMLValue))); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } private static String convertXMLDocumentToString(Document xmlDoc) { //Create a new object of TransformerFactory TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer; try { transformer = transformerFactory.newTransformer(); //Creating object of the Source document that is xml doc DOMSource source = new DOMSource(xmlDoc); StringWriter strWriter = new StringWriter(); StreamResult stResult = new StreamResult(strWriter); transformer.transform(source, stResult); String xmlString = strWriter.getBuffer().toString(); return xmlString; } catch (TransformerException e) { e.printStackTrace(); } return null; } } |
Read an XML File and Convert it to XML Document in Java
Sometimes we need to read an XML file and perform various operations on it. The following java program shows us how to read an XML file and convert it to an XML document.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package mypack; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class XMLFileToXMLDoc { public static void main(String[] args) { // Read XML File String inputXMLFile = "C:\\TestXMLFile.xml"; //Converting XML Doc to String Document xmlDoc = convertXMLFileToXMLDoc( inputXMLFile ); //Get the first node of XML Document to validate whether XML document is build correctly System.out.println("XML Doc First Node Value is : "+xmlDoc.getFirstChild().getNodeName()); } // Method to convert XML File into XML Document private static Document convertXMLFileToXMLDoc(String strXMLFilePath) { try { File file = new File(strXMLFilePath); //Create a new object of DocumentBuilderFactory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //Create an object DocumentBuilder to parse the XML file data DocumentBuilder db = dbf.newDocumentBuilder(); Document xmlDoc = db.parse(file); return xmlDoc; } catch (Exception e) { e.printStackTrace(); } return null; } } |
Conclusion
We have learned how to convert String to an XML and an XML to String in Java.Also,we have seen way to convert XML file to XML document.I hope you would like this article.Please don’t forget to share it.
Recommended Posts
- Actions Class in Selenium WebDriver
- Read and Write Excel File in Java Using Apache POI Lib
- Intuitive Way of MySQL Database Testing in Selenium | LeanFT
- UFT Developer Installation in Eclipse IDE
- Create Application Model Project in LeanFT & Write First Test
- What is Selenium WebDriver & its Architecture
- How to Read XML File Node / Tag Values in Java Selenium