解析 xml 时,在某些字行结尾处未获取完整数据,并以 read more 结尾
我正在尝试解析此内容:
当我解析时,我得到以下内容。
标题 = 工坊大都市 描述 = Workshop Metropolis groot succes Vlak voor de...阅读更多
阅读更多 描述中没有任何文字,您可能会在上面看到哪个 xml 标签? 我不明白为什么它会来。
代码如下:
///********************** XMLParsingExample.java **********************//
public class XMLParsingExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
// /** 发送 URL 来解析 XML 标签 */
URL sourceUrl = new URL("http://192.168.0.30/ibis.rss");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
for (int i = 0; i < sitesList.getLat().size(); i++) {
System.out.println("title = "+ sitesList.getLat().get(i).toString());
System.out.println("description = "+ sitesList.getLong().get(i).toString());
}
}
}
////MyXMLHandler
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("channel"))
{
/** 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;
/** set value */
if (localName.equalsIgnoreCase("title"))
sitesList.setLat(currentValue);
else if (localName.equalsIgnoreCase("description"))
sitesList.setLong(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 = StringEscapeUtils.unescapeHtml4(new String(ch, start, length).trim()); //new String(ch, start, length).trim()
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
public class SitesList {
/** Variables */
private ArrayList<String> mlat = new ArrayList<String>();
private ArrayList<String> mlong = new ArrayList<String>();
private ArrayList<String> mradiuslat = new ArrayList<String>();
private ArrayList<String> mradiuslong = new ArrayList<String>();
private ArrayList<String> minfo = new ArrayList<String>();
private ArrayList<String> msound = new ArrayList<String>();
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList<String> getLat() {
return mlat;
}
public void setLat(String lat) {
this.mlat.add(lat);
}
public ArrayList<String> getLong() {
return mlong;
}
public void setLong(String longs) {
this.mlong.add(longs);
}
}
I'm trying to parse this:
I get the following, when I parse.
title = Workshop Metropolis
description = Workshop Metropolis groot succes Vlak voor de...Read more
Read more is not any text on Description, which tag of xml you may see on above ?
I don't understand why it is coming.
Here's the code:
///********************** XMLParsingExample.java **********************//
public class XMLParsingExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
// /** Send URL to parse XML Tags */
URL sourceUrl = new URL("http://192.168.0.30/ibis.rss");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
for (int i = 0; i < sitesList.getLat().size(); i++) {
System.out.println("title = "+ sitesList.getLat().get(i).toString());
System.out.println("description = "+ sitesList.getLong().get(i).toString());
}
}
}
////MyXMLHandler
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("channel"))
{
/** 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;
/** set value */
if (localName.equalsIgnoreCase("title"))
sitesList.setLat(currentValue);
else if (localName.equalsIgnoreCase("description"))
sitesList.setLong(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 = StringEscapeUtils.unescapeHtml4(new String(ch, start, length).trim()); //new String(ch, start, length).trim()
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
public class SitesList {
/** Variables */
private ArrayList<String> mlat = new ArrayList<String>();
private ArrayList<String> mlong = new ArrayList<String>();
private ArrayList<String> mradiuslat = new ArrayList<String>();
private ArrayList<String> mradiuslong = new ArrayList<String>();
private ArrayList<String> minfo = new ArrayList<String>();
private ArrayList<String> msound = new ArrayList<String>();
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList<String> getLat() {
return mlat;
}
public void setLat(String lat) {
this.mlat.add(lat);
}
public ArrayList<String> getLong() {
return mlong;
}
public void setLong(String longs) {
this.mlong.add(longs);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先创建用于解析 [CData] 的 pojo 类(所有标记的 getter setter 方法)
采用 sax 解析器。把你的链接。
致电此:
我认为这会对您有所帮助。
first make
pojo class(getter setter method for your all tag)
for parsing [CData] take a sax parser. put your link.
from calling this:
I think this will help you.