reading xml with android
I need to build an app on android which read data from a windows based software built in vb.
How would I do this?. Is using XML is good option. If yes then 开发者_JS百科How to read xml with android?
You can use xml, json or a number of other formats.
I found using XmlPullParser to be easy for parsing xml. Included in that link is sample code.
I also had a good experience using gson to parse json.
If you know how to parse XML in any other language just go from there. You should decide on SAX/DOM. There are various frameworks for parsing XML depending on your specific needs. Have a look around and pick the one that fits best.
(Any Java XML Framework will do)
Your best option parsing XML on an Android device is using Simple Framework. It uses annotations to do the mapping XML <--> Object. Most of the XML frameworks are not Android friendly (like JAXB or XMLBeans)
You can find a good example here: http://www.javacodegeeks.com/2011/02/android-xml-binding-simple-tutorial.html
I parse my xml streams with the DocumentBuilderFactory
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(inputstream);
your can read the xml like:
org.w3c.dom.Node parent_node = doc.getElementsByTagName("TagName").item(0);
NodeList nl = parent_node.getChildNodes();
for(int i = 0;i<nl.getLength();i++){
String path = nl.item(i).getFirstChild().getFirstChild().getNodeValue();
String [] pairs = path.split(" ");
....
As hut suggested the Absolute best way to work with XML in Android is to use the Simple XML Library. You can take a look at my tutorial for including Simple in your Android project: its on my blog.
精彩评论