1

你好我有这个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>

我想通过 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>");
                }
            }

但发生运行时错误

任何帮助都会很有用。

4

1 回答 1

2

使用这个 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);

以防万一看看这个 xpath 语法

/ 从根节点中选择
// 从当前节点中选择文档中与选择匹配的节点,无论它们在哪里
. 选择当前节点
.. 选择当前节点的父节点
@ 选择属性
于 2013-05-31T13:16:33.807 回答