将XML从网站解析为Android设备

时间:2022-10-29 19:42:09

I am starting an Android application that will parse XML from the web. I've created a few Android apps but they've never involved parsing XML and I was wondering if anyone had any tips on the best way to go about it?

我正在启动一个Android应用程序,它将从Web解析XML。我已经创建了一些Android应用程序,但他们从未涉及解析XML,我想知道是否有人有关于最佳方法的任何提示?

5 个解决方案

#1


9  

Here's an example:

这是一个例子:

        try {
            URL url = new URL(/*your xml url*/);
            URLConnection conn = url.openConnection();

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(conn.getInputStream());

            NodeList nodes = doc.getElementsByTagName(/*tag from xml file*/);
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                NodeList title = element.getElementsByTagName(/*item within the tag*/);
                Element line = (Element) title.item(0);
                phoneNumberList.add(line.getTextContent());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

In my example, my XML file looks a little like:

在我的示例中,我的XML文件看起来有点像:

<numbers>
   <phone>
      <string name = "phonenumber1">555-555-5555</string>
   </phone>
   <phone>
      <string name = "phonenumber2">555-555-5555</string>
   </phone>
</numbers>

and I would replace /*tag from xml file*/ with "phone" and /*item within the tag*/ with "string".

我将xml文件中的/ *标记替换为标记* /中的“phone”和/ * item以及“string”。

#2


1  

I always use the w3c dom classes. I have a static helper method that I use to parse the xml data as a string and returns to me a Document object. Where you get the xml data can vary (web, file, etc) but eventually you load it as a string.

我总是使用w3c dom类。我有一个静态帮助器方法,我用它来解析xml数据作为一个字符串,并返回给我一个Document对象。你得到的xml数据可能会有所不同(网络,文件等),但最终你把它作为一个字符串加载。

something like this...

像这样的东西......

    Document document = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;

    try
    {
        builder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(data));
        document = builder.parse(is);
    }
    catch (SAXException e) { }
    catch (IOException e) { }
    catch (ParserConfigurationException e) { }

#3


1  

There are different types of parsing mechanisms available, one is SAX Here is SAX parsing example, second is DOM parsing Here is DOM Parsing example.. From your question it is not clear what you want, but these may be good starting points.

有不同类型的解析机制可用,一个是SAX这里是SAX解析示例,第二个是DOM解析这里是DOM解析示例..从你的问题不清楚你想要什么,但这些可能是一个很好的起点。

#4


1  

There are three types of parsing I know: DOM, SAX and XMLPullParsing.

我知道有三种类型的解析:DOM,SAX和XMLPullParsing。

In my example here you need the URL and the parent node of the XML element.

在我的示例中,您需要URL和XML元素的父节点。

try {
    URL url = new URL("http://www.something.com/something.xml");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(url.openStream()));
    doc.getDocumentElement().normalize();

    NodeList nodeList1 = doc.getElementsByTagName("parent node here");
    for (int i = 0; i < nodeList1.getLength(); i++) {
        Node node = nodeList1.item(i);
    }
} catch(Exception e) {

}

Also try this.

也试试这个。

#5


0  

I would use the DOM parser, it is not as efficient as SAX, if the XML file is not too large, as it is easier in that case.

如果XML文件不是太大,我会使用DOM解析器,它不如SAX高效,因为在这种情况下它更容易。

I have made just one android App, that involved XML parsing. XML received from a SOAP web service. I used XmlPullParser. The implementation from Xml.newPullParser() had a bug where calls to nextText() did not always advance to the END_TAG as the documentation promised. There is a work around for this.

我只做了一个Android应用程序,涉及XML解析。从SOAP Web服务接收的XML。我用过XmlPullParser。 Xml.newPullParser()的实现有一个错误,即对文档承诺,对nextText()的调用并不总是前进到END_TAG。有一个解决方案。

#1


9  

Here's an example:

这是一个例子:

        try {
            URL url = new URL(/*your xml url*/);
            URLConnection conn = url.openConnection();

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(conn.getInputStream());

            NodeList nodes = doc.getElementsByTagName(/*tag from xml file*/);
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                NodeList title = element.getElementsByTagName(/*item within the tag*/);
                Element line = (Element) title.item(0);
                phoneNumberList.add(line.getTextContent());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

In my example, my XML file looks a little like:

在我的示例中,我的XML文件看起来有点像:

<numbers>
   <phone>
      <string name = "phonenumber1">555-555-5555</string>
   </phone>
   <phone>
      <string name = "phonenumber2">555-555-5555</string>
   </phone>
</numbers>

and I would replace /*tag from xml file*/ with "phone" and /*item within the tag*/ with "string".

我将xml文件中的/ *标记替换为标记* /中的“phone”和/ * item以及“string”。

#2


1  

I always use the w3c dom classes. I have a static helper method that I use to parse the xml data as a string and returns to me a Document object. Where you get the xml data can vary (web, file, etc) but eventually you load it as a string.

我总是使用w3c dom类。我有一个静态帮助器方法,我用它来解析xml数据作为一个字符串,并返回给我一个Document对象。你得到的xml数据可能会有所不同(网络,文件等),但最终你把它作为一个字符串加载。

something like this...

像这样的东西......

    Document document = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;

    try
    {
        builder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(data));
        document = builder.parse(is);
    }
    catch (SAXException e) { }
    catch (IOException e) { }
    catch (ParserConfigurationException e) { }

#3


1  

There are different types of parsing mechanisms available, one is SAX Here is SAX parsing example, second is DOM parsing Here is DOM Parsing example.. From your question it is not clear what you want, but these may be good starting points.

有不同类型的解析机制可用,一个是SAX这里是SAX解析示例,第二个是DOM解析这里是DOM解析示例..从你的问题不清楚你想要什么,但这些可能是一个很好的起点。

#4


1  

There are three types of parsing I know: DOM, SAX and XMLPullParsing.

我知道有三种类型的解析:DOM,SAX和XMLPullParsing。

In my example here you need the URL and the parent node of the XML element.

在我的示例中,您需要URL和XML元素的父节点。

try {
    URL url = new URL("http://www.something.com/something.xml");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(url.openStream()));
    doc.getDocumentElement().normalize();

    NodeList nodeList1 = doc.getElementsByTagName("parent node here");
    for (int i = 0; i < nodeList1.getLength(); i++) {
        Node node = nodeList1.item(i);
    }
} catch(Exception e) {

}

Also try this.

也试试这个。

#5


0  

I would use the DOM parser, it is not as efficient as SAX, if the XML file is not too large, as it is easier in that case.

如果XML文件不是太大,我会使用DOM解析器,它不如SAX高效,因为在这种情况下它更容易。

I have made just one android App, that involved XML parsing. XML received from a SOAP web service. I used XmlPullParser. The implementation from Xml.newPullParser() had a bug where calls to nextText() did not always advance to the END_TAG as the documentation promised. There is a work around for this.

我只做了一个Android应用程序,涉及XML解析。从SOAP Web服务接收的XML。我用过XmlPullParser。 Xml.newPullParser()的实现有一个错误,即对文档承诺,对nextText()的调用并不总是前进到END_TAG。有一个解决方案。