Wednesday, October 15, 2014

Can't retrieve ID's
































































































































































































































































































































































































































































































































If my xml file (shown below as sample) has different employees with id's - is there a way I can get just the id's on there own?
































































































































































































































































































































































































































































































































































































































































































































































































Java Code:









































































































































































































































































































































































































































































































































<Employees>
<Employee id="1">
<age>29</age>
<name>Pankaj</name>
<gender>Male</gender>
<role>Java Developer</role>
</Employee>
<Employee id="2">
<age>35</age>
<name>Lisa</name>
<gender>Female</gender>
<role>CEO</role>
</Employee>
</Employees>







































































































































































































































































































































































































































































































































This is the code Im using - I know at the minute it gets the id details of id 3 but realistically I just want the id's and nothing else































































































































































































































































































































































































































































































































































































































































































































































































Java Code:









































































































































































































































































































































































































































































































































private static String getEmployeeById(Document doc, XPath xpath, int id) {
String env = null;
try {
XPathExpression pathExpression =
xpath.compile("/Employees/Employee[@id=3]"); // line gets everything to do with id 3 but doesnt display the id - I need it to display all the id's
env = (String) pathExpression.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException e) {
e.printStackTrace();
}

return env;
}







































































































































































































































































































































































































































































































































Can this be achieved using xpath? I've been stuck on it for ages and I can't seem to really find anything to do with it






























































































































































































































































































































































































































































































































































































































































































































































































No comments:

Post a Comment