1

I have the following xml file from vimeo: http://vimeo.com/api/v2/video/21331554.xml

I'm trying to extract the thumbnail-medium with the following code:

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();

But i get "undefined" as a return

1 Answer 1

1

You cannot cast a NodeList to Node so this line:

return ((Node) doc.getElementsByTagName("thumbnail_medium")).getNodeValue();

throws a ClassCastException. So you need to get the only item in the NodeList and get its text value with this line:

return doc.getElementsByTagName("thumbnail_medium").item(0).getTextContent();

I tested this with this two methods:

@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);

}

And

@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);

}

The first throws a ClassCastException and the second prints http://b.vimeocdn.com/ts/137/151/137151977_200.jpg I think that is the value you are looking for.

Also, how did you read from a File object passing a a URL?

Sign up to request clarification or add additional context in comments.

7 Comments

Oh thank you so much for taking the time to give me that detailed answer, I will check this tomorrow. And yes just noticed this as well with the URL and File I didn't know :)
I have some trouble with the ClassPathResource, i don't know from with package this class is from (i also use GAE)
ok i replaced ClassPathRessource by URL() and it's working fine for me now, thanks again ;)
Nice. Can you post the code to load the resource from a URL? It would como handy to know it
Sure i just made the edit to your last answer, thanks again Mael i really appreciate your help and i learned some stuff in the same time ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.