English 中文(简体)
SAXParser 无法获取具体数据
原标题:SAXParser Failing for a specific data

我试图分析一个 XML 文件 看起来像这个 :

<?xml version="1.0" encoding="utf-8"?>
<downloaddata>
    <downloaditem itemid="1">
    <title>Abdul kalaam Inspirational Talk</title>
    <downloadlink>http://o-o.preferred.spectranet-blr1.v8.lscache4.c.youtube.com/videoplayback?upn=Rxb-DvFeBTE&sparams=cp%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&fexp=906512%2C907217%2C907335%2C921602%2C919306%2C919316%2C904455%2C919324%2C904452&itag=18&ip=203.0.0.0&signature=96D7FA17DF684B4C2CD30F12251F3263C83EC443.05F62E98E1059BB44459ABF319F50DC4B7E6D90E&sver=3&ratebypass=yes&source=youtube&expire=1337691481&key=yt1&ipbits=8&cp=U0hSTFZUT19NS0NOMl9OTlNFOmlwaTFSSGFfd3NK&id=67ffa1d50864f57d&title=Abdul%20Kalam%20inspirational%20Speech%20on%20Leadership%20and%20Motivation</downloadlink>
    </downloaditem>
</downloaddata>

downloadlink 标签的数据与上面的数据相同时,看来解析失败了。 我试图用相同长度的其他数据替换数据, 并且效果良好 。

下面是我使用的机器人密码

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.os.Environment;

public class Wilxmlparser extends DefaultHandler{

List<VideoDetails> downloadList;
private String tempVal;
private VideoDetails tempVidDet;

public Wilxmlparser(){

}

public void parseXML() {

//get a factory
SAXParserFactory spf = SAXParserFactory.newInstance();
try {

    //get a new instance of parser
    SAXParser sp = spf.newSAXParser();

    File downloadInfo =new         File(Environment.getExternalStorageDirectory()+"/watchitlater/config/downloadinfo1.xml");        
    //parse the file and also register this class f或call backs
    sp.parse(downloadInfo, this);

}catch(SAXException se) {
    se.printStackTrace();
}catch(ParserConfigurationException pce) {
    pce.printStackTrace();
}catch (IOException ie) {
    ie.printStackTrace();
}
}


//Event Handlers
@Override
public void startElement(String uri, String localName, String qName, Attributes     attributes) throws SAXException {
//reset
tempVal = "";
if(qName.equalsIgnoreCase("downloaditem")) {
    tempVidDet = new VideoDetails();
    tempVidDet.setItemId(Integer.parseInt(attributes.getValue("itemid")));
    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
tempVal = new String(ch,start,length);
}

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

if(qName.equalsIgnoreCase("downloaditem")) {
downloadList.add(tempVidDet);
}else if (qName.equalsIgnoreCase("title")) {
    tempVidDet.setTitle(tempVal);
}else if (qName.equalsIgnoreCase("downloadlink")) {
    tempVidDet.setDownloadLink(tempVal);        
    }
}
}

The above code does not give a callback to endElement f或the above xml file. however if the xml were to be like

<?xml version="1.0" encoding="utf-8"?>
<downloaddata>
    <downloaditem itemid="1">
        <title>Abdul kalaam Inspirational Talk</title>
        <downloadlink>http://www.gmail.com/hello/world/sdfsdf%20.@@%!@#    ($dwe</downloadlink>
    </downloaditem>
</downloaddata>

<?xml version="1.0" encoding="utf-8"?>
<downloaddata>
    <downloaditem itemid="1">
        <title>Abdul kalaam Inspirational Talk</title>
            <downloadlink>httphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttphttpa</downloadlink>
    </downloaditem>
</downloaddata>

那很好,我做错什么了?

问题回答

您的剖析器无法解析相关 xml 的原因是它无效 。 导致您问题的部分数据含有必须逃避的字符 。 见 < a href="http:// en.wikipedia. org/ wiki/ XML#Characters_and_ escaping" rel= “ nofollow noreferrer" > Characters 并避开 XML 维基佩迪亚文章中的 以获取更多信息 。

这在产生xml的任何材料中都是最好的纠正方法,最简单的办法是将违规文本包装在CDATA部分

数据一经确定,您也可以在解析代码中看到误解引起的问题。

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
   tempVal = new String(ch,start,length);
}

开始标签和结束标签之间不会总是获得所有字符, 因为此方法的合同允许它被多次调用。 您需要将字符串缓冲附加到在 < code> startEplement 方法中初始化并在 < code> endEmentle 方法中使用的字符串缓冲中, 而不是简单地复制到字符串中 。

我对另一个SO问题 的回答,关于这个 characterers 方法解析问题,多一点。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签