java 用于简单 xml 节点字符串的 Android XML 解析器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5412237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android XML parser for simple xml node strings
提问by mousedown
I need to parse a series of simple XML nodes (String format) as they arrive from a persistent socket connection. Is a custom Android SAX parser really the best way? It seams slightly overkill to do it in this way
我需要解析一系列从持久套接字连接到达的简单 XML 节点(字符串格式)。自定义 Android SAX 解析器真的是最好的方法吗?它接缝略有矫枉过正做在这样
I had naively hoped I could cast the strings to XML then reference the names / attributes with dot syntax or similar.
我天真地希望我可以将字符串转换为 XML,然后使用点语法或类似语法引用名称/属性。
回答by Travis Webb
I'd use the DOM Parser. It isn't as efficient as SAX, but if it's a simple XML file that's not too large, it's the easiest way to get up and moving.
我会使用 DOM 解析器。它不如 SAX 高效,但如果它是一个不太大的简单 XML 文件,它是最简单的启动和移动方式。
Great tutorial on how to use it here: http://tutorials.jenkov.com/java-xml/dom.html
关于如何在此处使用它的精彩教程:http: //tutorials.jenkov.com/java-xml/dom.html
回答by jluzwick
You might want to take a look at the XPath library. This is a more simple way of parsing xml. It's similar to building SQL queries and regex's.
您可能想查看 XPath 库。这是一种更简单的xml解析方式。它类似于构建 SQL 查询和正则表达式。
http://www.ibm.com/developerworks/library/x-javaxpathapi.html
http://www.ibm.com/developerworks/library/x-javaxpathapi.html
回答by Joseph Earl
I'd go for a SAX Parser:
我会选择 SAX 解析器:
- It's much more efficient in terms of memory, especially for larger files: you don't parse an entire document into objects, instead the parser performs a single uni-directional pass over the document and triggers events as it goes through.
- It's actually surprisingly easy to implement: for instance take a look at Working with XML on Androidby IBM. It's only listings 5 and 6 that are the actual implementation of their SAX parser so it's not a lot of code.
- 它在内存方面效率更高,尤其是对于较大的文件:您不会将整个文档解析为对象,而是解析器对文档执行单向传递并在它通过时触发事件。
- 它实际上非常容易实现:例如查看IBM 的在 Android 上使用 XML。只有清单 5 和清单 6 才是其 SAX 解析器的实际实现,因此代码并不多。
回答by Martin Vysny
You can try to use Konsume-XML: SAX/STAX/Pull APIs are too low-level and hard to use; DOM requires the XML to fit into memory and is still clunky to use. Konsume-XML is based on Pull and therefore it's extremely efficient, yet the API is higher-level and much easier to use.
您可以尝试使用Konsume-XML:SAX/STAX/Pull API 太低级且难以使用;DOM 要求 XML 适合内存,但使用起来仍然很笨拙。Konsume-XML 基于 Pull,因此非常高效,而且 API 更高级且更易于使用。