내 프로젝트에서 XML을 구문 분석해야합니다. XML의 일부 항목에는 HTML 태그가 있습니다. 나는 그 태그를 제거하려고했지만 성공하지 못했습니다. 활동의 코드는 다음과 같습니다Android, XML 구문 분석, HTML 태그를 무시하는 방법?
이private NewsFeedItemList parseNewsContent() {
NewsParserHandler newsParserHandler = null;
Log.i("NewsList", "Starting to parse XML...");
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xr = parser.getXMLReader();
newsParserHandler = new NewsParserHandler();
xr.setContentHandler(newsParserHandler);
ByteArrayInputStream is = new ByteArrayInputStream(strServerResponseMsg.getBytes());
xr.parse(new InputSource(is));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NewsFeedItemList itemList = newsParserHandler.getNewsList();
// checkLog(itemList);
Log.i("NewsList", "Parsing XML finished. Sending result back to caller...");
return itemList;
}
"strServerResponseMsg"가 포함 된 XML 정보를 내가 완전히 분석되지 않습니다 HTML 태그가 모든 항목 그러나 그 구문 분석 캠 (http://www.mania.com.my/rss/ManiaTopStoriesFeedFull.aspx?catid=146)
의.
이 내 파서 핸들러 :
public class NewsParserHandler extends DefaultHandler {
private NewsFeedItemList newsFeedItemList;
private boolean current = false;
private String currentValue = null;
/* Because the feed has another "Title", "link" and "pubdate" name in root,
* we need to don't let to be stored in arrays. Therefore, we ignore all of
* them by incrementing count.*/
private int count = 0;
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
if(current) {
currentValue = new String(ch, start, length);
if(currentValue==null || currentValue=="" || currentValue==" ")
currentValue = "-";
current = false;
}
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
newsFeedItemList = new NewsFeedItemList();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
current = true;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
current = false;
if(localName.equals("title")) {
if(count >= 1)
newsFeedItemList.setTitle(currentValue);
}
if(localName.equals("description")) {
newsFeedItemList.setDescription(currentValue);
}
if(localName.equals("fullbody")) {
newsFeedItemList.setFullbody(currentValue);
}
if(localName.equals("link")) {
if(count >= 4)
newsFeedItemList.setLink(currentValue);
}
if(localName.equals("pubDate")) {
if(count >= 5)
newsFeedItemList.setPubDate(currentValue);
}
if(localName.equals("image")) {
newsFeedItemList.setImage(currentValue);
}
count++;
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
public NewsFeedItemList getNewsList() {
return newsFeedItemList;
}
}
내가 문자 currentValue = Html.fromHtml(currentValue).toString();
을 (으려고) 방법하지만 아무것도 효과를 취하지 않는다. 또한 "strServerResponseMsg"를 보내기 전에 HTML로 변경하려고했지만 파서는 아무 것도 구문 분석하지 않았습니다.
내가 너무 많이 주셔서 감사합니다. 감사.
감사합니다. Lalit,하지만 불행히도 작동하지 않습니다. 왜 이런 식으로 생각하는지 모르겠다. ( – Hesam