在XML文件中搜索元素值 [英] Search for element value in an XML file

查看:105
本文介绍了在XML文件中搜索元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a given XML file, I'm trying to search for the presence of a string using XPath in Java. However, even though the string is there, my output is always coming up as a No. Hoping someone here can point out what I may be doing wrong?

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中,我正在尝试检查单词的存在完美喜欢这样:

Within Java, I'm trying to check for the presence of the word "perfect" like this:

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");
}

运行时,输出始终为找不到。知道我做错了吗?

When I run this, the output is always "Not Found". Any idea what I am doing wrong?

推荐答案

我测试了这个......

I tested this...

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

这篇关于在XML文件中搜索元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆