Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Getting started example:</p> <p>xml file:</p> <pre><code>&lt;inventory&gt; &lt;book year="2000"&gt; &lt;title&gt;Snow Crash&lt;/title&gt; &lt;author&gt;Neal Stephenson&lt;/author&gt; &lt;publisher&gt;Spectra&lt;/publisher&gt; &lt;isbn&gt;0553380958&lt;/isbn&gt; &lt;price&gt;14.95&lt;/price&gt; &lt;/book&gt; &lt;book year="2005"&gt; &lt;title&gt;Burning Tower&lt;/title&gt; &lt;author&gt;Larry Niven&lt;/author&gt; &lt;author&gt;Jerry Pournelle&lt;/author&gt; &lt;publisher&gt;Pocket&lt;/publisher&gt; &lt;isbn&gt;0743416910&lt;/isbn&gt; &lt;price&gt;5.99&lt;/price&gt; &lt;/book&gt; &lt;book year="1995"&gt; &lt;title&gt;Zodiac&lt;/title&gt; &lt;author&gt;Neal Stephenson&lt;/author&gt; &lt;publisher&gt;Spectra&lt;/publisher&gt; &lt;isbn&gt;0553573862&lt;/isbn&gt; &lt;price&gt;7.50&lt;/price&gt; &lt;/book&gt; &lt;!-- more books... --&gt; &lt;/inventory&gt; </code></pre> <p>Java code:</p> <pre><code>import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File("c:\\tmp\\my.xml")); // normalize text representation doc.getDocumentElement().normalize(); System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); NodeList listOfBooks = doc.getElementsByTagName("book"); int totalBooks = listOfBooks.getLength(); System.out.println("Total no of books : " + totalBooks); for(int i=0; i&lt;listOfBooks.getLength() ; i++) { Node firstBookNode = listOfBooks.item(i); if(firstBookNode.getNodeType() == Node.ELEMENT_NODE) { Element firstElement = (Element)firstBookNode; System.out.println("Year :"+firstElement.getAttribute("year")); //------- NodeList firstNameList = firstElement.getElementsByTagName("title"); Element firstNameElement = (Element)firstNameList.item(0); NodeList textFNList = firstNameElement.getChildNodes(); System.out.println("title : " + ((Node)textFNList.item(0)).getNodeValue().trim()); } }//end of for loop with s var } catch (SAXParseException err) { System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.out.println(" " + err.getMessage ()); } catch (SAXException e) { Exception x = e.getException (); ((x == null) ? e : x).printStackTrace (); } catch (Throwable t) { t.printStackTrace (); } </code></pre>
 

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