简体   繁体   English

xml SAX解析未返回所需结果

[英]xml SAX parsing doesnt returned the desired result

I have an 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>

but when I parse it using SAX parser everything is returned correctly but when I try to parse <q_thighscore> or <q_phighscore> its 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? 但是当我使用SAX解析器解析它时,一切都正确返回,但是当我尝试解析<q_thighscore><q_phighscore>它返回的只是mode=1mode=2 ,但我想解析完整链接,这是问题所在,我现在应该怎么办?

You might want to use sax default Handler. 您可能要使用sax默认处理程序。 See if it works. 看看是否可行。 If not might need to extend default Handler and override behavior of this method to store the value correctly. 如果不是,则可能需要扩展默认的Handler并重写此方法的行为以正确存储值。 Where myURLString is a variable you define in you extended class. 其中myURLString是您在扩展类中定义的变量。 This variable can then be accessed in endElement method for eample. 然后可以在endElement方法中访问此变量以进行采样。 you have total control over this here. 您可以在这里完全控制。

So one way is to extend org.xml.sax.helpers.DefaultHandler; 因此,一种方法是扩展org.xml.sax.helpers.DefaultHandler;。

and override as below 并覆盖如下

   protected String myValueString;


   @Override
   public void characters(char[] ch, int start, int length) throws SAXException
   {
    super.characters(ch, start, length);  // deal with escape values here.
            myValueString = ...

    }

then inside   endElement(){


   check if matches your type and if so use myValueString;

我已经解决了它,我只用了Pull解析器而不是SAX解析器,就可以了:)

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

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