我是Android开发的新手,我试图创建一个从Web服务中提取xml的应用程序。
除了在Cdata标签中的位之外,所有xml的其余部分都处于良好状态,而不是拉入大量的html文本,它会输入“<p>
”。有人能指出我正确的方向,为什么它没有正确拉入
这是我的xml
<articles>
<article>
<name>AndroidPeople</name>
<headline>notandroidpeople</headline>
<website category="android">www.androidpeople.com</website>
<htmltext><![CDATA[<p>
HAVING lost to Manchester City and drawn with Manchester United, Yohan Cabaye is confident Newcastle are capable of rounding off a testing treble by beating Chelsea provided they reproduce their
performance from Old Trafford.
</p>
<p>
The Magpies drew 1-1 at the weekend, with Demba Ba's penalty a just reward for their efforts even if it owed much to a fortuitous decision from referee Mike Jones.
</p>
<p>
Whereas they were outclassed for periods of the previous weekend's defeat at the Etihad Stadium, Saturday's game saw them more than match a Manchester United side who remain comfortably ensconced
in second position.
</p>
<p>
And with fifth-placed Chelsea having lost three of their last five league games, Cabaye feels Newcastle will claim a notable scalp provided they do not allow their performance levels to dip from
the standards they established at the weekend.
</p>
<p>
"We learned from the City game," said the French midfielder, who could resume his partnership with Cheik Tiote in three days time if the Ivory Coast international finally completes his recovery
from a knee problem. "We learned not to give Man United too much respect.
</p>
<p>
"It was important to play higher up the pitch and I think we did well. Against Chelsea, we have to play like we did at Old Trafford."
</p>
<p>
While they have not beaten Chelsea in a league game since 2006, Newcastle are entitled to go into Saturday's lunch-time kick-off in a confident mood given their record of four wins and two draws
from their six home games this season.
</p>
<p>
"We have to believe in ourselves," said Cabaye. "It's important to win the game. We've got to play like the winning team. We will be at home so we have to play like a top-of-the-league team to win
this match."
</p>
<p>
Chelsea go into this weekend's game ten points adrift of league leaders Manchester City, and while last weekend's 3-0 win over Wolves lifted some of the pressure on Andre Villas-Boas, the Londoners
cannot afford to drop more points on their visit to the North-East.
</p>
<p>
Their title hopes have suffered a series of significant blows in the last few weeks, and Cabaye feels the battle for the Premier League crown is already a two-horse race.
</p>
<p>
"I think the two Manchester clubs will be the top two for the rest of the season," he said. "I think they will be the teams fighting for the league.
</p>
<p>
"Are City better? I don't know. It's hard to compare the two. Man United have some very good players, so it is difficult to simply say that Man City are better than Man United."
</p>]]></htmltext>
</article>
<article>
<name>iPhoneAppDeveloper</name>
<headline>notiphonepeople</headline>
<website category="iPhone">www.iphone-app-developer.com</website>
<htmltext><![CDATA[<p>
HAVING lost to Manchester City and drawn with Manchester United, Yohan Cabaye is confident Newcastle are capable of rounding off a testing treble by beating Chelsea provided they reproduce their
performance from Old Trafford.
</p>
<p>
The Magpies drew 1-1 at the weekend, with Demba Ba's penalty a just reward for their efforts even if it owed much to a fortuitous decision from referee Mike Jones.
</p>
<p>
Whereas they were outclassed for periods of the previous weekend's defeat at the Etihad Stadium, Saturday's game saw them more than match a Manchester United side who remain comfortably ensconced
in second position.
</p>
<p>
And with fifth-placed Chelsea having lost three of their last five league games, Cabaye feels Newcastle will claim a notable scalp provided they do not allow their performance levels to dip from
the standards they established at the weekend.
</p>
<p>
"We learned from the City game," said the French midfielder, who could resume his partnership with Cheik Tiote in three days time if the Ivory Coast international finally completes his recovery
from a knee problem. "We learned not to give Man United too much respect.
</p>
<p>
"It was important to play higher up the pitch and I think we did well. Against Chelsea, we have to play like we did at Old Trafford."
</p>
<p>
While they have not beaten Chelsea in a league game since 2006, Newcastle are entitled to go into Saturday's lunch-time kick-off in a confident mood given their record of four wins and two draws
from their six home games this season.
</p>
<p>
"We have to believe in ourselves," said Cabaye. "It's important to win the game. We've got to play like the winning team. We will be at home so we have to play like a top-of-the-league team to win
this match."
</p>
<p>
Chelsea go into this weekend's game ten points adrift of league leaders Manchester City, and while last weekend's 3-0 win over Wolves lifted some of the pressure on Andre Villas-Boas, the Londoners
cannot afford to drop more points on their visit to the North-East.
</p>
<p>
Their title hopes have suffered a series of significant blows in the last few weeks, and Cabaye feels the battle for the Premier League crown is already a two-horse race.
</p>
<p>
"I think the two Manchester clubs will be the top two for the rest of the season," he said. "I think they will be the teams fighting for the league.
</p>
<p>
"Are City better? I don't know. It's hard to compare the two. Man United have some very good players, so it is difficult to simply say that Man City are better than Man United."
</p>]]></htmltext>
</article>
</articles>
这是我的代码
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = null;
public static SitesList sitesList = null;
public static SitesList getSitesList() {
return sitesList;
}
public static void setSitesList(SitesList sitesList) {
MyXMLHandler.sitesList = sitesList;
}
/** Called when tag starts ( ex:- <name>AndroidPeople</name>
* -- <name> )*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("articles"))
{
/** Start */
sitesList = new SitesList();
} else if (localName.equals("website")) {
/** Get attribute value */
String attr = attributes.getValue("category");
sitesList.setCategory(attr);
}
else if (localName.equals("htmlText")){
}
}
/** Called when tag closing ( ex:- <name>AndroidPeople</name>
* -- </name> )*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("name"))
sitesList.setName(currentValue);
else if (localName.equalsIgnoreCase("headline"))
sitesList.setHeadline(currentValue);
else if (localName.equalsIgnoreCase("website"))
sitesList.setWebsite(currentValue);
else if (localName.equalsIgnoreCase("htmltext"))
sitesList.setHtmltext(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);
currentElement = false;
}
}
}
链接到xmlfile here
答案 0 :(得分:11)
问题的解决方案是在阅读字符时使用StringBuffer
,并在endElement
使用buffer.toString()
中获取所有数据。以下是一些代码段:
private StringBuffer buffer;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
buffer = new StringBuffer();
// TODO: your other code
}
public void characters(char[] ch,int start, int length) throws SAXException{
String readChars = new String(ch,start,length);
if(buffer != null) buffer.append(readChars);
}
public void endElement(String uri, String localName, String qName) throws SAXException {
currentValue = buffer.toString();
// TODO: your other code
}
希望它有所帮助。
答案 1 :(得分:2)
我将提供以下逻辑来解析这样的xml文件,这对我有用。
final ArrayList<NewsItem> newsList=new ArrayList<NewsItem>();
try
{
URL urlexec=new URL("http://www.briefsonline.co.ukname/example.xml");
URLConnection connection=urlexec.openConnection();
InputStream input=connection.getInputStream();
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
parser.parse(input,new DefaultHandler(){
boolean itemTagStarted=false;
String currentTag="";
StringBuffer buffer;
NewsItem item=null;
@Override
public void startDocument() throws SAXException {
Log.v("parsing started!!!","parsing started!!!");
}
@Override
public void startElement(String uri, String localName,String qName, Attributes attributes)
throws SAXException {
currentTag=localName;
Log.v("in start elelmet", "in start elelmet");
if(qName.equals("article"))
{
buffer=new StringBuffer();
itemTagStarted=true;
item=new NewsItem();
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(itemTagStarted)
{
if(currentTag.equals("name"))
{
item.setCatname(String.valueOf(ch,start,length));
}
if(currentTag.equals("headline"))
{
item.setCatid(String.valueOf(ch,start,length));
}
if(currentTag.equals("htmltext"))
{
buffer.append(String.valueOf(ch,start,length));
}
}
}
@Override
public void endElement(String uri, String localName,
String qName) throws SAXException {
currentTag="";
if(qName.equals("article"))
{
itemTagStarted=false;
newsList.add(item);
}
if(qName.equals("htmltext"))
{
item.setCatdesc(buffer.toString());
Log.v("Description data",buffer.toString());
}
}
@Override
public void endDocument() throws SAXException {
Log.v("parsing completed","parsing completed : array size : "+newsList.size());
}
});
}
catch(Exception ex)
{
ex.printStackTrace();
}
在此 NewsItem 是xml中存在的不同项目的简单java类,如名称,标题,httptext,网站等,具有setter和吸气方法。
希望它会对你有所帮助。