Android: данные в xml неправильно анализируются (саксофон) - PullRequest
2 голосов
/ 30 ноября 2011

Я новичок в разработке для Android, и я пытаюсь создать приложение, которое извлекает XML из веб-службы.

все остальное в xml тянет нормально, кроме битов в теге Cdata, вместо того, чтобы извлекать большое количество 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 здесь

Ответы [ 2 ]

11 голосов
/ 30 ноября 2011

Решением вашей проблемы является использование 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
    }

Надеюсь, это поможет.

2 голосов
/ 30 ноября 2011

Я предоставлю следующую логику для разбора такого 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 это простой Java-класс для различных элементов, присутствующих в xml, таких как name , headline, httptext, website и т. Д., Имеющих методы установки и методы получения.
Надеюсь, это поможет вам.

...