0

我正在尝试从 google api 解析数据:
http ://gdata.youtube.com/feeds/base/videos?q=kittens&client=ytapi-youtube-search&v=2&start-index=1

像这样

try {

        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(new InputSource(new StringReader(xml.trim())));


} catch (ParserConfigurationException e) {
            System.out.println("XML parse error: " + e.getMessage());
            return null;
} catch (SAXException e) {
            System.out.println("Wrong XML file structure: " + e.getMessage());
            return null;
} catch (IOException e) {
            System.out.println("I/O exeption: " + e.getMessage());
            return null;
}

并不断收到“SAXException”..我做错了什么?

准确的错误消息是:I/System.out(19026): Wrong XML file structure: Unexpected token (position:TEXT null@1:104174 in java.io.StringReader@410c2448)

4

1 回答 1

0

我做了这个,它对我有用:

public static void readGDATA(){

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = null;
      Document doc = null;

      try {

        db = dbf.newDocumentBuilder();
        doc = db.parse(new URL("http://gdata.youtube.com/feeds/base/videos?q=kittens&client=ytapi-youtube-search&v=2&start-index=1").openStream());
        System.out.println(doc.getElementsByTagName("name").getLength());



      } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 


}

控制台中的输出:

26

于 2013-02-04T16:58:03.777 回答