Android XML Parse XMLfromString(String xml)

Here you can find the source of XMLfromString(String xml)

Description

XM Lfrom String

Declaration

public final static Document XMLfromString(String xml) 

Method Source Code

//package com.java2s;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

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

public class Main {
    public final static Document XMLfromString(String xml) {

        Document doc = null;/*from  w  w  w.  j av  a  2  s. com*/

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            dbf.setCoalescing(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        } catch (ParserConfigurationException e) {
            System.out.println("XML parse error: " + e.getMessage());
            return null;
        } catch (SAXException e) {
            System.out.println("Wrong XML file structure: "
                    + e.getMessage());
            return null;
        } catch (IOException e) {
            System.out.println("I/O exeption: " + e.getMessage());
            return null;
        }

        return doc;

    }
}

Related

  1. parse(String xml)
  2. parse(String xmlstr, Class clazz, List fields, List elements, String itemElement)
  3. parseResponse(HttpResponse response)
  4. parseTagValue(String xml, String tag, String endTag)
  5. ReadCityCode(Context context, String cityname)