import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import javax.xml.xpath.XPathConstants;
import java.io.*;
class XML_XpathTest {
public static void main(String args[]) {
try {
XPathFactory factory = XPathFactory.newInstance( );
XPath xPath = factory.newXPath( );
String expression= "//*";
//expression= "/healthrecord/hospital/lung_disease/attribute::p_id";
//expression= "/healthrecord/hospital";
//expression= "/healthrecord";
//expression= "healthrecord/hospital";
//expression= "//hospital";
//expression= "healthrecord//hospital";
//expression= "//@p_id";
//expression= "//*";
NodeList shows = (NodeList) xPath.evaluate(expression,new InputSource(new FileReader("healthRecord.xml")),XPathConstants.NODESET);
System.out.println("Document has " + shows.getLength( ) + " nodes.");
for (int i = 0; i < shows.getLength(); i++)
{
String s1=shows.item(i).getNodeName();
System.out.println("Node Name:"+s1+" "+"\n");
String sout ="Node Name:"+s1+" "+"\n";
String s2=shows.item(i).getNodeValue();
}
}
catch(Exception e) {
System.out.println(e);
}
}
}