简体   繁体   English

XML SAX解析未返回所需结果

[英]XML SAX parsing doesn't returned the desired result

I have a XML provided by my client like this: 我的客户提供了这样的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>

Here is my parsing code: 这是我的解析代码:

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

When I parse it using SAX parser everything is returned correctly. 当我使用SAX解析器解析它时,一切都正确返回。 But when I try to parse <q_thighscore> or <q_phighscore> , it's returning just mode=1 and mode=2 , but I want full link to be parsed, where is the problem and what should I do now? 但是,当我尝试解析<q_thighscore><q_phighscore> ,它只返回mode=1mode=2 ,但是我想解析完整链接,问题出在哪里,我现在该怎么办?

those two entities have an ampersand - a "&" - that is being turned into a " & # 3 8 ;" 这两个实体的“&”符号被转换为“&#3 8;”。 so you've got to deal with those in your code differently 因此您必须以不同的方式处理代码中的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM