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
JavaDOM搜索xml_Java_Xml_Dom - Fatal编程技术网

JavaDOM搜索xml

JavaDOM搜索xml,java,xml,dom,Java,Xml,Dom,你好,我有这个xml <?xml version="1.0" encoding="utf-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="new2.xsl"?> <patients> <patient> <stoixeia_astheni> <arithmos_eksetasis>1</arithmos_eksetas

你好,我有这个xml

<?xml version="1.0" encoding="utf-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="new2.xsl"?>
<patients>
    <patient>
    <stoixeia_astheni>
        <arithmos_eksetasis>1</arithmos_eksetasis>
        <imerominia_eksetasis>11/12/2005</imerominia_eksetasis>
        <amka>14385</amka>

    </stoixeia_astheni>
    <stoixeia_epikoinonias>
        <dieuthinsi>Μητσοπούλου 20</dieuthinsi>

    </stoixeia_epikoinonias>
    <loipa_stoixeia>
        <fylo>Aρρεν</fylo>

    </loipa_stoixeia>
    </patient>
    <patient>
    same code here
    </patient>

</patients>

1.
11/12/2005
14385
Μητσοπούλου 20
ρρεν
这里的代码相同
我想通过amka值来搜索这个。 我试过这个:

 Document doc = docBuilder.parse(filepath);
            NodeList root= doc.getDocumentElement().getChildNodes();
            for(int i=0; i<root.getLength(); i++){
                if(root.item(i).getChildNodes().item(0).getChildNodes().item(2).getNodeValue()=="14385"){
                    pw.println("Gataki<br>");
                }
            }
documentdoc=docBuilder.parse(文件路径);
NodeList root=doc.getDocumentElement().getChildNodes();
对于(int i=0;i使用此xpath

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/patients/patient/stoixeia_astheni/amka/text()");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
DocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
Document doc=builder.parse();
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathfactory.newXPath();
XPathExpression expr=xpath.compile(“/patients/patient/stoixeia_-insulfi/amka/text()”;
NodeList nl=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
以防万一,看看这个xpath语法

/ Selects from the root node // Selects nodes in the document from the current node that match the selection no matter where they are . Selects the current node .. Selects the parent of the current node @ Selects attributes /从根节点中选择 //从当前节点中选择与所选内容匹配的文档中的节点,无论这些节点位于何处 。选择当前节点 ..选择当前节点的父节点 @选择属性
使用
.equals()
而不是
=
来比较字符串。学习xpath也可能对您有所帮助。@Zutty您是对的,但这不是唯一的错误。看看这个xpath教程,我对xpath做了一些修改,让您直接获得节点值。根据导入,我认为这是您可以自己做的事情。您可以打印类似于(int i=0;i)的
节点列表