迭代XPath只获取表名

时间:2014-02-07 13:06:44

标签: java xpath

我想迭代XPath只获取表名。这是我的Xpath值。

PromoteData/Table[@Id='auditSet']/Table[@Id='auditSetMapping']/Table[@Id='appExpr']

请指导我如何迭代此XPath的每个值。预期的输出将类似于'auditSet','auditSetMapping','appExpr'

1 个答案:

答案 0 :(得分:1)

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class XPathExtract {

  public static void main(String[] args) throws SAXException, IOException,
      ParserConfigurationException, XPathExpressionException {
    String xml = "...";

    // Parse the XML as DOM
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
        .parse(new InputSource(new StringReader(xml)));

    // Create a XPath instance
    XPath xpath = XPathFactory.newInstance().newXPath();

    // Evaluate the XPath. The result is a NodeList
    NodeList nodes = (NodeList) xpath.evaluate(
      "PromoteData/Table[@Id='auditSet']/Table[@Id='auditSetMapping']/Table[@Id='appExpr']", 
        doc, XPathConstants.NODESET);

    // Iterate over the nodes
    for (int i = 0; i < nodes.getLength(); i++) {
      // node.item(i) is a Node. If you are sure, it is always an Element you can do a cast
      Element el = (Element) nodes.item(i);
      // Process the element
      System.out.println(el.getAttribute("Id"));
    }
  }
}