This discussion is archived
10 Replies Latest reply: Sep 22, 2011 4:32 PM by 796440 RSS

select from xml

user11138293 Newbie
Currently Being Moderated
I have an xml and I want to do some select from this xml

here is my xml
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
  <entry>
    <media:group>
      <media:player url='http://www.youtube.com/watch?v=DzFeYRXDoig&feature=youtube_gdata_player'/>
      <media:title type='plain'>Anaganaga o Ammai Full Length Telugu Movie Sri kanth Soundarya</media:title>
      <yt:duration seconds='9199'/>
      <media:license type='text/html' href='http://www.youtube.com/t/terms'>youtube</media:license>
    </media:group>
  </entry>
  <entry>
    <media:group>
      <media:player url='http://www.youtube.com/watch?v=8_nUQEMjFY0&feature=youtube_gdata_player'/>
      <media:title type='plain'>Dubai Seenu - Full Length Telugu Movie - Ravi Teja - Nayana Tara</media:title>
      <yt:duration seconds='9423'/>
      <media:license type='text/html' href='http://www.youtube.com/t/terms'>youtube</media:license>
    </media:group>
  </entry>
  <entry>
    <media:group>
      <media:player url='http://www.youtube.com/watch?v=DDTykVPUTlE&feature=youtube_gdata_player'/>
      <media:title type='plain'>Pelli Kani Prasad - Full Length Telugu Movie - Allari Naresh - Sridevi</media:title>
      <yt:duration seconds='8215'/>
      <media:license type='text/html' href='http://www.youtube.com/t/terms'>youtube</media:license>
    </media:group>
  </entry>
  <entry>
    <media:group>
      <media:player url='http://www.youtube.com/watch?v=UuuqPznF5qE&feature=youtube_gdata_player'/>
      <media:title type='plain'>Andhrawala - NTR - Rakshitha - Full Length Telugu Movie</media:title>
      <yt:duration seconds='9546'/>
      <media:license type='text/html' href='http://www.youtube.com/t/terms'>youtube</media:license>
    </media:group>
  </entry>
  <entry>
    <media:group>
      <media:player url='http://www.youtube.com/watch?v=0LyMX7ZGqgs&feature=youtube_gdata_player'/>
      <media:title type='plain'>G - Full Length Telugu Movie - Ajith - Trisha</media:title>
      <yt:duration seconds='8961'/>
      <media:license type='text/html' href='http://www.youtube.com/t/terms'>youtube</media:license>
    </media:group>
  </entry>
</feed>
this xml has feed tag as root , it has multiple entry nodes each entry node has a medig:group and this has yt:duration



I want to select only the entry tags which has <yt:duration seconds='8961'/>

Please advice me how to select from xml , I tried using xpath but not sure If I can use it to select nodes from xml.

in my case i want to select all entry nodes whose child elment medis:group/yt:duration attribute seconds has a value equal to 8961 , please advice me how to do this.
  • 1. Re: select from xml
    EJP Guru
    Currently Being Moderated
    I tried using xpath but not sure If I can use it to select nodes from xml.
    Well that's what it's for. It doesn't do anything else except select from XML.

    What did you try?
  • 2. Re: select from xml
    user11138293 Newbie
    Currently Being Moderated
    this is what I tried as an example
         public static void parser(String xml)  throws Exception{
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              factory.setNamespaceAware(true); // never forget this!
              DocumentBuilder builder = factory.newDocumentBuilder();
              Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes())));
              XPathFactory xPathFactory= XPathFactory.newInstance();
              XPath xpath = xPathFactory.newXPath(d);
              XPathExpression expr = xpath.compile("/feed/entry");
              Object result = expr.evaluate(doc, XPathConstants.NODESET);
              System.out.println(result);
              NodeList nodes = (NodeList) result;
             for (int i = 0; i < nodes.getLength(); i++) {
                 System.out.println(nodes.item(i).getNodeValue()); 
             }          
         }
    I am assuming this code should give me all the entry nodes , but result returned from expr.evaluate does not contain any children , I dont know what what am I doing wrong.

    the xpath expression to find all seconds with a particular value is //yt:duration[@seconds=1764] right ?
  • 3. Re: select from xml
    user11138293 Newbie
    Currently Being Moderated
    I also tried Jdom not getting any results back

    my code
              org.jdom.input.SAXBuilder saxBuilder = new org.jdom.input.SAXBuilder();
              org.jdom.Document jdomDocument =saxBuilder.build(new InputSource(new ByteArrayInputStream(xml.getBytes())));
              java.util.List nodeList =org.jdom.xpath.XPath.selectNodes(jdomDocument, "//entry");
              
              Iterator iter=nodeList.iterator();
              while(iter.hasNext()) {
                  org.jdom.Element element = 
                      (org.jdom.Element) iter.next();
                  System.out.println(element);
              }
    the xpath expression I used is to select all entry nodes from the xml , I am doing something wrong for which the nodelist returned is always blank , please help me tell me, what am I doing wrong.
  • 4. Re: select from xml
    EJP Guru
    Currently Being Moderated
    the xpath expression to find all seconds with a particular value is //yt:duration[@seconds=1764] right ?
    That's correct, or /feed/yt:duration[@seconds=1764] as you had before, but you have to jump through some hoops to tell XPath and your parser about the namespaces you're using. For the moment strip out all the namespaces from your test XML file and get it working.
  • 5. Re: select from xml
    user11138293 Newbie
    Currently Being Moderated
    Thanks alot for your reply , can you please help me add the namespace , I cannot strip , becasue the src of the xml comes with all these namespaces ,

    I did this in my code
              org.jdom.input.SAXBuilder saxBuilder = new org.jdom.input.SAXBuilder();
              org.jdom.Document jdomDocument =saxBuilder.build(new InputSource(new ByteArrayInputStream(xml.getBytes())));
              XPath  xPath=XPath.newInstance("//entry");
              xPath.addNamespace("media", "http://search.yahoo.com/mrss/");
              xPath.addNamespace("yt", "http://gdata.youtube.com/schemas/2007");
              
              java.util.List nodeList =xPath.selectNodes(jdomDocument);
              
              Iterator iter=nodeList.iterator();
              while(iter.hasNext()) {
                  org.jdom.Element element = (org.jdom.Element) iter.next();
                  System.out.println(element);
              }
    still it did not work.
  • 6. Re: select from xml
    EJP Guru
    Currently Being Moderated
    There's more to it than that. You have to tell the parser to use namespaces (DocumentBuilderFactory.setNamespaceAware(true) in javax.xml.parsers, don't know in the JDOM package).

    You should also take my suggestion to strip this one file of namespaces for test purposes so you can at least get the rest of the code working separately.
  • 7. Re: select from xml
    796440 Guru
    Currently Being Moderated
    EJP wrote:
    You should also take my suggestion to strip this one file of namespaces for test purposes so you can at least get the rest of the code working separately.
    <rant>
    This has been a major pet peeve of mine lately. It comes up here a lot of course; it can be hard to convince less experienced developers that it's okay to ignore certain requirements temporarily during development in order to be able to test, debug, and prove smaller pieces individually. What really stomps my grapes though is when certain allegedly "senior" developers that I work with argue against it on the grounds that, "We can't release that way, so we daren't test that way."
    </rant>
  • 8. Re: select from xml
    EJP Guru
    Currently Being Moderated
    "We can't release that way, so we daren't test that way."
    Of course not, but you can sure develop that way. Piecewise. Stepwise refinement. From the known to the unknown.
  • 9. Re: select from xml
    DrClap Expert
    Currently Being Moderated
    EJP wrote:
    There's more to it than that. You have to tell the parser to use namespaces and I think you also have to load them into it somehow. I can't access my code right now but you should look into both those things.
    With the setNamespaceContext method of the XPath object.

    Unfortunately you then have to be able to create a NamespaceContext object, which requires writing your own class which implements the interface -- yeah, I know you know all of this, EJP, I'm just making a general statement here -- and override the methods to hard-code whatever namespace URI corresponds to a prefix and vice-versa. You'd think they could have made a convenience class for this and included it in the standard API, but no, everybody writes their own quick-and-dirty implementation twice and then decides there should be a convenience class.

    (End of my rant.)
  • 10. Re: select from xml
    796440 Guru
    Currently Being Moderated
    EJP wrote:
    "We can't release that way, so we daren't test that way."
    Of course not, but you can sure develop that way. Piecewise. Stepwise refinement. From the known to the unknown.
    I meant "can't test +in development+ that way in order to narrow down where the problem is or to see if this approach is totally untenable from the get-go", which is nonsense of course, but some so-called senior folks don't seem to get that.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points