Android : Cdata in xml not parsing correctly(sax)

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

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

here is my 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>

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;
        }

    }

}

link to xmlfile here

Answers


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
    }

Hope it helps.


Need Your Help

Table View not showing all 20 items in my list

xcode uitableview project

Making a project for school everything is working except when i ran my project i was getting a duplicate of my items in my list after 10. (i.e. items 1,2,3,4 will show up again as 11,12,13,14) Only i

Using HAXE to output javascript

javascript haxe

Is anyone using HAxe to output to javascript for their front end web development?