Android XML解析 - 带有嵌套标签的XML

3

我正在解析XML,但是无法解析嵌套在和另一个标记中的XML标记。以下是XML结构。

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

这是Java代码。我成功解析了MediaID和MediaName,但如何解析

标签内的<br>标签?
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
   }

你期望phoneNo的值是什么?第一个标签的值还是所有子标签的值还是作为XML? - Syam S
尝试为标签Ip创建一个NodeList(就像你为entry所做的那样),然后迭代列表以获取电话号码。 - gitter
你使用的是哪个XML解析器? - Alkis Kalogeris
@SyamS 我想要获取 <IP> 的值。我想要获取所有 <PhoneNo> 子标签的 app 值。 - Kami
@alkis 我正在使用这个例子 http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/ - Kami
2个回答

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;
            }

我该如何使用这段代码获取IP标签并将其存储到不同的变量中? - Arpit Patel

1
尝试使用递归。您可以按照自己的方式格式化输出。
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;
        }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接