[英]android : getting data from xml file
我想解析以下格式的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>
我能夠提取聯系人信息的值,例如ID,昵稱等。我的問題是如何獲取帳戶電子郵件帳戶email =“ john@example.com”的值
代碼摘錄是
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)
}
我嘗試了acct_email= parser.getValue((Element) account.item(0), "email");
但它是從聯系人返回的電子郵件,而不是帳戶內的電子郵件。
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.