解析具有相同标记名称但唯一数据的XML数据

时间:2014-09-01 23:26:46

标签: java android xml xml-parsing domparser

我试图将xml数据解析为android中的列表视图,这是我从Web服务方法获得的xml结果,我想得到值ref_num,summary,customer FOR EACH对象,但它们具有相同的标签名称和

   <UDSObjectList>
    <UDSObject>
    <Handle>cr:400047</Handle>
    <Attributes>
    <Attribute DataType="2002">
    <AttrName>ref_num</AttrName>
    <AttrValue>1</AttrValue>
    </Attribute>
    <Attribute DataType="2002">
    <AttrName>summary</AttrName>
    <AttrValue>this is a summary</AttrValue>
    </Attribute>
    <Attribute DataType="2005">
    <AttrName>customer</AttrName>
    <AttrValue>111111</AttrValue>
    </Attribute>
    </Attributes>
    </UDSObject>
    <UDSObject>
    <Handle>cr:400040</Handle>
    <Attributes>
    <Attribute DataType="2002">
    <AttrName>ref_num</AttrName>
    <AttrValue>2</AttrValue>
    </Attribute>
    <Attribute DataType="2002">
    <AttrName>summary</AttrName>
    <AttrValue>another summary</AttrValue>
    </Attribute>
    <Attribute DataType="2005">
    <AttrName>customer</AttrName>
    <AttrValue>12123</AttrValue>
    </Attribute>
    </Attributes>
    </UDSObject>

这是我用来解析它的代码         尝试{             DocumentBuilderFactory dbf =                 DocumentBuilderFactory.newInstance();             DocumentBuilder db = dbf.newDocumentBuilder();             InputSource是= new InputSource();             is.setCharacterStream(new StringReader(theirID));

        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("UDSObject");

        // iterate the employees
        for (int i = 0; i < nodes.getLength(); i++) {
           Element element = (Element) nodes.item(i);

           NodeList name = element.getElementsByTagName("AttrName");
           Element line = (Element) name.item(i);
           System.out.println("ID: " + getCharacterDataFromElement(line));

           NodeList title = element.getElementsByTagName("AttrValue");
           line = (Element) title.item(i);
           System.out.println("ID Value: " + getCharacterDataFromElement(line));



           userID = getCharacterDataFromElement(line);


        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
.
.
.
public static String getCharacterDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
           CharacterData cd = (CharacterData) child;
           return cd.getData();
        }
        return "?";
    }

当我运行它时,我只获取FIRST对象的值,如何为所有对象检索它并将每个对象的值保存在ArrayList中并将其显示到列表视图中

0 个答案:

没有答案
相关问题