0

I have following XML data,

<RESPONSE>
<param name="Type">NBFundTransfer</param>
<param name="Id">3213</param>
<param name="Token">26&ffr$5%877</param>
<param name="Stage">1</param>
</RESPONSE>

I want to fetch the node by its name.I use following method to fetch data,

NodeList nl = doc.getElementsByTagName("RESPONSE");
String[] Agreement = new String[nl.getLength()];
for (int i = 0; i < nl.getLength(); i++) {
    Node item = nl.item(i);
    if (item.getNodeType() == Node.ELEMENT_NODE) {
        Element ielem = (Element) item;
        NodeList id = ielem.getElementsByTagName("param");
        Data[i] = id.item(0).getChildNodes().item(0).getNodeValue();
    }
}

But the problem is, Iam only getting the data "NBFundTransfer" from first node which is names as "Type" .I want to fetch the data from all other nodes(Id,Token etc).Please someone help me to get a solution.Thank you..

6
  • you are parsing the wrong tag name, make it 'type' instead of 'param. Commented Feb 17, 2013 at 18:14
  • When i used type instead of param, it throws an error... Commented Feb 17, 2013 at 18:25
  • 1
    @BasimSherif : You're not iterating over your NodeList called id. You're only using id.item(0) Commented Feb 17, 2013 at 18:28
  • @Squonk: Can you please show me how can I do it? :) Commented Feb 17, 2013 at 18:29
  • @BasimSherif : Use a for loop. Commented Feb 17, 2013 at 18:32

1 Answer 1

3

public class XMLParsingExample extends Activity {

/** Create Object For SiteList Class */
SitesList sitesList = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** Create a new layout to display the view */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);

    /** Create a new textview array to display the results */
    TextView name[];
    TextView website[];
    TextView category[];

    try {

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL(
                "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    /** Get result from MyXMLHandler SitlesList Object */
    sitesList = MyXMLHandler.sitesList;

    /** Assign textview array lenght by arraylist size */
    name = new TextView[sitesList.getName().size()];
    website = new TextView[sitesList.getName().size()];
    category = new TextView[sitesList.getName().size()];

    /** Set the result text in textview and add it to layout */
    for (int i = 0; i < sitesList.getName().size(); i++) {
        name[i] = new TextView(this);
        name[i].setText("Name = "+sitesList.getName().get(i));
        website[i] = new TextView(this);
        website[i].setText("Website = "+sitesList.getWebsite().get(i));
        category[i] = new TextView(this);
        category[i].setText("Website Category = "+sitesList.getCategory().get(i));

        layout.addView(name[i]);
        layout.addView(website[i]);
        layout.addView(category[i]);
    }

    /** Set the layout view to display */
    setContentView(layout);

}

}

Sign up to request clarification or add additional context in comments.

2 Comments

use this Activity class for your xml parsing.
If you're going to answer a question on stackoverflow, answer it with relevance to the code in the question. Don't just post a load if irrelevant code and basically say "use this instead".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.