2

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>
1
  • What I have to do to show this (data have got "count" and "person") (count = 1) (person have got "name" and "cars") (name = "Adam") (cars have got "Porshe" and "minimini") Commented Nov 4, 2012 at 15:02

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Use this it's cleaner and more in context than mine.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.