2014-07-07 169 views
2

我正在解析xml,但我無法解析其內部和另一個標籤中的xml標籤。 這是XML結構。Android XML解析 - 在標籤內部具有標籤的XML

<Entry> 
    <MediaID>434234242342</MediaID> 
    <MediaName>Brazil</MediaName> 
    <PhoneNo> 
    <Ip>23232323232</Ip> 
    <Ip>32323232323</Ip> 
    <Ip>323232323232</Ip> 
    </PhoneNo> 
</Entry> 

這是Java代碼。我米成功解析MediaID和MEDIANAME,但我怎麼能解析內

Document doc = parser.getDomElement(return_string); // getting DOM element 
NodeList nl2 = doc.getElementsByTagName("Entry"); 

    for (int i = 0; i < nl2.getLength(); i++) { 

     Element e = (Element) nl2.item(i); 
     media_name = parser.getValue(e,"MediaName"); 
     mediaID = parser.getValue(e,"MediaID"); 
     phoneNo = parser.getValue(e,"PhoneNo"); //it is not working 
    } 
+0

您如何看待phoneNo的價值?所有子標籤的第一個標籤或值的值或作爲xml? –

+0

嘗試爲標記Ip製作NodeList(您爲輸入所做的方式),然後遍歷列表以獲取電話號碼。 – gitter

+0

您正在使用哪個xml解析器? – alkis

回答

1

嘗試recurssion。您可以按照需要的方式格式化輸出。

import java.io.IOException; 
import java.io.StringReader; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

public class DOMParser { 
    public static void main(final String[] args) throws SAXException, IOException, ParserConfigurationException { 
     String xml = "<Entry>" 
       + "<MediaID>434234242342</MediaID>" 
       + "<MediaName>Brazil</MediaName>" 
       + "<PhoneNo>" 
        + "<Ip>23232323232</Ip>" 
        + "<Ip>32323232323</Ip>" 
        + "<Ip>323232323232</Ip>" 
       + "</PhoneNo>" 
      + "</Entry>"; 
     DOMParser parser = new DOMParser(); 
     final Document doc = parser.getDomElement(xml); 
     parser.parse(doc.getDocumentElement()); 
    } 

    public void parse(final Element e) { 
     final NodeList children = e.getChildNodes(); 
     for (int i = 0; i < children.getLength(); i++) { 
      final Node n = children.item(i); 
      if(n.getNodeType() == Node.TEXT_NODE){ 
      System.out.println(n.getTextContent()); 
      } else if (n.getNodeType() == Node.ELEMENT_NODE) { 
      System.out.print(n.getNodeName() + " : "); 
      parse((Element) n); 
      } 
     } 
    } 

    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) { 
      return null; 
      } catch (SAXException e) { 
      return null; 
      } catch (IOException e) { 
      return null; 
      } 

      return doc; 
     } 
} 
1

的標籤,我不知道是什麼解析器是你的工作,我會建議XMLPullParser:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
     XmlPullParser xpp = factory.newPullParser(); 





     xpp.setInput(reader);// the reader you are using to read the xml file 


     int eventType = xpp.getEventType(); 

     // Loop through pull events until we reach END_DOCUMENT 
     while (eventType != XmlPullParser.END_DOCUMENT) { 
      // Get the current tag 
      String tagname = xpp.getName(); 

      // React to different event types appropriately 
      switch (eventType) { 
      case XmlPullParser.START_TAG: 
       if (tagname.equalsIgnoreCase(THE_TAG_YOUR_LOOKING_FOR)) { 
        //anything you want to do at the start of the tag 
       } 
       break; 

      case XmlPullParser.TEXT: 

       //normally you would retrive here the text which is between the tags 
       //with xpp.getText() 
       break; 

      case XmlPullParser.END_TAG: 
       //generally a serie of if else depending on which tag you are on and 
       //what you want to do with the content 
       break; 

      default: 
       break; 
      } 
+0

我如何獲得使用ip標記這個代碼?並存儲不同的變量? –