0

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要素を正しく取得できますが、トレーラー要素である要素が1つあります。

これは私の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値を正しく取得できます)。

これがなぜなのか誰か知っていますか?

4

0 に答える 0