如何获取字符串数组项的属性值?

时间:2011-11-30 15:49:54

标签: android

我的xml是这样的:

<string-array name="persons_array">
        <item deptcode="11S01">person 1</item>
        <item deptcode="12S01">person 2</item>
        <item deptcode="20S01">person 3</item>
        <item deptcode="35S04">person N</item>
    </string-array>

我只需要获取deptcode并将其放入编辑框中。

Ty

1 个答案:

答案 0 :(得分:0)

试试这个:

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource source = new InputSource();
source.setCharacterStream(new StringReader(xmlRecords));
Document doc = db.parse(source);
NodeList nodes = doc.getElementsByTagName("item");
for(int i = 0; i< nodes.getLength();i++)
{               
   NamedNodeMap attr = nodes.item(i).getAttributes();
//Next string is your deptcode 
   String str = attr.getNamedItem("deptcode").getNodeValue();
   //Here you can set it to EditText
}