我正在使用xml,並遇到了一個我無法解決的問題。我在xml代碼中標記了相同的標籤,並且我需要將它們全部拉入,但目前我所擁有的代碼只是拉入第一個標籤。 XML代碼我試圖解析是:XML多標籤問題Android
<Pickup>
<AddressLine>Address Line 1</AddressLine>
<AddressLine>Address Line 2</AddressLine>
<AddressLine>Address Line 3</AddressLine>
<AddressLine>Address Line 4</AddressLine>
<AddressLine>Address Line 5</AddressLine>
<Postcode>
<PostcodeOut>PostCode One</PostcodeOut>
<PostcodeIn>PostCode Two</PostcodeIn>
</Postcode>
<AddressCoords>
<Latitude>00.000000</Latitude>
<Longitude>-0.000000</Longitude>
</AddressCoords>
我與分析數據的代碼是:
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");
NodeList nlAddress = doc.getElementsByTagName("AddressLine");
AddressData = new StringBuilder();
addressData = new ArrayList<String>();
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
resultCode = parser.getValue(e, "BookingNo");
DateTime = parser.getValue(e, "PickupTime");
Element etwo = (Element) nlpickup.item(i);
Element eaddress = (Element) nlAddress.item(i);
PAddressTwo = parser.getValue(eaddress, "AddressLine");
AddressData.append(PAddressTwo + " ,");
PPostIn = parser.getValue(etwo, "PostcodeOut");
PPostOut = parser.getValue(etwo, "PostcodeIn");
VType = parser.getValue(e, "VehicleType");
Dist =parser.getValue(e, "Mileage");
Element ethree = (Element) nldestination.item(i);
DAddressOne = parser.getValue(ethree, "AddressLine");
DPostIn = parser.getValue(ethree, "PostcodeOut");
DPostOut = parser.getValue(ethree, "PostcodeIn");
}
,我使用XML解析器是:
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sbtwo = new StringBuilder();
String myfeed = null;
while ((myfeed = reader.readLine()) != null) {
final String newjsontwo = myfeed.replaceAll(
"throw 'allowIllegalResourceCall is false.';", "");
sbtwo.append(newjsontwo);
}
is.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* @param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* @param elem element
*/
public final String getElementValue(Node elem) {
Node child;
if(elem != null){
if (elem.hasChildNodes()){
for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){
if(child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
任何想法如何我的標籤和代碼在哪裏我會出錯?
感謝您的支持,因爲它解釋了很多。我已經嘗試了你說的代碼,在我發佈這個評論之前,你說得對,它確實提供了所有的地址行數據,這對我來說是一個痛苦的大聲笑。我想也許如果我做了像e.getElementsByTagName(「皮卡」),並將其傳遞給一個字符串,應該得到的一切都在取代,然後我可以通過它到一個文檔生成器,然後得到的地址線只是一份文件,認爲可能有用? –
是的,沒有自己嘗試,你對文檔構建器和字符串的想法可能會起作用。但是,我認爲解析單個文檔會更高效。另外,您不必構建新文檔,因爲您可以對原始文檔進行一切處理。 – joel
如果我的回答是正確的,那麼接受它。我想要的點和這個如何stackoverflow工作hehe。 – joel