这已成为我背后的真正痛苦。
我正在尝试解析的网址是http://torrentz.eu/feed_verifiedP?q=ubuntu
这是xml的简短版本:
<?xml version="1.0"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Torrentz - ubuntu</title>
<link>http://torrentz.eu/verified?q=ubuntu</link>
<description>ubuntu search</description>
<language>en-us</language>
<atom:link href="http://torrentz.eu/feed_verifiedP?q=ubuntu" rel="self" type="application/rss+xml" />
<item>
<title>ubuntu 11 10 desktop i386 iso</title>
<link>http://torrentz.eu/8ac3731ad4b039c05393b5404afa6e7397810b41</link>
<guid>http://torrentz.eu/8ac3731ad4b039c05393b5404afa6e7397810b41</guid>
<pubDate>Thu, 13 Oct 2011 15:02:06 +0000</pubDate>
<category>apps linux applications os software</category>
<description>Size: 695 MB Seeds: 4,613 Peers: 161 Hash: 8ac3731ad4b039c05393b5404afa6e7397810b41</description>
</item>
</channel>
</rss>
我的代码:
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
//Get Torrents
XMLTorrentsRSSHandler torrentsHandler = new XMLTorrentsRSSHandler();
xr.setContentHandler(torrentsHandler);
InputStream in = url.openStream();
xr.parse(new InputSource(in));
XMLTorrentsRSSParsedDataSet parsedTorrentsDataSet = torrentsHandler.getParsedData();
我一直得到这个例外:
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 53: mismatched tag
为什么翻盖会像这样折磨我??
编辑:这种方法工作正常,直到今天。也许这个网站发生了变化,但这个flippin'不匹配的标签在哪里?答案 0 :(得分:-1)
为什么你的构建路径上有Harmony?您的代码可以与Oracle JDK7u3中的内置SAXParser一起使用。如果没有理由使用和声实现,则应恢复为标准实现。
测试用例表格:
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
class Scratch {
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
final String document = "<?xml version=\"1.0\"?>\n" +
"<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n" +
" <channel>\n" +
" <title>Torrentz - ubuntu</title>\n" +
" <link>http://torrentz.eu/verified?q=ubuntu</link>\n" +
" <description>ubuntu search</description>\n" +
" <language>en-us</language>\n" +
" <atom:link href=\"http://torrentz.eu/feed_verifiedP?q=ubuntu\" rel=\"self\" type=\"application/rss+xml\" />\n" +
" <item>\n" +
" <title>ubuntu 11 10 desktop i386 iso</title>\n" +
" <link>http://torrentz.eu/8ac3731ad4b039c05393b5404afa6e7397810b41</link>\n" +
" <guid>http://torrentz.eu/8ac3731ad4b039c05393b5404afa6e7397810b41</guid>\n" +
" <pubDate>Thu, 13 Oct 2011 15:02:06 +0000</pubDate>\n" +
" <category>apps linux applications os software</category>\n" +
" <description>Size: 695 MB Seeds: 4,613 Peers: 161 Hash: 8ac3731ad4b039c05393b5404afa6e7397810b41</description>\n" +
" </item>\n" +
" </channel>\n" +
"</rss>\n";
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
ContentHandler torrentsHandler = new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.printf("%s / %s / %s\n", uri, localName, qName);
}
};
xr.setContentHandler(torrentsHandler);
xr.parse(new InputSource(new StringReader(document)));
}
}