如何从xml中获取选定的值?

时间:2022-12-03 10:14:30

Maybe somebody can help me with this question. I try to reed XML file with this code:

也许有人可以帮我解决这个问题。我尝试用这段代码reed XML文件:

    protected void parseAndReadXML (String xmlFile){
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(false);
            DocumentBuilder db = dbf.newDocumentBuilder();
            try {
                Document doc = db.parse(xmlFile);
                doc.getDocumentElement().normalize();
                System.out.println(doc.getDocumentElement().getNodeName());

                NodeList nList = doc.getElementsByTagName("films");

                System.out.println("Information of comedy films");

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

                    Node firstNode = nList.item(i);

                    if(firstNode.getNodeType() == Node.ELEMENT_NODE){

                        Element firstElem = (Element) firstNode;

                        NodeList fstNmElmntLst = firstElem.getElementsByTagName("name");

                          Element fstNmElmnt = (Element) fstNmElmntLst.item(0);

                          NodeList fstNm = fstNmElmnt.getChildNodes();

                          System.out.println("Name : "  + ((Node) fstNm.item(0)).getNodeValue());

                          NodeList secNmElmntLst = firstElem.getElementsByTagName("actor");

                          Element secNmElmnt = (Element) secNmElmntLst.item(0);

                          NodeList secNm = secNmElmnt.getChildNodes();

                          System.out.println("Actor Name : " + ((Node) secNm.item(0)).getNodeValue());

                          NodeList therNmElmntLst = firstElem.getElementsByTagName("year");

                          Element lstNmElmnt = (Element) therNmElmntLst.item(0);

                          NodeList therNm = lstNmElmnt.getChildNodes();

                          System.out.println("Year : " + ((Node) therNm.item(0)).getNodeValue());
                    }
                }

            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {       
                e.printStackTrace();
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }   
    }

This is the XML that i work with it

这是我使用它的XML

<CinemaCity>
  <genre>
    <comedy>
        <films>
            <name>A</name>
            <actor>CBrus</actor>
            <year>111</year>
        </films>
        <films>
            <name>B</name>
            <actor>CBrus2</actor>
            <year>111</year>
        </films>
        <films>
            <name>C</name>
            <actor>CBrus3</actor>
            <year>111</year>
        </films>
        <films>
            <name>D</name>
            <actor>CBrus4</actor>
            <year>111</year>
        </films>
    </comedy>
    <horror>
        <films>
            <name>A</name>
            <actor>HBrus</actor>
            <year>111</year>
        </films>
        <films>
            <name>B</name>
            <actor>HBrus2</actor>
            <year>111</year>
        </films>
        <films>
            <name>C</name>
            <actor>HBrus3</actor>
            <year>111</year>
        </films>
        <films>
            <name>D</name>
            <actor>HBrus4</actor>
            <year>111</year>
        </films>
     </horror>
   </genre>
</CinemaCity>

I need to print only Comedy film. After running this code i get all films in all genre.
What wrong i did?

我只需要打印喜剧电影。运行此代码后,我将获得所有类型的所有电影。我做错了什么?

3 个解决方案

#1


2  

You should use xpath to search XML documents. Not only is it the most concise api, it is also the most efficient when traversing the XML doc.

您应该使用xpath来搜索XML文档。它不仅是最简洁的api,它在遍历XML文档时也是最有效的。

Here is the piece of code you need:

这是您需要的一段代码:

import javax.xml.xpath.*;
import javax.xml.parsers.*;

Document doc = db.parse(xmlFile);

XPath xPath =  XPathFactory.newInstance().newXPath();
NodeList nList = (NodeList)xPath.compile("/CinemaCity/genre/comedy/films")
        .evaluate(doc, XPathConstants.NODESET);

System.out.println("Information of comedy films");
...

#2


1  

With this line

有了这条线

NodeList nList = doc.getElementsByTagName("films");

You select all films. If you modify the films to comedy, your nodelist will contain all the films under comedy.

你选择所有电影。如果您将影片修改为喜剧,您的节点列表将包含所有喜剧电影。

#3


0  

Change:

NodeList nList = doc.getElementsByTagName("films");

To:

NodeList nList = doc.getElementsByTagName( "comedy" ).item( 0 ).getChildNodes();

In this case you select a first comedy films node and then all child nodes for it.

在这种情况下,您选择第一个喜剧电影节点,然后选择所有子节点。

#1


2  

You should use xpath to search XML documents. Not only is it the most concise api, it is also the most efficient when traversing the XML doc.

您应该使用xpath来搜索XML文档。它不仅是最简洁的api,它在遍历XML文档时也是最有效的。

Here is the piece of code you need:

这是您需要的一段代码:

import javax.xml.xpath.*;
import javax.xml.parsers.*;

Document doc = db.parse(xmlFile);

XPath xPath =  XPathFactory.newInstance().newXPath();
NodeList nList = (NodeList)xPath.compile("/CinemaCity/genre/comedy/films")
        .evaluate(doc, XPathConstants.NODESET);

System.out.println("Information of comedy films");
...

#2


1  

With this line

有了这条线

NodeList nList = doc.getElementsByTagName("films");

You select all films. If you modify the films to comedy, your nodelist will contain all the films under comedy.

你选择所有电影。如果您将影片修改为喜剧,您的节点列表将包含所有喜剧电影。

#3


0  

Change:

NodeList nList = doc.getElementsByTagName("films");

To:

NodeList nList = doc.getElementsByTagName( "comedy" ).item( 0 ).getChildNodes();

In this case you select a first comedy films node and then all child nodes for it.

在这种情况下,您选择第一个喜剧电影节点,然后选择所有子节点。