Following is the XML structure –
<Projects>
<Project>
<Name>Web Site Designing</Name>
<Members>
<MName>Jack</MName>
<MName>John</MName>
<MName>Jill</MName>
</Members>
</Project>
<Project>
<Name>Site Ranking</Name>
<Members>
<MName>Jack</MName>
<MName>James</MName>
</Members>
</Project>
<Project>
<Name>E-Mailing</Name>
<Members>
<MName>Matt</MName>
</Members>
</Project>
<Projects>
What will be the XPath Query to get the following output in Java –
Web Site Designing,Jack,John,Jill
Site Ranking,Jack,James
E-Mailing,Matt
Right now the Java Code I am using is not doing the same. Pls find the java code below –
.......
.......
.......
String pName = "";
String mName = "";
Document doc = builder.parse("Project.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//Projects/Project");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
BufferedWriter out = new BufferedWriter(new FileWriter("projInfo.txt"));
Node node;
for (int i = 0; i < nodes.getLength(); i++)
{
node = nodes.item(i);
pName = xpath.evaluate("Name",node);
mName = xpath.evaluate("Members/MName",node);
//Here's something worng
out.write(pName + "," + mName + "\r\n");
}
out.close();
.......
Have a nice day, JB
This returns a node-list, but in the next code line:
you are treating it as a single string.
It would be correct to process all nodes contained in
mNameiterating throughmName.