4

In the following xml sample

      <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>

i need to get the value of "amount" for "code" = "4". If the income node does not contain such data ( gr with code = "4") i need to return something like null or boolean false. Purpose is to look through all the employees in the xml file, load them on an Arraylist with 0 if they dont have any amount with code 4 else load the amount value.

code i use for this part :

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

}

Problem is it writes "0000" in the arraylist for any gr found except the ones with code = "4".

I am really stuck.

Any ideas? Thank you guys!

6
  • gr4incomeList.add("0000"); if you want to write "0", shouldn't u be using gr4incomeList.add("0"); Commented Mar 2, 2015 at 13:56
  • Can there be more than one item with code="4", or just one? Commented Mar 2, 2015 at 14:00
  • @JLRishe only one item with code="4" per employee. Thank you Commented Mar 2, 2015 at 14:06
  • @nafas i need to store it as a 4 digit string, thanks Commented Mar 2, 2015 at 14:06
  • @Bonzay it seems ur code only works for employess that have code="4" as their first <gr> tag. am I right? Commented Mar 2, 2015 at 14:08

1 Answer 1

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

produces, for the XML snippet as shown:

0000
70.00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.