简体   繁体   English

android:从xml文件获取数据

[英]android : getting data from xml file

I want to parse an xml string of the format: 我想解析以下格式的xml字符串:

 <?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" 我能够提取联系人信息的值,例如ID,昵称等。我的问题是如何获取帐户电子邮件帐户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"); 我尝试了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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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