Android XML解析问题(多个标记)
我正在使用DOM解析器分析XML,但我似乎遇到了一个问题,我的Android XML解析问题(多个标记),android,xml,parsing,Android,Xml,Parsing,我正在使用DOM解析器分析XML,但我似乎遇到了一个问题,我的XML数据结构如下: <Bookings> <BookingNo>12345678</BookingNo> <Pickup> <AddressLine>Line 1</AddressLine> <AddressLine>Line 2</AddressLine> <AddressLine>L
XML
数据结构如下:
<Bookings>
<BookingNo>12345678</BookingNo>
<Pickup>
<AddressLine>Line 1</AddressLine>
<AddressLine>Line 2</AddressLine>
<AddressLine>Line 3</AddressLine>
<AddressLine>Line 4</AddressLine>
<AddressLine>Line 5</AddressLine>
<Postcode>
<PostcodeOut>pstOut</PostcodeOut>
<PostcodeIn>pstIn</PostcodeIn>
</Postcode>
<AddressCoords>
<Latitude>1.12345670</Latitude>
<Longitude>-1.1234567890</Longitude>
</AddressCoords>
</Pickup>
<Destination>
<AddressLine>Line 1</AddressLine>
<AddressLine>Line 2</AddressLine>
<AddressLine>Line 3</AddressLine>
<Postcode>
<PostcodeOut>pstOut</PostcodeOut>
<PostcodeIn>pstIn</PostcodeIn>
</Postcode>
<AddressCoords>
<Latitude>1.1234567890</Latitude>
<Longitude>-1.1234567890</Longitude>
</AddressCoords>
</Destination>
<PickupTime>DateTime</PickupTime>
<DestinationTime>DateTime</DestinationTime>
<VehicleType>Car</VehicleType>
<NumberOfPassengers>1</NumberOfPassengers>
<Passengers>
<PassengerName>Name</PassengerName>
<PassengerContactNo>123456</PassengerContactNo>
</Passengers>
<Mileage>2</Mileage>
</Bookings>
我的主要活动中的代码是:
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xml); // getting DOM
references = new ArrayList<String>();
NodeList nl = doc.getElementsByTagName("Bookings");
NodeList nlpickup = doc.getElementsByTagName("Pickup");
NodeList nldestination = doc
.getElementsByTagName("Destination");
AddressData = new StringBuilder();
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
resultCode = parser.getValue(e, "BookingNo");
Pickup = parser.getValue(e, "Pickup");
DateTime = parser.getValue(e, "PickupTime");
Element etwo = (Element) nlpickup.item(i);
PAddressTwo = parser.getValue(etwo, "AddressLine");
// System.out.println("/// "+ AddressData);
// System.out.println("/////"+parser.getValue(etwo,
// "AddressLine"));
PPostIn = parser.getValue(etwo, "PostcodeOut");
PPostOut = parser.getValue(etwo, "PostcodeIn");
Element ethree = (Element) nldestination.item(i);
DAddressOne = parser.getValue(ethree, "AddressLine");
DPostIn = parser.getValue(ethree, "PostcodeOut");
DPostOut = parser.getValue(ethree, "PostcodeIn");
splitDate();
map.put("BookNo", resultCode);
map.put("Datetime", TimeFinal + " on " + DateFinal);
map.put("PickupAddress", PAddressTwo + " " + PPostIn
+ " " + PPostOut);
map.put("DestinationAddress", DAddressOne + " "
+ DPostIn + " " + DPostOut);
references.add(resultCode);
mylist.add(map);
}
adapter = new SimpleAdapter(
MyJourney.this,
mylist,
R.layout.myjourneys_item,
new String[] { "BookNo", "Datetime",
"PickupAddress", "DestinationAddress" },
new int[] { R.id.journeysRefText,
R.id.journeysDateText,
R.id.journeysFromText, R.id.journeysToText });
}
XMLParser=newxmlparser();
Document doc=parser.getdoElement(xml);//获得DOM
references=newarraylist();
NodeList nl=doc.getElementsByTagName(“预订”);
NodeList nlpickup=doc.getElementsByTagName(“Pickup”);
NodeList nldestination=doc
.getElementsByTagName(“目的地”);
AddressData=新的StringBuilder();
//循环通过所有项目节点
对于(int i=0;i
我哪里做错了
我知道有一种叫做SAX解析器的东西,但我可以找到一个与我正在寻找的东西相匹配的示例。试试这个:
Element element = document.getDocumentElement();
// For Pickup
NodeList nodeListPickup = element.getElementsByTagName("Pickup");
for (int i = 0; i < nodeListPickup.getLength(); i++) {
Node node = nodeListPickup.item(i);
NodeList pickUp = node.getChildNodes();
for (int j = 0; j < pickUp.getLength(); j++) {
Node nodePickup = pickUp.item(j);
String name = nodePickup.getNodeName();
if(name.equals("AddressLine")){
System.out.println("Pickup > AddressLine > " +nodePickup.getFirstChild().getNodeValue());
}
}
}
// For Destination
NodeList nodeListDestination = element.getElementsByTagName("Destination");
for (int i = 0; i < nodeListDestination.getLength(); i++) {
Node node = nodeListDestination.item(i);
NodeList destination = node.getChildNodes();
for (int j = 0; j < destination.getLength(); j++) {
Node nodeDestination = destination.item(j);
String name = nodeDestination.getNodeName();
if(name.equals("AddressLine")){
System.out.println("Destination > AddressLine > " +nodeDestination.getFirstChild().getNodeValue());
}
}
}
Element=document.getDocumentElement();
//接送
NodeList nodeListPickup=element.getElementsByTagName(“拾取”);
对于(int i=0;i地址行>”+nodePickup.getFirstChild().getNodeValue());
}
}
}
//目的地
NodeList nodeListDestination=element.getElementsByTagName(“目的地”);
对于(int i=0;i地址行>”+nodeDestination.getFirstChild().getNodeValue());
}
}
}
我已经为皮卡和目的地创建了不同的节点列表,您可以在这两个节点中获得其余的内部标记,目前我只显示AddressLine。让我知道这是否有帮助。这确实很有帮助,现在只需要弄清楚如何正确地构造它lol
Element element = document.getDocumentElement();
// For Pickup
NodeList nodeListPickup = element.getElementsByTagName("Pickup");
for (int i = 0; i < nodeListPickup.getLength(); i++) {
Node node = nodeListPickup.item(i);
NodeList pickUp = node.getChildNodes();
for (int j = 0; j < pickUp.getLength(); j++) {
Node nodePickup = pickUp.item(j);
String name = nodePickup.getNodeName();
if(name.equals("AddressLine")){
System.out.println("Pickup > AddressLine > " +nodePickup.getFirstChild().getNodeValue());
}
}
}
// For Destination
NodeList nodeListDestination = element.getElementsByTagName("Destination");
for (int i = 0; i < nodeListDestination.getLength(); i++) {
Node node = nodeListDestination.item(i);
NodeList destination = node.getChildNodes();
for (int j = 0; j < destination.getLength(); j++) {
Node nodeDestination = destination.item(j);
String name = nodeDestination.getNodeName();
if(name.equals("AddressLine")){
System.out.println("Destination > AddressLine > " +nodeDestination.getFirstChild().getNodeValue());
}
}
}