سؤال

I'm attempting to following this example of SAX XML Parser: How to parse XML using the SAX parser

However when debugging - some items are returning null. I have verified they are contained within the XML file I am connecting to - but I believe I've set my getVideos/setVideos parameters incorrectly and I'm not sure how they might be corrected.

XML Data

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<response>
<cmd>getVideos</cmd>
<success>1</success>
<NumberOfVideos>4</NumberOfVideos>
<Videos>
<Video>
<VideoName>sample_iPod</VideoName>
<VideoDesc/>
<VideoUrl>
http://mobile.example.com/omv/wp-content/uploads/sites/6/2014/01/omv/1/06087297988b.m4v
</VideoUrl>
<VideoTags/>
</Video>
<Video>
<VideoName>sample_mpeg4</VideoName>
<VideoDesc/>
<VideoUrl>
http://mobile.example.com/omv/wp-content/uploads/sites/6/2014/01/omv/1/b5ed9e7100e2.mp4
</VideoUrl>
<VideoTags/>
</Video>
<Video>
<VideoName>sample_sorenson</VideoName>
<VideoDesc/>
<VideoUrl>
http://mobile.example.com/omv/wp-content/uploads/sites/6/2014/01/omv/1/2a8e64b24997.mov
</VideoUrl>
<VideoTags/>
</Video>
<Video>
<VideoName>sample_iTunes</VideoName>
<VideoDesc/>
<VideoUrl>
http://mobile.example.com/omv/wp-content/uploads/sites/6/2014/01/omv/1/6c7f65254aad.mov
</VideoUrl>
<VideoTags/>
</Video>
</Videos>
</response>

SAX Parser Related Java:

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

    if (qName.equalsIgnoreCase("response")) {
        // add it to the list
        response.add(cmd);
    } else if (qName.equalsIgnoreCase("success")) {
        cmd.setSuccess(tempVal);
    } else if (qName.equalsIgnoreCase("numberofvideos")) {
        cmd.setNumberOfVideos(tempVal);
    } else if (qName.equalsIgnoreCase("videos")) {
        cmd.setVideos(tempVal);
    } else if (qName.equalsIgnoreCase("video")) {
        cmd.setVideo(tempVal);
    } else if (qName.equalsIgnoreCase("videoname")) {
        cmd.setVideoName(tempVal);
    } else if (qName.equalsIgnoreCase("videourl")) {
        cmd.setVideoURL(tempVal);

    }

...

public class SAXXMLParser { 
    public static List<Cmd> parse(InputStream is) {
        List<Cmd> response = null;
        try {
            // create a XMLReader from SAXParser
            XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser()
                    .getXMLReader();
            // create a SAXXMLHandler
            SAXXMLHandler saxHandler = new SAXXMLHandler();
            // store handler in XMLReader
            xmlReader.setContentHandler(saxHandler);
            // the process starts
            xmlReader.parse(new InputSource(is));
            // get the `Video list`
            response = saxHandler.getResponse();

        } catch (Exception ex) {
            Log.d("XML", "SAXXMLParser: parse() failed");
            ex.printStackTrace();
        }

        // return Laptop list
        return response;
    }
}

...

 public class Cmd {
private String success;
private String cmd;
private String videos;
private String video;
private String numberofvideos;
private String videoname;
private String videourl;


public String getCmd() {
    return cmd;
}

public void setCmd(String cmd) {
    this.cmd = cmd;
}
public String getSuccess() {
    return success;
}

public void setSuccess(String success) {
    this.success = success;
}

public String getNumberOfVideos() {
    return numberofvideos;
}
public void setNumberOfVideos(String numberofvideos) {
    this.numberofvideos = numberofvideos;
}
public String getVideos() {
    return videos;
}
public void setVideos(String videos) {
    this.videos = videos;
}
public String getVideo() {
    return video;
}
public void setVideo(String video) {
    this.video = video;
}
public String getVideoName() {
    return videoname;
}

public void setVideoName(String videoname) {
    this.videoname = videoname;
}
public String getVideoURL() {
    return videourl;
}

public void setVideoURL(String videourl) {
    this.video = videourl;
}

Please let me know if any additional information is required. I will be happy to provide it.

هل كانت مفيدة؟

المحلول

Your code sets the member video instead of videourl in setvideourl method public void setVideoURL(String videourl) { this.video = videourl;<<---- This.videourl = videourl; }

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top