我正在开发一个应用程序,它以XML格式从远程数据源获取数据。
然后,我的应用程序会将XML解析为对象,以便将数据放入适当的字段中。
以下是我的数据的XML来源(省略了非必要部分):
<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<opensearch:Query searchTerms="36557"/>
<opensearch:totalResults>1</opensearch:totalResults>
<movies>
<movie>
<popularity>95126.637</popularity>
<translated>true</translated>
<adult>false</adult>
<language>en</language>
<original_name>Casino Royale</original_name>
<name>Casino Royale</name>
<alternative_name>James Bond - 2006 - Casino Royale</alternative_name>
<type>movie</type>
<id>36557</id>
<imdb_id>tt0381061</imdb_id>
<url>http://www.themoviedb.org/movie/36557</url>
<overview>James Bond goes on his first ever mission as a 00. Le Chiffre is a banker to the world's terrorists. He is participating in a poker game at Montenegro, where he must win back his money, in order to stay safe among the terrorist market. The boss of MI6, known simply as M sends Bond, along with Vesper Lynd to attend this game and prevent Le Chiffre from winning. Bond, using help from Felix Leiter, Mathis and having Vesper pose as his wife, enters the most important poker game in his already dangerous career. But if Bond defeats Le Chiffre, will he and Vesper Lynd remain safe?</overview>
<votes>1325</votes>
<rating>6.7</rating>
<tagline>Everyone has a past. Every legend has a beginning.</tagline>
<certification>PG-13</certification>
<released>2006-11-17</released>
<runtime>144</runtime>
<budget>150000000</budget>
<revenue>594786758</revenue>
<homepage>http://www.mgm.com/view/movie/233/Casino-Royale-(2006)/</homepage>
<trailer>http://www.youtube.com/watch?v=36mnx8dBbGE</trailer>
<categories>
.... OMITTED ....
</categories>
<keywords>
.... OMITTED ....
</keywords>
<studios>
.... OMITTED ....
</studios>
<languages_spoken>
<language_spoken code="en" name="English" native_name="English"/>
<language_spoken code="fr" name="French" native_name="Français"/>
</languages_spoken>
<countries>
.... OMITTED ....
</countries>
<images>
.... OMITTED ....
</images>
<cast>
.... OMITTED ...
</cast>
<version>2595</version>
<last_modified_at>2013-08-22 00:56:17 UTC</last_modified_at>
</movie>
</movies>
</OpenSearchDescription>
我的SAX正确解析XML我可以正确检索所有xml元素但是一个元素是预告元素。
这是我的java代码
public class MovieDetailHandler extends DefaultHandler {
private StringBuffer buffer = new StringBuffer();
private ArrayList<Movie> movieList;
private Movie movie;
private ArrayList<Image> movieImagesList;
private Image movieImage;
private ArrayList<String> movieCastList;
private String movieCast;
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
buffer.setLength(0);
if (localName.equals("movies")) {
movieList = new ArrayList<Movie>();
}
else if (localName.equals("movie")) {
movie = new Movie();
}
else if (localName.equals("images")) {
movieImagesList = new ArrayList<Image>();
}
else if (localName.equals("image")) {
movieImage = new Image();
movieImage.type = atts.getValue("type");
movieImage.url = atts.getValue("url");
movieImage.size = atts.getValue("size");
movieImage.width = Integer.parseInt(atts.getValue("width"));
movieImage.height = Integer.parseInt(atts.getValue("height"));
}
}
@Override
public void endElement(String uri, String localName, String qName)throws SAXException {
if (localName.equals("movie")) {
movieList.add(movie);
}
else if (localName.equals("popularity")) { // Get Popularity information
movie.popularity = buffer.toString();
}
else if (localName.equals("translated")) { // Get Translated information
movie.translated = Boolean.valueOf(buffer.toString());
}
else if (localName.equals("adult")) { // Get Adult information
movie.adult = Boolean.valueOf(buffer.toString());
}
else if (localName.equals("language")) { // Get Language information
movie.language = buffer.toString();
}
else if (localName.equals("original_name")) { // Get Original Name information
movie.originalName = buffer.toString();
}
else if (localName.equals("name")) { // Get Title information
movie.name = buffer.toString();
}
else if (localName.equals("type")) { // Get Type information
movie.type = buffer.toString();
}
else if (localName.equals("id")) { // Get Movie ID information
movie.id = buffer.toString();
}
else if (localName.equals("imdb_id")) { // Get IMDB ID information
movie.imdbId = buffer.toString();
}
else if (localName.equals("url")) { // Get TMDB URL information
movie.url = buffer.toString();
}
else if (localName.equals("votes")) { // Get Votes information
movie.votes = buffer.toString();
}
else if (localName.equals("overview")) { // Get Overview information
movie.overview = buffer.toString();
}
else if (localName.equals("rating")) { // Get Rating information
movie.rating = buffer.toString();
}
else if (localName.equals("released")) { // Get Released Date information
movie.released = buffer.toString();
}
else if (localName.equals("runtime")) { // Get Runtime information
movie.runtime = buffer.toString();
}
else if (localName.equals("trailer")) { // Get Trailer information
movie.trailer = buffer.toString();
}
else if (localName.equals("version")) {
movie.version = buffer.toString();
}
else if (localName.equals("last_modified_at")) {
movie.lastModifiedAt = buffer.toString();
}
else if (localName.equals("image")) {
movieImagesList.add(movieImage);
}
else if (localName.equals("images")) {
movie.imagesList = movieImagesList;
}
}
@Override
public void characters(char[] ch, int start, int length) {
buffer.append(ch, start, length);
}
public ArrayList<Movie> retrieveMovieList() {
return movieList;
}
我已经确定并仔细检查我是否可以在预告片元素之前以及预告片元素之后检索元素:
else if (localName.equals("budget")) {
movie.trailer = buffer.toString();
}
只是每当我试图检索拖车元素时,我都无法检索它。
注意:
我的LogCat正确地显示了上面附带的XML元素(它可以正确检索XML值)。
有谁知道为什么会这样?