JavaExample.net | Xml String to Document

For Document we refer org.w3c.dom.Document.
Well, in this example we want to transform a xml string into a Document object

public static Document parseXmlFile(String xmlRequest)
    {
        // get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	// if we want manage namespace
        dbf.setNamespaceAware(true);
        try
        {
 
            // Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();
 
            // parse using builder to get DOM representation of the XML file
            return db.parse(new InputSource(new StringReader(xmlRequest)));
 
        }
        catch (ParserConfigurationException pce)
        {
            pce.printStackTrace();
        }
        catch (SAXException se)
        {
            se.printStackTrace();
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
        return null;
    }
 

Tagged with →  
Share →

Leave a Reply

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