2012-12-03 39 views
1

我正在使用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)); 
    } 

任何想法如何我的标签和代码在哪里我会出错?

回答

0

你没有做错任何事。解析多个元素基本上就是您使用Bookings NodeList nl所做的。使用标记名称Document类调用getElementsByTagName获取NodeList,并使用NodeList的getLength()方法和item()方法遍历NodeList内的节点。

NodeList nl = doc.getElementsByTagName("AdressLine"); 
for(int i = 0; i < nl.getLength(); i++) { 
    Node n = nl.item(0); 
    // do something with node... 
    Element e = (Element)n; 
    e.getElementsByTagName("PostCode"); 
} 

如果Node对象被转换为Element类,则Element类对象只允许访问相应Node对象的子元素。在上面的代码中,e.getElementsByTagName("PostCode");仅返回相应AdressLine元素的PostCode子元素。

对于您的情况,使用以下代码行,例如在执行Document类的getElementsByTagName方法时,解析整个xml文档的每个AdressLine。 Document类的doc对象对应于整个文档。

NodeList nlAddress = doc.getElementsByTagName("AddressLine"); 

我希望这有助于!

+0

感谢您的支持,因为它解释了很多。我已经尝试了你说的代码,在我发布这个评论之前,你说得对,它确实提供了所有的地址行数据,这对我来说是一个痛苦的大声笑。我想也许如果我做了像e.getElementsByTagName(“皮卡”),并将其传递给一个字符串,应该得到的一切都在取代,然后我可以通过它到一个文档生成器,然后得到的地址线只是一份文件,认为可能有用? –

+0

是的,没有自己尝试,你对文档构建器和字符串的想法可能会起作用。但是,我认为解析单个文档会更高效。另外,您不必构建新文档,因为您可以对原始文档进行一切处理。 – joel

+0

如果我的回答是正确的,那么接受它。我想要的点和这个如何stackoverflow工作hehe。 – joel