示例 XML:
<rss version="2.0">
<channel>
<title>The Channel Title Goes Here</title>
<description>
The explanation of how the items are related goes here
</description>
<link>/events</link>
<item>
<title>
<![CDATA[ some title ]]>
</title>
<description>
<![CDATA[ ]]>
</description>
<content:encoded xmlns:content="http://stuff.com">
<![CDATA[
<event>
<name>some name</name>
<startdate>some date</startdate>
<starttime>some time</starttime>
<location>some place</location>
<description></description>
</event>
]]>
</content:encoded>
</item>
<item>
</item>
...
</channel>
</rss>
我的 SAX 解析器startElement
方法从不进入以下if
语句,但 sysout 将“事件”打印到 logcat。为什么它永远localName.equals("event")
找不到true
?
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
System.out.println(localName);
if (localName.equals("event")) // <-- never goes in here
{
// do stuff
}
}
这与 CDATA 以及我如何设置解析器有关吗?这是我实例化所有内容的方式:
URL sourceUrl = new URL(kEventsURL);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));