使用具有多个条件Java的XPath在特定标记内读取

 - - - -   kvarga 2013-11-30, 08:50 2013-11-30, 08:54:46 - NODE x:6.5cm y:10.5cm index:7   I WANT THIS PARA 1   kvarga 2013-11-30, 08:50 2013-11-30, 08:54:46 - NODE x:6.5cm y:10.5cm index:7 I WANT THIS PARA 2     

Hye我想读取这个xml文件,需要获取标签内的文本,如下所示:

   

我一直在尝试使用我的代码获得结果:

  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); org.w3c.dom.Document document = builder.parse( new FileInputStream("c:\\y.xml")); XPath xPath = XPathFactory.newInstance().newXPath(); String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name='Description'and @type='STRING']"; System.out.println(expression); NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); } } catch (ParserConfigurationException | SAXException | IOException e) { System.out.print(e); } 

我的代码有问题无法弄清楚是什么!

如果我使用XPath表达式,我的代码工作正常:

 String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@type='STRING']"; 

它工作正常但我的特定标签是:

   I WANT THIS PARA 1  

所以输出应该是:

  I WANT THIS PARA 1 I WANT THIS PARA 2 

提前致谢

尝试使用此表达式为您的XPath。

 String expression = "//ADOXML//MODELS//MODEL//MODELATTRIBUTES//ATTRIBUTE[@name='Description' and @type='STRING'] | //ADOXML//MODELS//MODEL//MODELATTRIBUTES//INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']"; 

它显示两个Attributes标签,其中name =’Description’1)直接在MODELATTRIBUTES标签下访问,2)在INSTANCE标签下

我认为你所拥有的很好,你只需要在表达式的末尾添加/text()以从元素中获取文本节点。

此外,当您直接访问文本节点时,您不再需要在节点上调用getFirstChild ,如果有多个文本节点,这可能会有问题!

在查找属性值时,您也可以使用eq而不是= ,因为它们在您的示例中始终是primefaces的。 我也认为虽然这两种forms在语义上是等价的,但在语法上看起来更好的是背靠背使用两个谓词而不是(至少对我来说;-))

 String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name eq 'Description'][@type eq 'STRING']/text()"; System.out.println(expression); NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { System.out.println(nodeList.item(i).getNodeValue()); } 

根据我的Given XML文件,我的XPath表达式应该是:

 String expression = "/ADOXML/MODELS/MODEL/INSTANCE/ATTRIBUTE[@name='Description' and @type='STRING']"; 

它的作品是宾果游戏!

试试这个:

 List list = new ArrayList<>(); try { XPathExpression expr = xpath.compile(//ATTRIBUTE[@name='description']/text()"); NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) list.add(nodes.item(i).getNodeValue()); } catch (XPathExpressionException e) { e.printStackTrace(); } return list; 

更多示例xpath与java:

 public static String obtenirTextNodoMultiple(Document doc, String nodo) { String resultat = ""; int i = 0; boolean trobat = true; try { if (doc != null) while ((resultat.equals("")) && (trobat)) { String valor = doc.getElementsByTagName(nodo).item(i).getTextContent(); if (valor != "") { trobat = false; if (!trobat) resultat = valor; } i++; } } catch (Exception e) { e.printStackTrace(); } return resultat; } public static String recValorFiltreFill(String pare*father*, String nodeComparar*node to compare*, String valorAComparar*value to compare*, Document doc, String nodo*node*) { String resultat = ""; try { XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression expr = null; if (doc != null) { if ((pare.equals("")) && (valorAComparar.equals(""))) { expr = xpath.compile("//" + nodo); } else if (valorAComparar.equals("")) { expr = xpath.compile("//" + pare + "/" + nodo); } else { expr = xpath.compile("//" + pare + "[" + nodeComparar + "='" + valorAComparar + "']/" + nodo); } resultat = (String)expr.evaluate(doc, XPathConstants.STRING); } } catch (Exception e) { e.printStackTrace(); } return resultat; }