0

I am new in Android and I dont know which parser is best so guys can you help me to parse this XMl file

<poem name="P01">
    <stanza name="P1S1">
        <Line name="P1S1L1">
            <word name="P1S1L1W1">Twinkle</word>
            <word name="P1S1L1W2">twinkle</word>
            <word name="P1S1L1W3">little</word>
            <word name="P1S1L1W4">star</word>
        </Line>
        <Line name="P1S1L2">
            <word name="P1S1L2W1">How</word>
            <word name="P1S1L2W2">I</word>
            <word name="P1S1L2W3">wonder</word>
            <word name="P1S1L2W4">what</word>
            <word name="P1S1L2W5">you</word>
            <word name="P1S1L2W6">are</word>
        </Line>

    </stanza>

    </poem>

I was give Id or Name to every tag and I want to read the text/value of tag using that Name/ID not Tag Name because if I want to read the text wonder so there is a many word tag so how could I read the single value/text of the tag using that name/ID . So please give me suggestion on this sorry for my English Thanx in advance

devnull
  • 118,548
  • 33
  • 236
  • 227

2 Answers2

0

It is easy. You should use a DOM parser.

Here's a link.

You can also go for SAX parsing but that is rather complicated.

Community
  • 1
  • 1
Vikram Gupta
  • 6,496
  • 5
  • 34
  • 47
  • XML pull parser is easy or complicated for this xml – NitsDeveloper May 30 '13 at 08:57
  • Please give me the example of Pull parser – NitsDeveloper May 30 '13 at 10:44
  • XML pull parser would be a headache for this simple xml file. I don't encourage you to use Pull parser. Anyways here's a [link for XML PULL parsing](http://adf.ly/3103720/banner/http://rajeshvijayakumar.blogspot.in/2013/04/xml-pull-parser-example-in-android.html). – Vikram Gupta May 30 '13 at 11:20
0

use this code :

private static void xmlParser() {
    try {
        String filepath = "yourfile.xml";
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);
        // Get the root element
        Node min = doc.getElementsByTagName("poem").item(0);
        NamedNodeMap attr = min.getAttributes();
                    // get the value of the name=
        Node nodeAttr = attr.getNamedItem("name");
                    //if you want to change the value of the name use 
                    nodeAttr.setTextContent("new name value");
                    //if you want to get the value between >value< use 
                    nodeAttr.getTextContent()
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filepath));
        transformer.transform(source, result);
    } catch (ParserConfigurationException e) {
            e.printStackTrace();
    }
     catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException sae) {
            sae.printStackTrace();
        } catch (TransformerConfigurationException e) {

            e.printStackTrace();
        } catch (TransformerException e) {

            e.printStackTrace();
        }
}

just apply the above for the rest of the tags ..

mmoghrabi
  • 1,233
  • 1
  • 14
  • 23