0

我有以下 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();
    }
}

但问题是,我只从名为“Type”的第一个节点获取数据“NBFundTransfer”。我想从所有其他节点(Id、Token 等)获取数据。请有人帮我解决。谢谢..

4

1 回答 1

3

公共类 XMLParsingExample 扩展 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);

}

}

于 2013-02-17T18:31:15.130 回答