Now my software will show me (Root element: data) from xml file, but I need list of nodes. This what Xml file have got for example:
(data have got "count" and "person")
(count = 1)
(person have got "name" and "cars")
(name = "Adam")
(cars have got "Porshe" and "minimini")
Softweare
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public void Ada()
{
File fXmlFile = new File("c:\\file.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());
}
XML File:
<data>
<count>1</count>
<person>
<name>Adam<name>
<cars>
<fast>Porshe</fast>
<slow>MiniMini</slow>
</cars>
</person>
</data>
You can use:
doc.getChildNodes()
to get deeper and the recursively drill dow until there are no children left.
It will return a NodeList
you can the use the size()
to 'power' a for loop.
Take a look at XPath: http://developer.android.com/reference/javax/xml/xpath/package-summary.html
You can basically do anything you need with XML on Android using that.
So something like:
XPath x = XPathFactory.newInstance().newXPath();
String expression = "/data";
InputSource source = new InputSource("someXML.xml");
NodeSet nodes = (NodeSet)xpath.evaluate(expression, source, XPathConstants.NODESET);
That should give you a NodeSet of the nodes.
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.