0

这是我的 XML:

<STATE sname="AndhraPradesh" >
    <DISTRICT dname="Ananthapur" >
        <address>
            Apollo Pharmacy, ANANTHAPUR (0.0 km)
            D.NO.15/545, NEAR SAPTHAGIRI CIRCLE ANANTHAPUR, Rayalaseema, ANANTHAPUR, ANATHAPUR , ANDHRA PRADESH
        </address>
    </DISTRICT>

    <DISTRICT dname="Kurnool" >
        <address>
            Apollo Pharmacy, KURNOOL (0.0 km)
            municipal no. D No: 46/697-A, Budwarpeta, KURNOOL, KURNOOL, ANDHRA PRADESH
        </address>
        <address>
            Apollo Pharmacy, KURNOOL 2 (0.0 km)
            1-155, Rayalaseema, KURNOOL 2, KURNOOL, ANDHRA PRADESH
        </address>
    </DISTRICT>

    <DISTRICT dname="EastGodawari" >

        <address>
            Apollo Pharmacy, VENKATESWAR COLONY (MAHABUB NAGAR) (0.0 km)
            D.No.7-4-58/A, Main Road Opp:A.P.S.E.B.Buliding, Mahabub, VENKATESWAR COLONY (MAHABUB NAGAR), HYDERABAD, ANDRA PRADESH
        </address>
        <address>
            Apollo Pharmacy, MAHABOOBNAGAR (0.0 km)
            1-5-35/6, Beside Andhrabank ATM, Mahaboobnagar, Telangana, MAHABOOBNAGAR, HYDERABAD, ANDHRA PRADESH
        </address>
        <address>
            Apollo Pharmacy, METTUGUDA - MAHABOOBANAGAR - 2 (0.0 km)
            1-4-30/D/B, Mahaboobnagar, METTUGUDA - MAHABOOBANAGAR - 2, HYDERABAD, ANDHRA PRADESH
        </address>
    </DISTRICT>

</STATE>

 <STATE sname="AndhraPradesh1111" >
    <DISTRICT dname="Ananthapur1111" >
        <address>
            Apollo Pharmacy, ANANTHAPUR (0.0 km)
        </address>
    </DISTRICT>

    <DISTRICT dname="Kurnool1111" >
        <address>
            Apollo Pharmacy, KURNOOL (0.0 km)
        </address>
        <address>
            Apollo Pharmacy, KURNOOL 2 (0.0 km)
        </address>
    </DISTRICT>

    <DISTRICT dname="EastGodawari1111" >

        <address>
            Apollo Pharmacy, VENKATESWAR COLONY (MAHABUB NAGAR) (0.0 km)
        </address>
        <address>
            Apollo Pharmacy, MAHABOOBNAGAR (0.0 km)
        </address>
        <address>
            Apollo Pharmacy, METTUGUDA - MAHABOOBANAGAR - 2 (0.0 km)
        </address>
    </DISTRICT>

</STATE>

我想在微调器中显示值。当我选择状态时,相关区域必须加载到区域微调器中。根据地区微调器,该地区的地址应加载到文本视图中。

4

1 回答 1

0

使用dom解析 //从/res/raw文件夹中读取myxml.xml文件 InputStream is=getResources().openRawResource(R.raw.country);

Log.i("TAG","is value==>"+is);
DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc=builder.parse(is, null);

//get the root node
NodeList nodeList=doc.getElementsByTagName("STATE sname");

Log.i("TAG","nodeList Length --" + nodeList.getLength()); //output:- -1
Log.i("TAG","nodeList name --" + nodeList.item(0).getNodeName());
//output:- message
for(int j=0; j<nodeList.getLength(); j++) {
Node nodee=nodeList.item(j);
for(int i=0;i<nodee.getChildNodes().getLength();i++)
{
Node node=nodee.getChildNodes().item(i);
if(node.getNodeName().equalsIgnoreCase("STATE sname")){
Log.i("TAG","from node::" + node.getTextContent());

countryname.add(node.getTextContent());
countrySelector.setAdapter(countryname);


countrySelector.setOnItemSelectedListener(this);

}
else if(node.getNodeName().equalsIgnoreCase("DISTRICT dname")){
Log.i("TAG","to node::" + node.getTextContent());
countryid.add(node.getTextContent());
}
}
}


is.close();
}
catch(Exception e){
System.out.println("ERROR while parsing xml:----" + e.getMessage());
}

此解析使用并使用微调器显示值

于 2012-07-31T06:39:15.553 回答