Convert string to XML document in Java

Occasionally, when coding in Java, we come across a String that is actually an XML. In order to manipulate it, we must convert it into an XML Document (org.w3c.dom.Document). Additionally, for debugging or passing it to another function, there may be a need to convert a Document object into a String. To address these requirements, I am presenting two utility functions.

        1. The function convertStringToDocument takes a string as input and converts it into a

      DOM

        Document before returning it. To accomplish this, we will utilize InputSource and StringReader.

     

      The function convertDocumentToString, on the other hand, receives a Document as input and converts it into a string. To achieve this, we will make use of Transformer, StringWriter, and StreamResult.
    package com.scdev.xml;
    
    import java.io.StringReader;
    import java.io.StringWriter;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.OutputKeys;
    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 StringToDocumentToString {
    
        public static void main(String[] args) {
            final String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"+
                                    "<Emp id=\"1\"><name>Pankaj</name><age>25</age>\n"+
                                    "<role>Developer</role><gen>Male</gen></Emp>";
            Document doc = convertStringToDocument(xmlStr);
            
            String str = convertDocumentToString(doc);
            System.out.println(str);
        }
    
        private static String convertDocumentToString(Document doc) {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer;
            try {
                transformer = tf.newTransformer();
                // below code to remove XML declaration
                // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                StringWriter writer = new StringWriter();
                transformer.transform(new DOMSource(doc), new StreamResult(writer));
                String output = writer.getBuffer().toString();
                return output;
            } catch (TransformerException e) {
                e.printStackTrace();
            }
            
            return null;
        }
    
        private static Document convertStringToDocument(String xmlStr) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
            DocumentBuilder builder;  
            try  
            {  
                builder = factory.newDocumentBuilder();  
                Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) ); 
                return doc;
            } catch (Exception e) {  
                e.printStackTrace();  
            } 
            return null;
        }
    
    }
    

    When we execute the program mentioned above, we obtain the identical String output that was used to form the DOM Document.

    <?xml version="1.0" encoding="UTF-8"?><Emp id="1"><name>Pankaj</name><age>25</age>
    <role>Developer</role><gen>Male</gen></Emp>
    

    To get a condensed version of a String without new line characters, you can utilize the replaceAll(“\n|\r”, “”) method.

     

    more tutorials:

    One example of Spring REST XML and JSON(Opens in a new browser tab)

    Interview Questions for Web Services – SOAP, RESTful(Opens in a new browser tab)

    convert string to character array in Java.(Opens in a new browser tab)

    Java String substring() method(Opens in a new browser tab)

     

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds