Is there an easier way to parse XML in Java?

前端 未结 13 506
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 02:50

I\'m trying to figure out how to parse some XML (for an Android app), and it seems pretty ridiculous how difficult it is to do in Java. It seems like it requires creating an

相关标签:
13条回答
  • 2020-11-30 03:08

    Try http://simple.sourceforge.net, its an XML to Java serialization and binding framework, its fully compatible with Android and is very lightweight, 270K and no dependencies.

    0 讨论(0)
  • 2020-11-30 03:08

    Acording to me, you should use SAX parser because: - Fast - you can control everything in XML document

    You will pay more time to coding, but it's once because you will create code template to parse XML

    From second case, you only edit content of changes.

    Good luck!

    0 讨论(0)
  • 2020-11-30 03:08

    In my opinion, using XPath for parsing XML may be your easiest coding approach. You can embody the logic for pulling out nodes from an XML document in a single expression, rather than having to write the code to traverse the document's object graph.

    I note that another posted answer to this question already suggested using XPath. But not yet for your Android project. As of right now, the XPath parsing class is not yet supported in any Android release (even though the javax.xml namespace is defined in the Dalvik JVM, which could fool you, as it did me at first).

    Inclusion of XPath class in Android is a current work item in late phase. (It is being tested and debugged by Google as I write this). You can track the status of adding XPath to Davlik here: http://code.google.com/p/android/issues/detail?id=515

    (It's an annoyance that you cannot assume things supported in most Java VMs are included yet in the Android Dalvik VM.)

    Another option, while waiting for official Google support, is JDOM, which presently claims Dalvik VM compatibility and also XPath support (in beta). (I have not checked this out; I'm just repeating current claims from their web site.)

    0 讨论(0)
  • 2020-11-30 03:10

    There is a very good example shows for XmlPullParser for any type of xml. It could also parse as a generic way, you do not need to change any thing for that just get that class and put into your android project.

    Generic XmlPullParser

    0 讨论(0)
  • 2020-11-30 03:11

    I've created a really simple API to solve precisely this problem. It's just a single class that you can include in your code base and it's really clean and easy to parse any XML. You can find it here:

    http://argonrain.wordpress.com/2009/10/27/000/

    0 讨论(0)
  • 2020-11-30 03:16

    There are two different types of processors for XML in Java (3 actually, but one is weird). What you have is a SAX parser and what you want is a DOM parser. Take a look at http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ for how to use the DOM parser. DOM will create a tree which you can navigate pretty easily. SAX is best for large documents but DOM is much easier if slower and much more memory intensive.

    0 讨论(0)
提交回复
热议问题