I have an xml which looks like this:
{ <xml><ep><source type="xml">...</source><source type="text">..</source></ep></xml>}
Here I want to retrieve the value of "source type" where type is an attribute.
I tried the following, which didn't work:
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
try {
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse("D:/workspace1/ereader/src/main/webapp/configurations/config.xml");
    System.out.println(dDoc);
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node node = (Node) xPath.evaluate("//xml/source/@type/text()", dDoc, XPathConstants.NODE);
    System.out.println(node);
} catch (Exception e) {
    e.printStackTrace();
This also didn't work:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader("config.xml"));
Document doc = builder.parse(is);
NodeList nodeList = doc.getElementsByTagName("source");
for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    if (node.hasAttributes()) {
        Attr attr = (Attr) node.getAttributes().getNamedItem("type");
        if (attr != null) {
            String attribute = attr.getValue();
            System.out.println("attribute: " + attribute);
        }
    }
}
 
     
     
     
     
     
     
     
    