Parsing big XML files using SAX parser (skip some

2019-04-26 20:01发布

I am currently developing an app that retrieves data from the internet using SAX. I used it before for parsing simple XML files like Google Weather API. However, the websites that I am interested in take parsing to the next level. The page is huge and looks messy. I only need to retrieve some specific lines; the rest is not useful for me.
Is it possible to skip those useless lines/tags, or do I have to go step by step?

7条回答
叼着烟拽天下
2楼-- · 2019-04-26 20:09

You can try a combination of TagSoup for creating a parseable XML document and XPath for fetching the interesting parts.

查看更多
smile是对你的礼貌
3楼-- · 2019-04-26 20:09

See my answer to a similar question for a strategy of using SAX to skip/ignore tags:

Skipping nodes with sax

It involves switching ContentHandlers on the XMLReader. When you read a porting of the XML document you want to skip you simply swap in a ContentHandler that does nothing with the events. When the end of the section to be ignored is reached it passes control back to the content handler you were using to process the XML content.

查看更多
该账号已被封号
4楼-- · 2019-04-26 20:14

I like commons-digester. It allows you to specify rules against particular tags. The rule gets executed only when the tag is encountered.

Digester is built over sax and hence has all the sax features plus the specificity that is required for selectively parsing specific tags. It also uses a stack that is pushed with new elements as and when the corresponding tag is encountered and is popped when the element ends.

I use it for parsing all my configuration files.

Check out digester at http://commons.apache.org/digester/

查看更多
▲ chillily
5楼-- · 2019-04-26 20:27

Yes you can do it, just ignore the tags you are not interested in. But note that the entire document will have to be parsed for this (DefaultHandler impl)

public startElement(String uri, String localName, 
     String qName, Attributes attributes)  {
  if(localName.equals("myInterestingTag") {
     // do your thing....
  }
}

public void endElement(String uri, String localName, String qName) {
  if(localName.equals("myInterestingTag") {
     // do your thing....
  }
}

public void characters(char[] ch, int start, int length) {
  // if parsing myinteresting tag... do some stuff.
}
查看更多
\"骚年 ilove
6楼-- · 2019-04-26 20:31

Yes, you can skip. Just define those tag which you want and it will only fetch those tag values.

查看更多
三岁会撩人
7楼-- · 2019-04-26 20:33

You can try to use XPath which will use SAX behind the scene to parse your xml. The downside here is that XML will be parsed on every call of Xpath evaluate method.

查看更多
登录 后发表回答