0
我使用sax解析器解析xml文件。 xml文件包含链接标记中具有下一个属性的另一个xml文件的链接。我必须继续阅读,直到没有下一个属性的最后一个xml文件。 以下是XML文件:
阅读xml文件和xml文件的链接并保持解析
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/EokUNzGJBI8/comments" />
<link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/EokUNzGJBI8/comments/batch" />
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/EokUNzGJBI8/comments?start-index=1&max-results=25" />
<link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/EokUNzGJBI8/comments?start-index=26&max-results=25" />
我曾尝试以下:
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean content=false;
int i=0;
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("Content")) {
content = true;
i+=1;
}
if(qName.equalsIgnoreCase("Link") && attributes.getValue("rel").equalsIgnoreCase("next")){
l=attributes.getValue("href");
u=true;
}
}
要递归读取URL中l
上述回到我做的follwoing:
saxParser2.parse(new InputSource(ur.openStream()), handler);//to read original url
while(l!=null)
{
urs=new URL(l); //successive urls
saxParser.parse(new InputSource(urs.openStream()), handler);
}
的上面继续打印最后一个响应,然后在最后一个xml中找不到下一个响应。
这意味着我可以循环这个saxParser2同时呼吁复位? – MaxSteel 2013-03-08 08:04:24
@orabog基本上我想要做的就是读取链接标签中的rel =“next”的href,并解析它,直到最后一个没有rel =“next”attrib的xml文件。 – MaxSteel 2013-03-08 08:07:36