简体   繁体   中英

android : getting data from xml file

I want to parse an xml string of the format:

 <?xml version='1.0' encoding='UTF-8' standalone='yes' ?><account email="john@example.com"><contacts><contact><id>0</id><nickname></nickname><firstname></firstname><lastname></lastname><emailxyz@gmail.com</email><passcode>p</passcode><creationdate>16 Dec 2013 17:40:58</creationdate><status>T</status></contact></contacts></account>

I am able to extract the values contact data such as id, nickname etc. My issue is how to get the value of account email account email="john@example.com"

extract of code is

NodeList account = doc.getElementsByTagName(Constants.ACCOUNTS_TAG);

        NodeList responseList = doc.getElementsByTagName("contact");

        for(int i =0 ;i < responseList.getLength();i++){
                             parser.getValue(response, Constants.EMAIL_TAG)
         }

I tried acct_email= parser.getValue((Element) account.item(0), "email"); but it is returning the email from contact not the email within account.

NodeList nList = doc.getElementsByTagName("account");

for (int temp = 0; temp < nList.getLength(); temp++) {

        Node nNode = nList.item(temp);

        System.out.println("\nCurrent Element :" + nNode.getNodeName());

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

            Element eElement = (Element) nNode;

            System.out.println("email id : " + eElement.getAttribute("email"));
            }
    }

xml文件基本上是一个字符串,因此您可以使用Scanner对象手动对其进行解析,也可以取消编组以将其转换为对象并获取其数据字段

NodeList accounts = getElementsByTagName("account");
for (int j = 0; j < accounts.getLength(); j++) {
    accounts.item(j).getAttributes().getNamedItem("email");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM