找不到属性时返回 null 的 JAVA Xpath 表达式

标签 java xml xpath

在下面的 xml 示例中

      <employee>
        <payment contractType="1">
          <income type="0">
            <gr amount="2063.00" kae="211" code="1" />
            <gr amount="400.00" kae="215" code="6" />
            <et amount="47.55" kae="292" code="4012501" />
            <et amount="105.21" kae="293" code="4052000" />
            <de amount="88.15" code="4003101" />
          </income>
         </payment>
      </employee>
      <employee>
        <payment contractType="1">
          <income type="0">
            <gr amount="70.00" kae="213" code="4" />
            <gr amount="1560.00" kae="211" code="1" />
          </income>
       </payment>
    </employee>

我需要获取“code”=“4”的“amount”值。如果收入节点不包含此类数据(gr with code = "4"),我需要返回类似 null 或 boolean false 的内容。目的是查看 xml 文件中的所有员工,如果他们没有任何金额,代码为 4,则将他们加载到带有 0 的 Arraylist 中,否则加载金额值。

我在这部分使用的代码:

    public class ReadXMLfile {
    public static void main(String[] args) {

    try {
        FileInputStream file = new FileInputStream(new File("E2015_1_1.xml"));

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder =  builderFactory.newDocumentBuilder();

        Document xmlDocument = builder.parse(file);

        XPath xPath =  XPathFactory.newInstance().newXPath();


    expression = "/psp/body/organizations/organization/employees/employee/payment/income[@type='0']/gr";
    nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
    ArrayList<String> gr4incomeList = new ArrayList<String>();

    for (int i = 0; i < nodeList.getLength(); i++) {

          String acode = (String)nodeList.item(i).getAttributes().getNamedItem("code").getNodeValue();
          System.out.println("acode = '" + acode + "'");

               if (acode.equals("4")){
                    System.out.println(nodeList.item(i).getAttributes().getNamedItem("amount").getNodeValue());
                    gr4incomeList.add(nodeList.item(i).getAttributes().getNamedItem("amount").getNodeValue());
                    System.out.println("array = " + gr4incomeList.get(i));
        }else
                    gr4incomeList.add("0000");

    }

     } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
}

问题是它在 arraylist 中为找到的任何 gr 写入“0000”,代码为“4”的除外。

我真的卡住了。

有什么想法吗?谢谢大家!

最佳答案

XPathExpression exp = xpath.compile("/employees/employee");
NodeList nodeList = (NodeList)exp.evaluate(xmlDocument, XPathConstants.NODESET);
XPathExpression grexp = xpath.compile("payment/income/gr[@code='4']");
XPathExpression amexp = xpath.compile("payment/income/gr[@code='4']/@amount");
for( int i = 0; i < nodeList.getLength(); ++i ){
    Node item = nodeList.item( i );
    Object resexp1 = grexp.evaluate( item, XPathConstants.NODE );
    if( resexp1 != null ){
        String resexp2 = amexp.evaluate( item );
        System.out.println( resexp2 );
    } else {
        System.out.println( "0000" );
    }
}

为 XML 片段生成如下所示:

0000
70.00

关于找不到属性时返回 null 的 JAVA Xpath 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28811313/

相关文章:

python - 无法在 python selenium 中使用 selenium chrome webdriver 定位元素

java - 在使用 Cucumber 和 JUnit 时,如何截取屏幕截图并将其附加到 Allure 报告中?

python - 使用 python elementtree 将 xml 解析为行项目

java - 如何在Android Studio中的EditText中填充 float 和整数

xml - 实体被引用但未声明

xpath - 通过 XPath 提取节点之间的文本

Javascript/JSON 获取给定子节点的路径?

Java应用程序: Getting input before running

Java Replaceall 忽略大小写和特殊字符

java - 如何使用 reSTLet 从 jax-rs 获取客户端的 ip