在Android中解析具有不同属性值的XML节点

时间:2013-02-17 18:10:05

标签: android xml-parsing

我有以下XML数据,

<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>

我想通过名称获取节点。我使用以下方法获取数据

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();
    }
}

但问题是,Iam只获取数据&#34; NBFundTransfer&#34;从第一个节点开始,名称为&#34; Type&#34;我想从所有其他节点(Id,Token等)获取数据。请有人帮我解决。谢谢..

1 个答案:

答案 0 :(得分: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);

}

}