Search code examples
androidxmldomxml-parsingdomparser

Parsing XML file stored in internal storage using DOM parser in android.


I have created an xml file in the device's internal storage as described on the android developers website. I now want to parse the file using DOM parser. What do i need to do to make the DOM parser read my XML file?? Here's a snippet:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document dom = db.parse(new InputSource(new StringReader(data)));
    dom.getDocumentElement().normalize(); 

What do i need to put in the place of "data" in:

    Document dom = db.parse(new InputSource(new StringReader(data)));

I know it's silly but any help would be appreciated.


Solution

  • You can make a input stream of the xml string like below and then getting nodes you can parse to get values.

    InputStream is = new ByteArrayInputStream(theXMLString.getBytes("UTF-8"));
    
            // Build XML document
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(is);
    

    Remember you are passing xml file as a string.