Android - 使用 SAXParser 的通用 XML 解析器
我需要编写一个可以在整个 Android 应用程序中重用的 XML 解析类,据我所知,SAXParser 对于移动应用程序来说是最好的。我正在使用本指南:
http://www.jondev.net/articles/Android_XML_SAX_Parser_Example
我希望解析的文档类型是来自 Blogger GData API 的提要 - 示例是:
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/"CUIGRnc4fyp7ImA9Wx9SEEg."'>
<id>tag:blogger.com,1999:user-464300745974.blogs</id>
<updated>2010-11-29T17:58:47.937Z</updated>
<title>Tim's Blogs</title>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/blogs'/>
<link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/blogs'/>
<link rel='alternate' type='text/html' href='http://www.blogger.com/profile/blogid'/>
<author>
<name>Tim</name>
<uri>http://www.blogger.com/profile/blogid</uri>
<email>[email protected]</email>
</author>
<generator version='7.00' uri='http://www.blogger.com'>Blogger</generator>
<openSearch:totalResults>2</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry gd:etag='W/"DUIBQHg-cCp7ImA9Wx9TF0s."'>
<id>tag:blogger.com,1999:user-464300745974.blog-blogid</id>
<published>2010-06-22T10:59:38.603-07:00</published>
<updated>2010-11-26T02:32:31.658-08:00</updated>
<title>Application Testing Blog</title>
<summary type='html'>This blog is for testing the Android application.</summary>
<link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/blogs/blogid'/>
<link rel='alternate' type='text/html' href='http://devrum.blogspot.com/'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://devrum.blogspot.com/feeds/posts/default'/>
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/posts/default'/>
<link rel='http://schemas.google.com/blogger/2008#template' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/template'/>
<link rel='http://schemas.google.com/blogger/2008#settings' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/settings'/>
<author>
<name>Tim</name>
<uri>http://www.blogger.com/profile/blogid</uri>
<email>[email protected]</email>
</author>
</entry>
<entry gd:etag='W/"C08HRXo4eSp7ImA9Wx9TE0o."'>
<id>tag:blogger.com,1999:user-464300745974.blog-515600026106499737</id>
<published>2010-06-22T10:59:00.328-07:00</published>
<updated>2010-11-21T12:37:14.431-08:00</updated>
<title>Development Blog</title>
<summary type='html'>etc</summary>
<link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/blogs/515600026106499737'/>
<link rel='alternate' type='text/html' href='http://rumdev.blogspot.com/'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://rumdev.blogspot.com/feeds/posts/default'/>
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.blogger.com/feeds/515600026106499737/posts/default'/>
<link rel='http://schemas.google.com/blogger/2008#template' type='application/atom+xml' href='http://www.blogger.com/feeds/515600026106499737/template'/>
<link rel='http://schemas.google.com/blogger/2008#settings' type='application/atom+xml' href='http://www.blogger.com/feeds/515600026106499737/settings'/>
<author>
<name>Tim</name>
<uri>http://www.blogger.com/etc</uri>
<email>[email protected]</email>
</author>
</entry>
</feed>
我需要从上述提要中解析博客 ID 和帖子 ID。从我在 SAX 上找到的任何示例来看,它们根本不是通用的。我想写一个可重用的,您有任何示例如何相应地修改 SAXParser 吗?
I need to write an XML parsing class that I can reuse throughout my Android application and from what I've read a SAXParser is the best for a mobile application. I am using this guide:
http://www.jondev.net/articles/Android_XML_SAX_Parser_Example
And the type of document I wish to parse is a feed from the Blogger GData API - example would be:
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/"CUIGRnc4fyp7ImA9Wx9SEEg."'>
<id>tag:blogger.com,1999:user-464300745974.blogs</id>
<updated>2010-11-29T17:58:47.937Z</updated>
<title>Tim's Blogs</title>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/blogs'/>
<link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/blogs'/>
<link rel='alternate' type='text/html' href='http://www.blogger.com/profile/blogid'/>
<author>
<name>Tim</name>
<uri>http://www.blogger.com/profile/blogid</uri>
<email>[email protected]</email>
</author>
<generator version='7.00' uri='http://www.blogger.com'>Blogger</generator>
<openSearch:totalResults>2</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry gd:etag='W/"DUIBQHg-cCp7ImA9Wx9TF0s."'>
<id>tag:blogger.com,1999:user-464300745974.blog-blogid</id>
<published>2010-06-22T10:59:38.603-07:00</published>
<updated>2010-11-26T02:32:31.658-08:00</updated>
<title>Application Testing Blog</title>
<summary type='html'>This blog is for testing the Android application.</summary>
<link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/blogs/blogid'/>
<link rel='alternate' type='text/html' href='http://devrum.blogspot.com/'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://devrum.blogspot.com/feeds/posts/default'/>
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/posts/default'/>
<link rel='http://schemas.google.com/blogger/2008#template' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/template'/>
<link rel='http://schemas.google.com/blogger/2008#settings' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/settings'/>
<author>
<name>Tim</name>
<uri>http://www.blogger.com/profile/blogid</uri>
<email>[email protected]</email>
</author>
</entry>
<entry gd:etag='W/"C08HRXo4eSp7ImA9Wx9TE0o."'>
<id>tag:blogger.com,1999:user-464300745974.blog-515600026106499737</id>
<published>2010-06-22T10:59:00.328-07:00</published>
<updated>2010-11-21T12:37:14.431-08:00</updated>
<title>Development Blog</title>
<summary type='html'>etc</summary>
<link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/blogid/blogs/515600026106499737'/>
<link rel='alternate' type='text/html' href='http://rumdev.blogspot.com/'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://rumdev.blogspot.com/feeds/posts/default'/>
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.blogger.com/feeds/515600026106499737/posts/default'/>
<link rel='http://schemas.google.com/blogger/2008#template' type='application/atom+xml' href='http://www.blogger.com/feeds/515600026106499737/template'/>
<link rel='http://schemas.google.com/blogger/2008#settings' type='application/atom+xml' href='http://www.blogger.com/feeds/515600026106499737/settings'/>
<author>
<name>Tim</name>
<uri>http://www.blogger.com/etc</uri>
<email>[email protected]</email>
</author>
</entry>
</feed>
I need to parse the blog IDs and post IDs out of feeds like the above. From any example I find on SAX, they are not generic at all. I'd like to write a reusable one, do you have any examples how I can modify the SAXParser accordingly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SAX 解析器是事件驱动的解析器。您为每个类型的 XML 节点(开始元素、结束元素、属性、文本等)编写一个处理程序,然后解析器迭代 XML 文档,并将 SAX 事件发送给您(= 处理程序中的方法被调用)。
在您的特定情况下,您正在寻找两个节点序列:
所以您必须在处理程序中存储一些状态信息以了解您所在的位置。这是代码(我自己没有尝试过,你必须调试它):
SAX parsers are event driven parsers. You write a handler for every TYPE of XML node (start element, end element, attribute, text, etc..) and then parser iterates XML document and SAX events are sent to you (= methodes in your handler get called).
In your particular case you are looking for two node sequences:
<feed><id>
<feed><entry><id>
So you have to store some state info inside your handler to know where you are. Here is the code (didn't try it myself, you'll have to debug it):
尝试按照以下方式进行操作:
}
您可以使用如下方式访问自定义 SAXParser:
因此,您将自定义解析器传递到 Xml Reader 中,并从源中获取结果。在 Xml 解析期间,处理程序从“起始文档”开始,然后迭代 xml 中的元素(在开头调用 startElement,在开头调用 endElement)。在这两者之间调用字符方法 - 拾取字符(然后您可以在 endElement 中执行任何您想要的操作)。解析器在调用 endDocument 时完成,因此您可以根据需要在元素或整个文档的开头和结尾处进行设置和拆除。
希望这会有所帮助,并且接近您想要做的事情。
Try something along the lines of this:
}
You can access the custom SAXParser with something like this:
So you pass your custom parser into the Xml Reader, and get the results from the source. During the Xml Parsing, the handler starts at "start document" then iterates through the elements in the xml (calling startElement at the start, endElement at the beginning). The characters method is called in between these two - picking up the characters (which you can then do whatever you want with in the endElement). The parser is finished when endDocument is called, so you can set things up and tear them down at the start and end of elements or the whole document if you wish.
Hope this helps, and is close to what you are looking to do.