我有来自vimeo的以下xml文件:http://vimeo.com/api/v2/video/21331554.xml
我正在尝试使用以下代码提取缩略图媒体:
File fXmlFile = new File("http://vimeo.com/api/v2/video/" + linkId + ".xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
return ((Node) doc.getElementsByTagName("thumbnail_medium")).getNodeValue();
但我得到“未定义”作为回报
答案 0 :(得分:1)
return ((Node) doc.getElementsByTagName("thumbnail_medium")).getNodeValue();
抛出ClassCastException。因此,您需要获取NodeList中的唯一项目并使用此行获取其文本值:
return doc.getElementsByTagName("thumbnail_medium").item(0).getTextContent();
我用这两种方法测试了这个:
@Test
public void domTestVimeo() throws ParserConfigurationException,
SAXException, IOException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new ClassPathResource("vimeo.xml")
.getInputStream());
doc.getDocumentElement().normalize();
String val = ((Node) doc.getElementsByTagName("thumbnail_medium"))
.getNodeValue();
System.out.println(val);
}
和
@Test
public void yourTest() throws ParserConfigurationException, SAXException,
IOException {
// File fXmlFile = new File("http://vimeo.com/api/v2/video/" + 21331554
// + ".xml");
InputStream is = new ClassPathResource("vimeo.xml").getInputStream();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
String val = doc.getElementsByTagName("thumbnail_medium").item(0)
.getTextContent();
System.out.println(val);
}
第一个抛出ClassCastException,第二个抛出http://b.vimeocdn.com/ts/137/151/137151977_200.jpg
我认为这是你要找的值。
另外,您是如何从传递URL的File对象中读取的?