Java XML Document from String PDF Print E-mail
Written by Charles   
Wednesday, 30 December 2009 11:48

Sometimes you will begin with your XML in String form and want to be able to parse the DOM. You need a Document. Here's one way to do it. The code is HERE:

 

import org.w3c.dom.Document;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


public class XmlString {

    public Document xmlFromString(String xmlString) {
        Document result = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        InputSource source = new InputSource(new StringReader(xmlString));

        try {
            result = factory.newDocumentBuilder().parse(source);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }

        return result;
    }
}

 

Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Wednesday, 30 December 2009 11:53 )