Hi,
How can I get to read children of “UserInfo” node. For example I want FirstName, LastName, Age and State (parameters and their values). I’ve tried using the XPathExpression but not seeing the results I want. Please assist.
inputFile has following xml text.
<GetUserInfoResponse>
** <Return>
** <UserInfo>
** <FirstName>David</FirstName>
** <LastName>Hunter</LastName>
** <Age>45</Age>
** <State>Main</State>
** </UserInfo>
** </Return>
** </GetUserInfoResponse>"
public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException
{
File inputFile = new File(“C:\Automation\Test.txt”);
Document doc = builder.parse(inputFile);
doc.getDocumentElement().normalize();
// Creating an Xpath to access the node
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();
XPathExpression ex = xpath.compile("//GetUserInfoResponse/Return/UserInfo");
Object res = ex.evaluate(doc, XPathConstants.NODESET);
NodeList nList = (NodeList) res;
System.out.println("Length = " + nList.getLength());
for (int j = 0; j < nList.getLength(); j++)
{
System.out.println("Node Name = " +nList.item(j).getChildNodes());
}
}