Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a sample code, it's a simple java project, not android, but it works for me!</p> <pre><code>import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class test { public static void main(String argv[]) { try { File fXmlFile = new File("D:/Xml.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("item"); System.out.println("----------------------------"); for (int temp = 0; temp &lt; nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (eElement.getElementsByTagName("name").item(0) .getTextContent().equals("Margherita")) { System.out.println("Id: " + eElement.getElementsByTagName("id").item(0) .getTextContent()); System.out.println("It costs: " + eElement.getElementsByTagName("cost").item(0) .getTextContent()); System.out.println(eElement .getElementsByTagName("name").item(0) .getTextContent()); System.out.println("Description:" + eElement.getElementsByTagName("description") .item(0).getTextContent()); System.out.println(); } } } } catch (Exception e) { e.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