在XML文件中搜索元素值

时间:2013-06-17 02:46:05

标签: java xml xpath

在给定的XML文件中,我试图在Java中使用XPath搜索字符串的存在。然而,即使字符串在那里,我的输出也总是作为号码。希望有人可以指出我可能做错了什么?

XML文件:

<article>
 <body>
  <section>
    <h1>intro1</h1>
    <region>introd1</region>
    <region>introd2</region>
  </section>
  <section>
    <h1 class="pass">1 task objectives</h1>
    <region>object1</region>
    <region>object2</region>
  </section>
  <section>
     <h1 class="pass">1 task objectives</h1>
     <region>object1</region>
     <region>This is the Perfect Word I am looking for</region>
  </section>
 </body>
</article>

在Java中,我试图像这样检查单词"perfect"的存在:

expr = xpath.compile("//article//body//section//region[contains(.,'perfect')]");
object result = expr.evaluate(doc,XPathConstants.NODESET);
NodeList nodes = (NodeList)result;

if (nodes.getLength() > 0) { 
    System.out.println("Found");
    // do other stuff here
} else {
    System.out.println("Not found");
}

当我运行它时,输出始终为"Not Found"。知道我做错了吗?

2 个答案:

答案 0 :(得分:3)

我测试了这个...

expr =xpath.compile("/article/body/section/region[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'perfect')]");

<article>
    <body>
        <section>
            <h1>intro1</h1>
            <region>perfect</region>
            <region>Perfect</region>
        </section>
        <section>
            <h1 class="pass">1 task objectives</h1>
            <region>pErFeCt</region>
            <region>Not Perfect</region>
        </section>
        <section>
            <h1 class="pass">1 task objectives</h1>
            <region>object1</region>
            <region>This is the Perfect Word I am looking for</region>
        </section>
    </body>
</article>

使用...

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TestXML05 {

    public static void main(String[] args) {
        try {

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            Document doc = factory.newDocumentBuilder().parse(new File("Sample.xml"));

            XPathFactory xFactory = XPathFactory.newInstance();
            XPath xPath = xFactory.newXPath();
            XPathExpression exp = xPath.compile("/article/body/section/region[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'perfect')]");

            NodeList nl = (NodeList)exp.evaluate(doc.getFirstChild(), XPathConstants.NODESET);
            for (int index = 0; index < nl.getLength(); index++) {

                Node node = nl.item(index);
                System.out.println(node.getTextContent());

            }


        } catch (Exception ex) {
            Logger.getLogger(TestXML05.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

哪个输出......

perfect
Perfect
pErFeCt
Not Perfect
This is the Perfect Word I am looking for

答案 1 :(得分:2)

XML / XPath区分大小写,您的XPath应该是

//article//body//section//region[contains(., 'Perfect')]

要使用不区分大小写,请使用此

//article//body//section//region[
    contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
    'perfect')
]