使用XPath Java在Tag内部读取

时间:2014-03-13 08:05:28

标签: java xml xpath

Hye我是使用Java读取XML文件的新手我的问题是我一直在尝试读取xml并且在特定标记之间我想获取所需的数据我正在使用XPath并且我的查询是:

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

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

   <ATTRIBUTE name="Description" type="STRING"> SOME TEXT </ATTRIBUTE>

但我想只读取这些类型的标签内的数据,以便我的输出应该是:

  SOME TEXT

在标签内! 有人可以帮我,我怎么能这样做请xml阅读新手!尽我所能:

  String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name='Description' and ./type/text()='STRING']";

但它不会给我任何输出! 提前谢谢

我的代码:

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

我的代码存在问题,无法弄清楚是什么!

2 个答案:

答案 0 :(得分:0)

此代码适用于我,更改后的XPath为:

"/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name='Description'][@type='STRING']"

private static final String EXAMPLE_XML =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
        "<ADOXML adoversion=\"Version 5.1\" username=\"kvarga\" database=\"adonisdb\" time=\"08:55\"   date=\"30.11.2013\" version=\"3.1\">" +
            "<MODELS>" +
                "<MODEL version=\"\" applib=\"ADONIS BPMS BP Library 5.1\" libtype=\"bp\" modeltype=\"Business process model\" name=\"Product development\" id=\"mod.25602\">" +
                    "<MODELATTRIBUTES>" +
                        "<ATTRIBUTE name=\"Version number\" type=\"STRING\"> </ATTRIBUTE>" +
                        "<ATTRIBUTE name=\"Author\" type=\"STRING\">kvarga</ATTRIBUTE>" +
                        "<ATTRIBUTE name=\"Description\" type=\"STRING\">I WANT THIS PARA 2</ATTRIBUTE>" +
                    "</MODELATTRIBUTES>" +
                "</MODEL>" +
            "</MODELS>" +
        "</ADOXML>";

public static void main(String[] args) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = builderFactory.newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(EXAMPLE_XML.getBytes()));
        XPath xPath =  XPathFactory.newInstance().newXPath();
        String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name='Description'][@type='STRING']";
        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 (Exception e) {
        System.out.print(e);
    }
}

输出:

###I WANT THIS PARA 2###

答案 1 :(得分:0)

上述代码工作正常。

您也可以尝试其他方式来获取文本节点 -

String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE/text()";

NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);

System.out.println(nodeList.item(0).getNodeValue());