我有一个从 web 服务调用获取 xml 响应的 Android 应用程序。我正在尝试使用 DOM 解析器解析 xml。我无法获取 StartDate 标记的值以及调用标记中的其他标记。我出错的任何想法。当我从 startDate 记录值时,它是空的。谢谢。
public class DomParser {
private static final String TAG = DomParser.class.getSimpleName();
DocumentBuilderFactory builderFactory;
DocumentBuilder builder;
Document document;
public DomParser() {
super();
Log.e(TAG, "inside domparser constructor");
builderFactory = DocumentBuilderFactory.newInstance();
document = null;
try {
builder = builderFactory.newDocumentBuilder();
Log.e(TAG, "built the dom factory");
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}// end of constructor
public void parseXmlString(String str){
try {
String s = str.replace("<", "<");
String t = s.replace(">", ">");
document = builder.parse( new InputSource(new StringReader(t)));
Log.e(TAG, "document = " + document);
Log.e(TAG, "str = " + t);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.e(TAG, "about to parse the rootElement");
Element rootElement = document.getDocumentElement();
NodeList nodes = rootElement.getChildNodes();
Node rota = nodes.item(0);
NodeList callList = rota.getChildNodes();
for(int i = 0; i < callList.getLength(); i++){
Node call = callList.item(i);
NodeList callChildrenList = call.getChildNodes();
for(int j = 0; j < callChildrenList.getLength(); j++){
Node callChild = callChildrenList.item(j);
if(callChild instanceof Element){
//a child element to process
Element child = (Element) callChild;
String startDate = child.getAttribute("StartDate");
Log.e(TAG, "element StartDate = " + startDate);
}
}
}
}//end of parseXmlString
}
.
<?xml version="1.0" encoding="utf-8"?>
10-05 15:49:17.680: E/DomParser(31495): <string xmlns="http://tempuri.org/"><Rota><Call><StartDate>2012-10-05T07:00:00+01:00</StartDate><EndDate>2012-10-05T07:25:00+01:00</EndDate><Duration>25</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Ebbs</ClientSurname><ClientForename>Vera</ClientForename><NeedName>Assist With Dressing, Laundry, Leave A Drink, Leave A Sandwich For Later, Make A Hot Drink, Prepare Breakfast, Shower, TIME BAND - Early Breakfast</NeedName><CarerAwayReason /><CallID>47418572-73a1-4dab-bc24-739c122b2b25</CallID><ActualTimeIn>07:30</ActualTimeIn><ActualTimeOut>07:49</ActualTimeOut></Call><Call><StartDate>2012-10-05T07:30:00+01:00</StartDate><EndDate>2012-10-05T08:00:00+01:00</EndDate><Duration>30</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Brown</ClientSurname><ClientForename>Margaret</ClientForename><NeedName>Careline, Diabetic Type 2, Empty Commode, KEY SAFE, Leave A Drink, Make A Hot Drink, Meds - Level II Administer, Prepare Snack, TIME BAND - Early Bed</NeedName><CarerAwayReason /><CallID>14f4f796-c7a6-4942-9d91-e9e7a40394f5</CallID><ActualTimeIn>08:00</ActualTimeIn><ActualTimeOut>08:21</ActualTimeOut></Call><Call><StartDate>2012-10-05T08:00:00+01:00</StartDate><EndDate>2012-10-05T08:40:00+01:00</EndDate><Duration>40</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Weaver</ClientSurname><ClientForename>Margaret</ClientForename><NeedName>KEY SAFE, Morning Call - Get Up And Breakfast, Personal Care</NeedName><CarerAwayReason /><CallID>2a7e6c70-ee2c-4657-a313-a18b87a766ed</CallID><ActualTimeIn>08:35</ActualTimeIn><ActualTimeOut>09:13</ActualTimeOut></Call><Call><StartDate>2012-10-05T09:00:00+01:00</StartDate><EndDate>2012-10-05T09:20:00+01:00</EndDate><Duration>20</Duration><CallStatusID>3</CallStatusID><CallStatusName>NCR</CallStatusName><ClientSurname>Howarth</ClientSurname><ClientForename>Edith</ClientForename><NeedName>Change Incontinence Pads, KEY SAFE, Make Bed / Change As Neccessary, Meds - Level II Administer, Personal Care, Prepare Breakfast, TIME BAND - Late Breakfast</NeedName><CarerAwayReason /><CallID>383c9be6-4cbe-459a-92ed-5dc309a0bffb</CallID></Call><Call><StartDate>2012-10-05T09:00:00+01:00</StartDate><EndDate>2012-10-05T09:30:00+01:00</EndDate><Duration>30</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Kastik</ClientSurname><ClientForename>Lydia</ClientForename><NeedName>Keys Needed, Leave A Drink, Leave A Sandwich For Later, Make A Hot Drink, Make Bed / Change As Neccessary, Medication Prompt, Meds - Level II Administer, Personal Care, TIME BAND - Early Breakfast</NeedName><CarerAwayReason /><CallID>f4cc1f96-ef65-4231-9e03-0b8263b04d27</CallID><ActualTimeIn>09:28</ActualTimeIn><ActualTimeOut>09:57</ActualTimeOut></Call><Call><StartDate>2012-10-05T10:30:00+01:00</StartDate><EndDate>2012-10-05T11:00:00+01:00</EndDate><Duration>30</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Terett</ClientSurname><ClientForename>Mamie</ClientForename><NeedName>Assist With Dressing, Meds - Self Medicating, Morning Call, Personal Care, Strip Wash, TIME BAND - Late Breakfast</NeedName><CarerAwayReason /><CallID>03f52182-b9ff-4fa9-9b71-1a20b8bbeeaa</CallID><ActualTimeIn>10:10</ActualTimeIn><ActualTimeOut>10:57</ActualTimeOut></Call><Call><StartDate>2012-10-05T12:00:00+01:00</StartDate><EndDate>2012-10-05T12:15:00+01:00</EndDate><Duration>15</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Garland</ClientSurname><ClientForename>Mavis</ClientForename><NeedName>Empty Catheter Bag, Medication Prompt, Meds - Level II Administer, TIME BAND - Early Lunch, Toiletting / Commode</NeedName><CarerAwayReason /><CallID>802d8adf-3a80-40bd-be91-f29db7c1291e</CallID><ActualTimeIn>11:16</ActualTimeIn><ActualTimeOut>11:38</ActualTimeOut></Call><Call><StartDate>2012-10-05T12:30:00+01:00</StartD
[更新]
Element rootElement = document.getDocumentElement();
NodeList nodes = rootElement.getChildNodes();
Node rota = nodes.item(0);
NodeList callList = rota.getChildNodes();
for(int i = 0; i < callList.getLength(); i++){
Node call = callList.item(i);
NodeList callChildrenList = call.getChildNodes();
for(int j = 0; j < callChildrenList.getLength(); j++){
Node callChild = callChildrenList.item(j);
Log.e(TAG, "callChild name = " + callChild.getNodeName());
}