Xpath Java通过测试值获取元素 [英] Xpath Java get element by testing value

查看:134
本文介绍了Xpath Java通过测试值获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Xpath来解析一个xml字符串,但我不知道如何使用表达式来检索我想要的。



我有以下xml内容:

 < root1> 
< root2>
< A>某事< / A>
< B>某事< / B>
< C>
&D
< E> DataE< / E>
< F> DataF< / F>
< G>某事< / G>
< H>某事< / H>
< I>
< J
< K> DataK< / K>
< L> DataL< / L>
< / J>
< / I>
< / D>
&D
< E> DataE_ERROR< / E>
< F> DataF< / F>
< G>某事< / G>
< H>某事< / H>
< I>
< J
< K> DataK< / K>
< L> DataL< / L>
< / J>
< / I>
< / D>
< / C>
< / root2>
< / root1>

我想获取F,K,L的值,当E的值为DataE for



我正在使用这个Xpath,但是找不到好的表情!



here是我的java代码:

  public void getit()
{
String xml =上面的xml ;

DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
文档d = b.parse(new InputSource(new StringReader(xml)));
d.getDocumentElement()。normalize();

//不知道!如何通过测试E
的值来使其工作String expression =// E / text()| // F / text()| // K / text()| // L / text( );

XPath xPath = XPathFactory.newInstance()。newXPath();
Object result = xPath.compile(expression).evaluate(d,XPathConstants.NODESET);

NodeList nodes =(NodeList)result; (int i = 0; i< nodes.getLength(); i ++){
System.out.println(nodes.item(i).getNodeValue());

}

}


解决方案

只是这样我可以确保查询正常工作,我修改了如下的示例XML ...

 < root1> ; 
< root2>
< A>某事< / A>
< B>某事< / B>
< C>
&D
< E> DataE< / E>
< F> DataF 1< / F>
< G>某事< / G>
< H>某事< / H>
< I>
< J
< K> DataK 1< / K>
< L> DataL 1< / L>
< / J>
< / I>
< / D>
&D
< E> DataE_ERROR< / E>
< F> DataF 2< / F>
< G>某事< / G>
< H>某事< / H>
< I>
< J
< K> DataK 2< / K>
< L> DataL 2< / L>
< / J>
< / I>
< / D>
< / C>
< / root2>
< / root1>

魔术在这里...

  String expression =// F [ancestor :: D / E [text()='DataE']] | // K [ancestor :: D / E [text()= 'DataE']] | // L [ancestor :: D / E [tex 

基本上这个读取...



查找任何 F 节点,谁有一个祖先包含 D E 的孩子,其文本等于 DataE 或...

现在,这很重要,您可以使用 ../ 来查找父节点,但 K L 被埋在子节点中,我不知道它们是否可能更深或更浅,所以我去了这个方法。



您可能需要对其进行细化,但我将关系 D / E 重要。



有了这个(和下面的例子),我能够生成以下输出...

 找到DataF 1 
找到DataK 1
找到DataL 1

可运行示例:

  public class TestXPath {

public static void main(String [] args){
String xml =上面的xml;

try {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
文档d = b.parse(new File(Values.xml));
d.getDocumentElement()。normalize();

String expression =// F [ancestor :: D / E [text()='DataE']] | // K [ancestor :: D / E [text()=' ']] | // L [祖先:: D / E [文本()=' DataE']];
XPath xPath = XPathFactory.newInstance()。newXPath();
Object result = xPath.compile(expression).evaluate(d,XPathConstants.NODESET);

NodeList nodes =(NodeList)result; (int i = 0; i< nodes.getLength(); i ++){

Node node = nodes.item(i);

System.out.println(Found+ node.getTextContent());
}
} catch(ParserConfigurationException | SAXException | IOException | XPathExpressionException | DOMException exp){
exp.printStackTrace();
}
}

}


I'm trying to use Xpath to parse an xml string, but i don't know how use the expression to retrieve what i want.

I have the following xml content :

<root1>
    <root2>
        <A>something</A>
        <B>something</B>
        <C>
            <D>
                <E>DataE</E>
                <F>DataF</F>
                <G>something</G>
                <H>something</H>
                <I>
                    <J>
                      <K>DataK</K>
                      <L>DataL</L>
                    </J>
                </I>
            </D>
            <D>
                <E>DataE_ERROR</E>
                <F>DataF</F>
                <G>something</G>
                <H>something</H>
                <I>
                    <J>
                      <K>DataK</K>
                      <L>DataL</L>
                    </J>
                </I>
            </D>
        </C>
     </root2>
</root1>

I would like to get the value of F,K,L when the value of E is DataE for example.

I'm using for this Xpath but i can't find the good expression !

here is my java code :

public void getit()
{
    String xml = "The xml above";

    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    DocumentBuilder b = f.newDocumentBuilder();
    Document d = b.parse(new InputSource(new StringReader(xml)));
    d.getDocumentElement().normalize();

    // No idea ! how can i make it work by testing the value of E
    String expression = "//E/text()|//F/text()|//K/text()|//L/text()";

    XPath xPath = XPathFactory.newInstance().newXPath();
    Object result = xPath.compile(expression).evaluate(d, XPathConstants.NODESET);

    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue()); 
    }

}

解决方案

Just so I could ensure the query was working, I modified the example XML as follows...

<root1>
    <root2>
        <A>something</A>
        <B>something</B>
        <C>
            <D>
                <E>DataE</E>
                <F>DataF 1</F>
                <G>something</G>
                <H>something</H>
                <I>
                    <J>
                        <K>DataK 1</K>
                        <L>DataL 1</L>
                    </J>
                </I>
            </D>
            <D>
                <E>DataE_ERROR</E>
                <F>DataF 2</F>
                <G>something</G>
                <H>something</H>
                <I>
                    <J>
                        <K>DataK 2</K>
                        <L>DataL 2</L>
                    </J>
                </I>
            </D>
        </C>
    </root2>
</root1>

The magic is here...

String expression = "//F[ancestor::D/E[text()='DataE']]|//K[ancestor::D/E[text()='DataE']]|//L[ancestor::D/E[tex

Basically, this reads...

Find any F node, who has an ancestor that contains D with a child of E whose text is equal to DataE or ...

Now, this is important, you could use ../ to find the parent node, but K and L are buried in sub nodes and I'm not sure if they might be deeper or shallow, so I went for this method.

You might need to refine it a bit, but I took the relationship of D/E as been important.

With this (and the example below), I was able to generate the following output...

Found DataF 1
Found DataK 1
Found DataL 1

Runnable example:

public class TestXPath {

    public static void main(String[] args) {
        String xml = "The xml above";

        try {
            DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
            DocumentBuilder b = f.newDocumentBuilder();
            Document d = b.parse(new File("Values.xml"));
            d.getDocumentElement().normalize();

            String expression = "//F[ancestor::D/E[text()='DataE']]|//K[ancestor::D/E[text()='DataE']]|//L[ancestor::D/E[text()='DataE']]";
            XPath xPath = XPathFactory.newInstance().newXPath();
            Object result = xPath.compile(expression).evaluate(d, XPathConstants.NODESET);

            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {

                Node node = nodes.item(i);
                System.out.println("Found " + node.getTextContent());
            }
        } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException | DOMException exp) {
            exp.printStackTrace();
        }
    }

}

这篇关于Xpath Java通过测试值获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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