我有下面的XML,我試圖打印一些節點的值。例如,下面的代碼我想打印Java:解析XML文件時出現問題
NodeList list = doc.getElementsByTagName("photo");
element = (Element)list.item(0);
list = element.getChildNodes();
System.out.println(list.item(0).getNodeName());
System.out.println(list.item(0).getNodeValue());
,我得到
null
#text
,而不是「標題」和「的Bigfish活200812」
我在做什麼錯? 感謝
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photo id="2882550369" secret="21054282c8" server="3106" farm="4" dateuploaded="1222202793" isfavorite="0" license="0" safety_level="0" rotation="0" views="5" media="photo">
<owner nsid="[email protected]" username="fishthemusic" realname="masayoshi yamamiya" location="kawasaki, japan" iconserver="4" iconfarm="1" />
<title>bigfish live 200812</title>
<description>photo by Kazuhiro Nakamura</description>
<visibility ispublic="1" isfriend="0" isfamily="0" />
<dates posted="1222202793" taken="2008-09-24 05:46:33" takengranularity="0" lastupdate="1222998937" />
<editability cancomment="1" canaddmeta="0" />
<publiceditability cancomment="1" canaddmeta="0" />
<usage candownload="1" canblog="1" canprint="0" canshare="1" />
<comments>0</comments>
<notes />
<tags>
<tag id="314160-2882550369-80673" author="[email protected]" raw="bigfish" machine_tag="0">bigfish</tag>
<tag id="314160-2882550369-5558" author="[email protected]" raw="live" machine_tag="0">live</tag>
<tag id="314160-2882550369-29726586" author="[email protected]" raw="upcoming:event=1167424" machine_tag="1">upcoming:event=1167424</tag>
</tags>
<urls>
<url type="photopage">http://www.flickr.com/photos/fishthemusic/2882550369/</url>
</urls>
</photo>
</rsp>
我不認爲他在getChildNodes中獲取屬性,但他正在爲空白字符獲取文本節點。 – 2011-04-06 15:36:40
XML除非在<[[CDATA]]> – 2011-04-06 15:39:54
@ Travis中忽略空格,這是不正確的。首先,屬性*不被認爲是DOM中元素節點的子元素,其次,空白在默認情況下不被忽略。 CDATA只是語法糖,它與空白沒有特別的關係。 – 2011-04-06 15:41:13