XML SAX解析不返回所需的结果

时间:2012-08-01 14:24:57

标签: java android xml-parsing

我的客户提供的XML就像这样:

<?xml version='1.0' encoding='UTF-8' ?><quizzes updatetimestamp='2012-08-01 06:24'><quiz q_id='1'>
<q_name>Airplane!</q_name >
<q_appleid>com.patelware.moviesquiz.free</q_appleid>
<q_description>"Roger, Roger"</q_description>
<q_urlicon>http://www.inall3.com/king/icons/airplane_icon.png</q_urlicon>
<q_urlmainimg>http://www.inall3.com/king/icons/airplane_main.png</q_urlmainimg>
<q_urldb>http://www.inall3.com/king/dbs/airplane.xml</q_urldb>
<q_timesofupload>2011-05-01 00:00:00</q_timesofupload>
<q_priceindollar>0.00</q_priceindollar>

<q_cattype>Comedy</q_cattype>
<q_thighscore>http://www.inall3.com/hs/highscoreairplane.php?action=gethighscores&amp;mode=1</q_thighscore>
<q_phighscore>http://www.inall3.com/hs/highscoreairplane.php?action=gethighscores&amp;mode=2</q_phighscore>
</quiz>

这是我的解析代码:

public class DynamicXmlHandler extends DefaultHandler {

    DynamicXMLgettersetter info = new DynamicXMLgettersetter();

    boolean q_nameOn = false;
    String q_nameValue = null;

    boolean q_DescrptionOn = false;
    String q_DescriptionValue = null;

    boolean q_MainImgOn = false;
    String q_MainImgValue = null;

    boolean q_urldbOn = false;
    String q_urldbValue = null;

    boolean q_PriceOn = false;
    String q_PriceValue = null;

    boolean q_CatagoryOn = false;
    String q_CatagoryValue = null;

    boolean q_TmodeOn = false;
    String q_TmodeValue = null;

    boolean q_PmodeOn = false;
    String q_PmodeValue = null;

    // .................................................................

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        if (localName.equals("q_name")) {

            q_nameOn = true;
        }

        if (localName.contentEquals("q_description")) {

            q_DescrptionOn = true;

        }

        if (localName.contentEquals("q_urlmainimg")) {

            q_MainImgOn = true;

        }
        if (localName.contentEquals("q_urldb")) {

            q_urldbOn = true;

        }
        if (localName.contentEquals("q_priceindollar")) {

            q_PriceOn = true;

        }
        if (localName.contentEquals("q_cattype")) {

            q_CatagoryOn = true;

        }
        if (localName.contentEquals("q_thighscore")) {

            q_TmodeOn = true;

        }
        if (localName.contentEquals("q_phighscore")) {

            q_PmodeOn = true;

        }

        super.startElement(uri, localName, qName, attributes);
    }

    // ..................................................................

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (q_nameOn == true) {

            q_nameValue = new String(ch, start, length);

        }

        if (q_DescrptionOn == true) {

            q_DescriptionValue = new String(ch, start, length);

        }

        if (q_MainImgOn == true) {

            q_MainImgValue = new String(ch, start, length);

        }
        if (q_urldbOn == true) {

            q_urldbValue = new String(ch, start, length);

        }
        if (q_PriceOn == true) {

            q_PriceValue = new String(ch, start, length);

        }
        if (q_CatagoryOn == true) {

            q_CatagoryValue = new String(ch, start, length);

        }
        if (q_TmodeOn == true) {

            q_TmodeValue = new String(ch, start, length);

        }
        if (q_PmodeOn == true) {

            q_PmodeValue = new String(ch, start, length);

        }

        super.characters(ch, start, length);
    }

    // ..................................................................

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        if (localName.equalsIgnoreCase("q_name")) {

            q_nameOn = false;
            info.setqname(q_nameValue);
        }

        if (localName.equalsIgnoreCase("q_description")) {

            q_DescrptionOn = false;
            info.setdescription(q_DescriptionValue);
        }

        if (localName.equalsIgnoreCase("q_urlmainimg")) {

            q_MainImgOn = false;
            info.seturlimage(q_MainImgValue);
        }
        if (localName.equalsIgnoreCase("q_urldb")) {

            q_urldbOn = false;
            info.seturldb(q_urldbValue);
        }

        if (localName.equalsIgnoreCase("q_priceindollar")) {

            q_PriceOn = false;
            info.setpriceindollar(q_PriceValue);
        }

        if (localName.equalsIgnoreCase("q_cattype")) {

            q_CatagoryOn = false;
            info.setcattype(q_CatagoryValue);
        }

        if (localName.equalsIgnoreCase("q_thighscore")) {

            q_TmodeOn = false;
            info.setthighscore(q_TmodeValue);
        }

        if (localName.equalsIgnoreCase("q_phighscore")) {

            q_PmodeOn = false;
            info.setphighscore(q_PmodeValue);
        }


        super.endElement(uri, localName, qName);
    }

当我使用SAX解析器解析它时,一切都正确返回。但是,当我尝试解析<q_thighscore><q_phighscore>时,它只返回mode=1mode=2,但我希望解析完整的链接,问题出在哪里,我该怎么办?现在做什么?

1 个答案:

答案 0 :(得分:1)

这两个实体有一个&符号 - 一个“&amp;” - 那变成了“&amp;#3 8;” 所以你必须以不同的方式处理代码中的那些