安卓:CDATA在XML中无法正确解析(SAX) [英] Android : Cdata in xml not parsing correctly(sax)

查看:146
本文介绍了安卓:CDATA在XML中无法正确解析(SAX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的android开发,和IM试图创建一个从Web服务拉动XML的应用程序。

I'm new to android development, and im trying to create an app that is pulling xml from a web service.

在XML中的其他一切拉细,除了在CDATA标记位,而不是在一个大ammount的HTML文本的拉动,它拉在< P> 。可能有人点我在正确的方向,为什么它不能正常拉动

all the rest of the xml pulls in fine, apart from the bits in the Cdata tag, instead of pulling in a large ammount of html text, it pulls in "<p>". Could someone point me in the right direction as to why its not pulling in properly

这是我的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>

和这里是我的code

and here is my code

    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 这里

推荐答案

该问题的解决方案是使用的StringBuffer 同时读取字符,并在你的的endElement 使用 buffer.toString()来获取所有数据。下面是一些code片段:

The solution to your problem is using a StringBuffer while reading characters and in your endElement use buffer.toString() to get all data. Here are some code snippets:

    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
    }

希望它帮助。

这篇关于安卓:CDATA在XML中无法正确解析(SAX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆