0

我使用 Java,并且我已经实现了一个 DOM 解析器来解析一个 xml 文件。

我使用 XPath 来浏览我的 xml 文档中的元素/属性。

我有一个特定的 xpath 表达式:

XPathExpression exprBoolean = xpath.compile("/root/customer/name/text()='George'");
Boolean test = (Boolean) exprBoolean.evaluate(doc, XPathConstants.BOOLEAN);

当客户 George 在 xml 文件中时,前面的表达式返回 true,当该名称 (George) 不在 xml 文件中时返回 false。

在我的 Java 编程文件中,我使用了映射到 Java 布尔值的 XPathConstants.BOOLEAN 数据类型。

如果我在 xml 文件中找到 George 客户(如果 test == true),我想输出该节点的子节点或节点本身。我可以这样做吗?如果布尔表达式为真,我想返回那个特定的节点。那可能吗?

我的目标是返回一个特定的节点,以防该节点包含特定的文本()。但为了测试这一点,我将执行“text()='George'”并且该表达式返回一个布尔值而不是一个节点。那么,在我使用 XPathConstants.BOOLEAN 之后如何返回一个节点?

ParseFileXML如下:

package dom_parser;

import java.io.IOException;
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 ParseFileXML {


  public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {


  DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  domFactory.setNamespaceAware(true); 
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("root.xml");

  XPath xpath = XPathFactory.newInstance().newXPath();   
  //XPathExpression exprBoolean = xpath.compile("/root/customer/name/text()='George'");

  /* new edit */
  XPathExpression expr = xpath.compile("/root/customer[name = 'George']");
  /* new edit */

  /*Boolean test = (Boolean) exprBoolean.evaluate(doc, XPathConstants.BOOLEAN); */

  /* new edit */
  NodeList customers = (NodeList)expr.evaluate(doc, XPathConstants.NODESET); 
  /* new edit */


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

        //Output Names
        System.out.println("Name of Customer : " + customers.item(i).getNodeValue()); 
        //GET null!!! in here!!!

        XPathExpression expr1 = xpath.compile("../../address/text()");
        Object result1 = expr1.evaluate(customers.item(i),XPathConstants.NODESET);
        NodeList address = (NodeList) result1;

        for(int z = 0; z < address.getLength(); z++) {
            System.out.println("---- Address ---" + address.item(z).getNodeValue());
        }
  }


    }
}

root.xml 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <customer>
        <name>George</name>
        <address>NY</address>
        <telephones>
            <telephone>
                <number>1</number>
                <number>2</number>
            </telephone>
        </telephones>
    </customer>     

    <customer>
        <name>George2</name>
        <address>NY2</address>
        <telephones>
            <telephone>
                <number>12</number>
                <number>22</number>
            </telephone>
        </telephones>
    </customer>     
</root>
4

1 回答 1

1

好吧,您应该编写一个 XPath 表达式来选择您感兴趣的节点,例如/root/customer[name = 'George'],然后您可以选择它们并进一步处理它们。根据您使用的 API,如果不存在此类节点,则路径将返回空 NodeList。

这是一个例子:

  NodeList customers = (NodeList)expr.evaluate(doc, XPathConstants.NODESET); 


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


        XPathExpression expr1 = xpath.compile("address");
        Node address = (Node)expr1.evaluate(customers.item(i),XPathConstants.NODE);
        System.out.println(address.getTextContent());


  }
于 2013-09-21T09:07:28.913 回答