Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple XML Parsing in Android

Tags:

android

xml

My xml is shown below:

<.sUID>yPkmfG3caT6cxexj5oWy34WiUUjj8WliWit45IzFVSOt6gymAOUA==<./sUID>
<.Shipping>0.00<./Shipping>
<.DocType>SO<./DocType>

How do I parse this simple xml in Android?

like image 200
Aakash Yatish Avatar asked Aug 04 '26 17:08

Aakash Yatish


1 Answers

Make a document:

public Document XMLfromString(String v){

        Document doc = null;

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(v));
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            System.out.println("Wrong XML file structure: " + e.getMessage());
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return doc;

    }

Then read it like so:

Document doc = x.XMLfromString("<XML><sUID>yPkmfG3caT6cxexj5oWy34WiUUjj8WliWit45IzFVSOt6gymAOUA==</sUID> <Shipping>0.00</Shipping> <DocType>SO</DocType></XML>");

NodeList nodes = doc.getElementsByTagName("XML");
like image 59
Mark Mooibroek Avatar answered Aug 07 '26 07:08

Mark Mooibroek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!