从特定树级获取XML节点

时间:2011-03-03 17:43:18

标签: java xml xpath

我需要一个类似Document.getElementsByTagName()的方法,但是只搜索某个级别的标记(即不是具有相同名称的嵌套标记)

示例文件:

<script>
  <something>
    <findme></findme><!-- DO NOT FIND THIS TAG -->
  </something>
  <findme></findme><!-- FIND THIS TAG -->
</script>

Document.getElementsByTagName()只返回文档中的所有findme标记。

3 个答案:

答案 0 :(得分:5)

以下是XPath的示例

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class TestXPath {

    private static final String FILE = "a.xml" ;
    private static final String XPATH = "/script/findme";
    public static void main(String[] args) {

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
        DocumentBuilder builder;
        try {
            builder = docFactory.newDocumentBuilder();
            Document doc = builder.parse(FILE);
            XPathExpression expr = XPathFactory.newInstance().newXPath().compile(XPATH);
            Object hits = expr.evaluate(doc, XPathConstants.NODESET ) ;
            if ( hits instanceof NodeList ) {
                NodeList list = (NodeList) hits ;
                for (int i = 0; i < list.getLength(); i++ ) {
                    System.out.println( list.item(i).getTextContent() );
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

<script>
  <something>
    <findme>1</findme><!-- DO NOT FIND THIS TAG -->
  </something>
  <findme>Find this</findme><!-- FIND THIS TAG -->
  <findme>More of this</findme><!-- FIND THIS TAG AS WELL -->
</script>

产生

Find this
More of this

答案 1 :(得分:1)

如果你正在使用DOM,我能想到的唯一方法就是一个递归函数,它会查看每个元素的子元素。

答案 2 :(得分:0)

使用:

/*/findme

此XPath表达式选择所有findme个元素,这些元素是XML文档顶部元素的子元素。

此表达式

//findme[count(ancestor::*) = 5]

选择XML文档中具有五个祖先元素的所有findme元素 - 即它们都处于“6级”。