使用下面的 xml 中的 xml parsing,如何解析 description 細節?
<item>
<title>All India Lawyer Conference at Lucknow </title>
<description>
<text><![CDATA[<p style="text-align: center;">]]></text>
<image>http://www.bjp.org/images/stories/kamala_2.png</image>
<text><![CDATA[
<p style="text-align: center;"><span style="color: #ff6600;"><span style="font-size: 24pt;"><strong>Bharatiya Janata Party</strong><strong><em><br /><br /></em></strong></span></span></p>
<span style="color: #008000;"><span style="font-size: 18pt;"><span style="font-family: arial black,avant garde;"> </span></span></span>
<p style="text-align: center;"><span style="color: #008000;"><span style="font-size: 18pt;"><strong><span style="font-family: 'arial black', 'avant garde';">All India Lawyer Conference </span></strong></span></span></p>
<p style="text-align: center;"><span style="color: #008000;"><span style="font-size: 18pt;"><strong><span style="font-family: 'arial black', 'avant garde';">at Lucknow (Uttar Pradesh)</span></strong></span></span></p>
<p style="text-align: center;"><span style="font-family: 'arial black', 'avant garde'; color: #008000; font-size: large;"><strong><br /></strong></span></p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"><strong><span style="font-family: andale mono,times;"><em><span style="font-size: 14pt;"><span style="color: #ff0000;">of</span></span></em></span></strong></p>
<p style="text-align: center;"><span style="font-family: arial black,avant garde;"><span style="font-size: 14pt;"><span style="color: #000080;"><strong>BJP President Shri Nitin Gadkari</strong></span></span></span></p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"><span style="font-size: 12pt;"><span style="color: #ff0000;"><strong>on<br /></strong></span></span></p>
<p style="text-align: center;"><strong><span style="font-size: 12pt;"><span style="color: #ff0000;">May 15, 2011 (Sunday)</span></span><br /></strong></p>]]></text>
</description>
<author> [email protected] (Anurag Pathak)</author>
<pubDate>Sun, 15 May 2011 00:00:00 GMT</pubDate>
</item>
我想把文本以粗體顯示:
@Override
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException
{
printParseInfo("startElement:", uri, localName, qName);
int attributesLength = attributes.getLength();
for (int i = 0; i < attributesLength; i++)
{
printAttributeInfo(attributes, i);
}
currentElement = true;
if (localName.equals("rss"))
{
/** Start */
sitesList = new SitesList();
}
}
/** Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
currentElement = false;
printParseInfo("endElement: ", uri, localName, qName);
/** set value */
if (localName.equalsIgnoreCase("title"))
{
//Log.d("title--->",currentValue);
sitesList.setTitle(currentValue);
}
else
if(localName.equalsIgnoreCase("span"))
{
//Log.d("strong-->",currentValue);
sitesList.setStrong(currentValue);
}
if (localName.equalsIgnoreCase("description"))
{
//Log.d("description--->",currentValue);
sitesList.setDescription(currentValue);
}
else
if (localName.equalsIgnoreCase("author"))
{
//Log.d("Author--->",currentValue);
sitesList.setAuthor(currentValue);
}
else
if(localName.equalsIgnoreCase("pubDate"))
{
//Log.d("pubDate--->",currentValue);
sitesList.setPubDate(currentValue);
}
else
if(localName.equalsIgnoreCase("image"))
{
//Log.d("Image--->",currentValue);
sitesList.setImage(currentValue);
}
}
/** Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get AndroidPeople Character ) */
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
if (currentElement)
{
currentValue = new String(ch, start, length);
Log.d("char-->",currentValue);
currentElement = false;
}
}
使用 XML 解析器
1. SAX
2. DOM
3. StAX