在 Android 中使用 DOM 解析器

发布于 2024-10-19 19:27:11 字数 693 浏览 4 评论 0原文

我正在使用 DOM 解析器从 XML 文件中检索信息,如下所示:

<data>
    <metData>
        <wantedInformation>
    </metData>
    <metData>
        <Information>
    </metData>
    <metData>
        <Information>
    </metData>
<data>

问题是因为我不知道如何仅解析 的第一部分。我不需要第二部分和第三部分,但解析器无论如何都会显示它们。

xml 文件来自天气预报网站: http://www.meteo.si/uploads/probase /www/fproduct/text/sl/fcast_SLOVENIA_MIDDLE_latest.xml
我只需要以下行:oblačno

I'm using the DOM parser to retrive information from a XML file that looks like this:

<data>
    <metData>
        <wantedInformation>
    </metData>
    <metData>
        <Information>
    </metData>
    <metData>
        <Information>
    </metData>
<data>

The problem is because I don't know how to parse only the first part of <metData>. I don't need the second and the third part, but the parser displays them anyway.

The xml file is from a weather forcast site:
http://www.meteo.si/uploads/probase/www/fproduct/text/sl/fcast_SLOVENIA_MIDDLE_latest.xml
and I need just the following line: <nn_shortText>oblačno</nn_shortText>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記柔刀 2024-10-26 19:27:11

请注意您的 XML 文件是否格式正确,

您必须注意我在下面显示的三个方法,它们是

    1. getElementsByTagName - Mention the tag which you want to parse
    2.getChildNodes - retervies the child node 
    3.getNodeValue()- with the help of this method you can access the
 value of particular tag

步骤 1:创建一个解析 _Information_Value 的方法,以便解析信息标签的数据

String[] infoId=null;

public  void  parse_Information_Value() throws UnknownHostException{


        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {

            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(this.getInputStream());
            org.w3c.dom.Element root = dom.getDocumentElement();
            NodeList items = root.getElementsByTagName("metData");
            int a=items.getLength();
            int k=0;

            for (int i = 0; i < items.getLength(); i++) {
                Message_category message = new Message_category();
                Node item = items.item(i);
                NodeList properties = item.getChildNodes();
                for (int j = 0; j < properties.getLength(); j++) {
                    Node property = properties.item(j);
                    String name = property.getNodeName();
                    if (name.equalsIgnoreCase("wantedInformation")) {
                        message.setId(property.getFirstChild()
                                .getNodeValue());
                        infoId[k]=property.getFirstChild().getNodeValue();
                        k++;
                    }
                }

            }
        } catch (Exception e) {         }

    }

Pls take care whether your XML file is well formed or not,

You have to the notice three methods which i had shown below, they are

    1. getElementsByTagName - Mention the tag which you want to parse
    2.getChildNodes - retervies the child node 
    3.getNodeValue()- with the help of this method you can access the
 value of particular tag

Step 1: Create a Method to parse _Information_Value ,inorder to parse the data of Information tag

String[] infoId=null;

public  void  parse_Information_Value() throws UnknownHostException{


        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {

            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(this.getInputStream());
            org.w3c.dom.Element root = dom.getDocumentElement();
            NodeList items = root.getElementsByTagName("metData");
            int a=items.getLength();
            int k=0;

            for (int i = 0; i < items.getLength(); i++) {
                Message_category message = new Message_category();
                Node item = items.item(i);
                NodeList properties = item.getChildNodes();
                for (int j = 0; j < properties.getLength(); j++) {
                    Node property = properties.item(j);
                    String name = property.getNodeName();
                    if (name.equalsIgnoreCase("wantedInformation")) {
                        message.setId(property.getFirstChild()
                                .getNodeValue());
                        infoId[k]=property.getFirstChild().getNodeValue();
                        k++;
                    }
                }

            }
        } catch (Exception e) {         }

    }
倾城°AllureLove 2024-10-26 19:27:11

根据文档的大小,您可能还想使用面向流的解析器,例如 SAXStax,它不会将整个文档拉入内存,因此比 DOM 需要更少的内存。

好消息是 SAX 已内置于 Android 中,因此您可以立即使用它。
请参阅此链接了解使用示例。

Depending on the size of your document, you may also want to use at a streaming oriented parser like SAX or Stax, which does not pull the whole document into memory and thus needs less memory than DOM.

Good thing is that SAX is already built into Android, so you can use it right away.
See this link for a usage example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文