Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to get the attribute value of an xml node using java
    text
    copied!<p>I've an xml which looks like this:</p> <pre><code>{ &lt;xml&gt;&lt;ep&gt;&lt;source type="xml"&gt;...&lt;/source&gt;&lt;source type="text"&gt;..&lt;/source&gt;&lt;/ep&gt;&lt;/xml&gt;} </code></pre> <p>here i wanna retrieve the value of "source type" where type s an attribute.</p> <p>I 'd tried like this,But its not working:</p> <pre><code> 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(); </code></pre> <p>i've tried this too :</p> <pre><code>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 &lt; 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); } } } </code></pre> <p>pls help me!!</p> <p>Thanks in advance, Varsha.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload