解析xml文件时遇到问题。 这是此文件的一部分:
<item>
<title>Andrew of Auditory Implant</title>
<link>http://www.flickr.com/photos/mike-burns/65651809/</link>
<description><p><a href="http://www.flickr.com/people/mike-burns/">Mike Burns</a> a posté une photoÂ*:</p>
<p><a href="http://www.flickr.com/photos/mike-burns/65651809/" title="Andrew of Auditory Implant"><img src="http://farm1.staticflickr.com/31/65651809_0935d9acbf_m.jpg" width="180" height="240" alt="Andrew of Auditory Implant" /></a></p>
<p>November 20th, 2005 @ Middle East Upstairs.</p></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"><p>November 20th, 2005 @ Middle East Upstairs.</p></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>
问题出在这一行:
<media:category scheme="urn:flickr:tags">upcoming40467 auditoryimplant middleeastupstairs upcomingorg upcomingevent</media:category>
这是我的java代码:
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"));
.....
它适用于标题和描述,但不适用于媒体:类别。 当我这样做时:
System.out.println(courant.getChild("media:category"));
我找到了:
空 有谁可以帮助我?
答案 0 :(得分:0)
我没有测试过这个(因为我通常不使用JDom)但是你必须关心XML命名空间,你不能只是忽略它们或者将namespace +:+ localname视为名称。这表示Element似乎有namespace aware getChild
method。