简体   繁体   中英

parsing XML file with jdom+eclipse

I have a problem while parsing an xml file. this is a portion of this file:

<item>
  <title>Andrew of Auditory Implant</title>
  <link>http://www.flickr.com/photos/mike-burns/65651809/</link>
  <description>&lt;p&gt;&lt;a href="http://www.flickr.com/people/mike-burns/"&gt;Mike Burns&lt;/a&gt; a posté une photoÂ*:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.flickr.com/photos/mike-burns/65651809/" title="Andrew of Auditory Implant"&gt;&lt;img src="http://farm1.staticflickr.com/31/65651809_0935d9acbf_m.jpg" width="180" height="240" alt="Andrew of Auditory Implant" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;November 20th, 2005 @ Middle East Upstairs.&lt;/p&gt;</description>
  <pubDate>Mon, 21 Nov 2005 14:25:17 -0800</pubDate>
  <dc:date.Taken>2005-11-20T22:24:30-08:00</dc:date.Taken>
  <author flickr:profile="http://www.flickr.com/people/mike-burns/">nobody@flickr.com (Mike Burns)</author>
  <guid isPermaLink="false">tag:flickr.com,2004:/photo/65651809</guid>
  <media:content url="http://farm1.staticflickr.com/31/65651809_0935d9acbf_b.jpg" type="image/jpeg" height="1024" width="766" />
  <media:title>Andrew of Auditory Implant</media:title>
  <media:description type="html">&lt;p&gt;November 20th, 2005 @ Middle East Upstairs.&lt;/p&gt;</media:description>
  <media:thumbnail url="http://farm1.staticflickr.com/31/65651809_0935d9acbf_s.jpg" height="75" width="75" />
  <media:credit role="photographer">Mike Burns</media:credit>
  <media:category scheme="urn:flickr:tags">upcoming40467 auditoryimplant middleeastupstairs upcomingorg upcomingevent</media:category>
  <creativeCommons:license>http://creativecommons.org/licenses/by-sa/2.0/deed.fr</creativeCommons:license>
</item>

the problem is in this line:

<media:category scheme="urn:flickr:tags">upcoming40467 auditoryimplant middleeastupstairs upcomingorg upcomingevent</media:category>

this is my java code:

SAXBuilder sxb = new SAXBuilder();
    try
    {
    //On crée un nouveau document JDOM avec en argument le fichier XML


    document = sxb.build(new File(fileName));

      racine = document.getRootElement().getChild("channel");

      System.out.println( "racine"+racine);
      List listPhotos = racine.getChildren("item");
      System.out.println (listPhotos.size());
    //On crée un Iterator sur notre liste
      Iterator j = listPhotos.iterator();
      while(j.hasNext())
      {


         Element courant = (Element)j.next();

         System.out.println("courant:="+courant);


    String Title =   courant.getChild("title").getValue();
         System.out.println("Title:="+Title);
    String  description=     courant.getChild("description").getValue();
         System.out.println("description:="+description);
    //String  tags=  courant.getChild("media:category").getValue();
         //System.out.println("media:category:="+tags);
         System.out.println(courant.getChild("media:category"));
.....

it works for title and description but not for media:category. when I do:

System.out.println(courant.getChild("media:category"));

I found:

null Could anyone help me?

I have not tested this (because I don't use JDom usually) but you have to care for XML namespaces, you can't just ignore them or treat namespace+:+localname as name. That said it seems that Element has a namespace aware getChild method .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM