Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XPathJava通过测试值获取元素_Java_Xml_Xpath_Expression - Fatal编程技术网

XPathJava通过测试值获取元素

XPathJava通过测试值获取元素,java,xml,xpath,expression,Java,Xml,Xpath,Expression,我试图使用Xpath解析xml字符串,但我不知道如何使用表达式检索我想要的内容 我有以下xml内容: <root1> <root2> <A>something</A> <B>something</B> <C> <D> <E>DataE</E>

我试图使用Xpath解析xml字符串,但我不知道如何使用表达式检索我想要的内容

我有以下xml内容:

<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>

某物
某物
达泰
达塔夫
某物
某物
达塔克
数据
数据错误
达塔夫
某物
某物
达塔克
数据
例如,当E的值是DataE时,我想得到F,K,L的值

我正在使用Xpath,但找不到好的表达式

以下是我的java代码:

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

}
public void getit()
{
String xml=“上面的xml”;
DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();
DocumentBuilder b=f.newDocumentBuilder();
文档d=b.parse(新的InputSource(新的StringReader(xml));
d、 getDocumentElement().normalize();
//不知道!我怎样才能通过测试E的值来让它工作呢
字符串表达式=“//E/text()|//F/text()|//K/text()|//L/text()”;
XPath=XPathFactory.newInstance().newXPath();
Object result=xPath.compile(expression).evaluate(d,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
对于(int i=0;i
为了确保查询正常工作,我修改了示例XML,如下所示

<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>
基本上,这是

查找任何
F
节点,其祖先包含
D
,其子节点
E
的文本等于
DataE

现在,这一点很重要,您可以使用
。/
查找父节点,但是
K
L
隐藏在子节点中,我不确定它们是更深还是浅,所以我选择了这种方法

您可能需要对其进行一些改进,但我认为
D/E
的关系非常重要

通过这个(和下面的例子),我能够生成以下输出

Found DataF 1
Found DataK 1
Found DataL 1
可运行示例:

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

}
公共类TestXPath{
公共静态void main(字符串[]args){
String xml=“上面的xml”;
试一试{
DocumentBuilderFactory f=DocumentBuilderFactory.newInstance();
DocumentBuilder b=f.newDocumentBuilder();
文档d=b.parse(新文件(“Values.xml”);
d、 getDocumentElement().normalize();
字符串表达式=“//F[祖先::D/E[text()='DataE']]|//K[祖先::D/E[text()='DataE']]|//L[祖先::D/E[text()='DataE']]”;
XPath=XPathFactory.newInstance().newXPath();
Object result=xPath.compile(expression).evaluate(d,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
对于(int i=0;i
这只是一个小问题,谢谢,如果我想通过示例在J和K之间添加另一个关系(仅当K=dataK时才显示J),我该怎么做呢?首先让该查询起作用……我怀疑类似于
//J[祖先::K[text()='dataK']]
的东西可能会起作用……对不起。。我指的是K和L之间的关系。(仅当K=dataK时才显示L)我尝试了以下方法:
//F[祖先::D/E[text()='DataE']]|//L[祖先::J/K[text()='dataK']]
。。但是缺少“E”验证我能做的最好的事情就是给你父节点,
//D[E[text()='DataE']和后代::K[text()='DataK']]
,然后你可以使用这个和另一个xPath查询来找到子节点……或者你可以尝试
//F[祖先::D/E[text()='DataE']和祖先::D/后代::K[text()='DataK'].//L[祖先::D/E[text()='DataE']和祖先::D/genderant::K[text()='DataK']]
-满嘴都是:P
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();
        }
    }

}