Question

That's my xml file:

    <name>John</name>
    <currency>
    <euro>1</euro>
    <dollar>1</dollar>
    </currency>
    <name>Jordan</name>
    <currency>
    <euro>2</euro>
    <dollar>2</dollar>
    </currency>

And my Java parseXML() method looks like this:

public void parseXML() throws XmlPullParserException, IOException,
        NullPointerException {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();
File file = new File("in.xml");

FileInputStream fis = new FileInputStream(file);
xpp.setInput(new InputStreamReader(fis));

int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_TAG) {

        String tag = xpp.getName();

        Currency c = new Currency();
        if (tag.equals("euro")) {
            eventType = xpp.next();
            c.setEuro(Integer.parseInt(xpp.getText()));

        }
        if (tag.equals("dollar")) {
            eventType = xpp.next();
            c.setDollar(Integer.parseInt(xpp.getText()));
        }

        if (tag.equals("name")) {
            eventType = xpp.next();
            nameList.add(xpp.getText());

        }

How to parse tags euro and dollar inside currency tag? I have no idea how to do this. Anyone can help me with this problem? I looked for it in google but didn't get my solution.

Was it helpful?

Solution

You might want to take a look at the XML pull parser tutorial on the Android Developers site. Here's a partial outline of a solution adapted from that tutorial.

First create a method that goes through the document and looks for the top-level tags you're interested in:

private void readDocument(XmlPullParser parser) throws XmlPullParserException, IOException {
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("name")) {
            readName(parser);
        } else if (name.equals("currency")) 
               readCurrency(parser);
        else {
            skip(parser);
        }
    }  
}

Then handle each tag with its own method, repeating the process as necessary:

private void readCurrency(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "currency");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("euro")) {
            euro = readEuro(parser);
        } else if (name.equals("dollar")) {
            dollar = readDollar(parser);
        } else {
            skip(parser);
        }
    }
}

// Read in the content of the Euro element.
private String readEuro(XmlPullParser parser) {
    return readText(parser);
}

I've omitted the skip() and readText() methods, which are detailed in the tutorial, and you'll obviously also want to add code to store the data you read in somehow. But hopefully that gives you an idea of how you can structure your parser to deal with the nested elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top