我的客户提供了这样的 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&mode=1</q_thighscore>
<q_phighscore>http://www.inall3.com/hs/highscoreairplane.php?action=gethighscores&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>
or<q_phighscore>
时,它只返回mode=1
and mode=2
,但我想要解析完整链接,问题出在哪里,我现在该怎么办?