如何在Java中使用XPath读取XML

我想用Java中的XPath读取XML数据,因此对于我收集的信息,我无法根据我的要求解析XML。

这是我想要做的事情:

通过它的URL从在线获取XML文件,然后使用XPath解析它,我想在其中创建两个方法。 一个是我在其中输入一个特定的节点属性ID,我得到所有的子节点作为结果,第二个是假设我只想获得一个特定的子节点值

<?xml version="1.0"?>
<howto>
  <topic name="Java">
      <url>http://www.rgagnonjavahowto.htm</url>
  <car>taxi</car>
  </topic>
  <topic name="PowerBuilder">
       <url>http://www.rgagnon/pbhowto.htm</url>
       <url>http://www.rgagnon/pbhowtonew.htm</url>
  </topic>
  <topic name="Javascript">
        <url>http://www.rgagnon/jshowto.htm</url>
  </topic>
 <topic name="VBScript">
       <url>http://www.rgagnon/vbshowto.htm</url>
 </topic>
 </howto>

在上面的示例中,如果我通过@name进行搜索,并且还想从@name'Javascript'中只返回一个节点元素,我想要读取所有元素。

我希望我清除了我的问题:)

谢谢。


你需要做的事情如下:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(<xpath_expression>);

然后调用expr.evaluate()传递该代码中定义的文档和您期望的返回类型,并将结果转换为结果的对象类型。

如果你需要特定XPath表达式的帮助,你应该把它作为单独的问题来提出(除非这是你的问题 - 我理解你的问题是如何在Java中使用API​​)。

编辑:(对评论的回应):这个XPath表达式将为您提供PowerBuilder下的第一个URL元素的文本:

/howto/topic[@name='PowerBuilder']/url/text()

这会让你获得第二个:

/howto/topic[@name='PowerBuilder']/url[2]/text()

你用这个代码得到它:

expr.evaluate(doc, XPathConstants.STRING);

如果您不知道给定节点中有多少个网址,那么您应该这样做:

XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

然后遍历NodeList。


你可以试试这个。

XML文档

另存为employees.xml

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
    <Employee id="1">
        <age>29</age>
        <name>Pankaj</name>
        <gender>Male</gender>
        <role>Java Developer</role>
    </Employee>
    <Employee id="2">
        <age>35</age>
        <name>Lisa</name>
        <gender>Female</gender>
        <role>CEO</role>
    </Employee>
    <Employee id="3">
        <age>40</age>
        <name>Tom</name>
        <gender>Male</gender>
        <role>Manager</role>
    </Employee>
    <Employee id="4">
        <age>25</age>
        <name>Meghan</name>
        <gender>Female</gender>
        <role>Manager</role>
    </Employee>
</Employees>

解析器类

这个类有以下几种方法

  • 项目清单
  • 一个将返回输入标识的员工姓名的方法。
  • 将返回年龄大于输入年龄的员工名称列表的方法。
  • 一种将返回女性雇员姓名列表的方法。
  • 源代码

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    
    public class Parser {
    
        public static void main(String[] args) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder;
            Document doc = null;
            try {
                builder = factory.newDocumentBuilder();
                doc = builder.parse("employees.xml");
    
                // Create XPathFactory object
                XPathFactory xpathFactory = XPathFactory.newInstance();
    
                // Create XPath object
                XPath xpath = xpathFactory.newXPath();
    
                String name = getEmployeeNameById(doc, xpath, 4);
                System.out.println("Employee Name with ID 4: " + name);
    
                List<String> names = getEmployeeNameWithAge(doc, xpath, 30);
                System.out.println("Employees with 'age>30' are:" + Arrays.toString(names.toArray()));
    
                List<String> femaleEmps = getFemaleEmployeesName(doc, xpath);
                System.out.println("Female Employees names are:" +
                        Arrays.toString(femaleEmps.toArray()));
    
            } catch (ParserConfigurationException | SAXException | IOException e) {
                e.printStackTrace();
            }
    
        }
    
    
        private static List<String> getFemaleEmployeesName(Document doc, XPath xpath) {
            List<String> list = new ArrayList<>();
            try {
                //create XPathExpression object
                XPathExpression expr =
                    xpath.compile("/Employees/Employee[gender='Female']/name/text()");
                //evaluate expression result on XML document
                NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
                for (int i = 0; i < nodes.getLength(); i++)
                    list.add(nodes.item(i).getNodeValue());
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
            return list;
        }
    
    
        private static List<String> getEmployeeNameWithAge(Document doc, XPath xpath, int age) {
            List<String> list = new ArrayList<>();
            try {
                XPathExpression expr =
                    xpath.compile("/Employees/Employee[age>" + age + "]/name/text()");
                NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
                for (int i = 0; i < nodes.getLength(); i++)
                    list.add(nodes.item(i).getNodeValue());
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
            return list;
        }
    
    
        private static String getEmployeeNameById(Document doc, XPath xpath, int id) {
            String name = null;
            try {
                XPathExpression expr =
                    xpath.compile("/Employees/Employee[@id='" + id + "']/name/text()");
                name = (String) expr.evaluate(doc, XPathConstants.STRING);
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
    
            return name;
        }
    
    }
    

    入门示例:

    xml文件:

    <inventory>
        <book year="2000">
            <title>Snow Crash</title>
            <author>Neal Stephenson</author>
            <publisher>Spectra</publisher>
            <isbn>0553380958</isbn>
            <price>14.95</price>
        </book>
    
        <book year="2005">
            <title>Burning Tower</title>
            <author>Larry Niven</author>
            <author>Jerry Pournelle</author>
            <publisher>Pocket</publisher>
            <isbn>0743416910</isbn>
            <price>5.99</price>
        </book>
    
        <book year="1995">
            <title>Zodiac</title>
            <author>Neal Stephenson</author>
            <publisher>Spectra</publisher>
            <isbn>0553573862</isbn>
            <price>7.50</price>
        </book>
    
        <!-- more books... -->
    
    </inventory>
    

    Java代码:

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    
    
    try {
    
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse (new File("c:tmpmy.xml"));
    
        // normalize text representation
        doc.getDocumentElement().normalize();
        System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
    
        NodeList listOfBooks = doc.getElementsByTagName("book");
        int totalBooks = listOfBooks.getLength();
        System.out.println("Total no of books : " + totalBooks);
    
        for(int i=0; i<listOfBooks.getLength() ; i++) {
    
            Node firstBookNode = listOfBooks.item(i);
            if(firstBookNode.getNodeType() == Node.ELEMENT_NODE) {
    
                Element firstElement = (Element)firstBookNode;                              
                System.out.println("Year :"+firstElement.getAttribute("year"));
    
                //-------
                NodeList firstNameList = firstElement.getElementsByTagName("title");
                Element firstNameElement = (Element)firstNameList.item(0);
    
                NodeList textFNList = firstNameElement.getChildNodes();
                System.out.println("title : " + ((Node)textFNList.item(0)).getNodeValue().trim());
            }
        }//end of for loop with s var
    } catch (SAXParseException err) {
        System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
        System.out.println(" " + err.getMessage ());
    } catch (SAXException e) {
        Exception x = e.getException ();
        ((x == null) ? e : x).printStackTrace ();
    } catch (Throwable t) {
        t.printStackTrace ();
    }                
    
    链接地址: http://www.djcxy.com/p/68485.html

    上一篇: How to read XML using XPath in Java

    下一篇: Java OCR implementation