2012-02-25 53 views
1

我正在測試如何使用SAXParser並瞭解其組件。這裏是我用來測試我的XML文件:XML SAX:在一個示例XML文件中解釋`qName`和`localName`的結果

<?xml-stylesheet href="/externalflash/NASA_Detail/NASA_Detail.xsl" type="text/xsl"?> 

<rss version="2.0"> 
    <channel> 
     <title>NASA Image of the Day</title> 
     <link>http://www.nasa.gov/multimedia/imagegallery/index.html</link> 
     <description>The latest NASA "Image of the Day" image.</description> 
     <language>en-us</language> 
     <docs>http://blogs.law.harvard.edu/tech/rss</docs> 
     <managingEditor>[email protected]</managingEditor> 
     <webMaster>[email protected]</webMaster> 

     <item xmlns:java_code="xalan://gov.nasa.build.Utils1"> 
      <title>Expedition 30 Cosmonauts Perform Spacewalk</title> 
      <link>http://www.nasa.gov/multimedia/imagegallery/image_feature_2181.html</link> 
      <description>This image of Russian cosmonauts Oleg Kononenko and Anton Shkaplerov, both Expedition 30 flight engineers, was taken during a spacewalk on Thursday, Feb. 16, 2012. During the six-hour, 15-minute spacewalk, Kononenko and Shkaplerov moved the Strela-1 crane from the Pirs Docking Compartment in preparation for replacing it in 2012 with a new laboratory and docking module. The duo used another boom, the Strela-2, to move the hand-operated crane to the Poisk module for future assembly and maintenance work. Both telescoping booms extend like fishing rods and are used to move massive components outside the station. On the exterior of the Poisk Mini-Research Module 2, they also installed the Vinoslivost Materials Sample Experiment, which will investigate the influence of space on the mechanical properties of the materials. The spacewalkers also collected a test sample from underneath the insulation on the Zvezda Service Module to search for any signs of living organisms. Both spacewalkers wore Russian Orlan spacesuits bearing blue stripes and equipped with NASA helmet cameras. Image Credit: NASA</description> 
      <guid isPermaLink="true">http://www.nasa.gov/multimedia/imagegallery/image_feature_2181.html</guid> 
      <pubDate>Wed, 22 Feb 2012 00:00:00 EST</pubDate> 
      <enclosure length="1234567" type="image/jpeg" url="http://www.nasa.gov/images/content/624856main_image_2181_516-387.jpg"/> 
     </item> 
    </channel> 
</rss> 

當我使用的SAXParser來測試這個文件,這是我輸出的一些行:

start Element: rss 
Local Name // this line I don't know why null 
qNam: rss 
start characters: 

start Element: channel 
Local Name 
qNam: channel 
start characters: 

我的問題是:在我的XML文件,沒有namespace所以每個元素將屬於它的默認namespace,並且爲什麼qName將等於local Name(因爲沒有prefixqName)。

但是在上面的結果中,在元素rsschannel(以及我還沒有發佈的所有其他)中,local Name爲空!

誰能爲我解釋一下。

謝謝:)

+0

它不是「空」,它是空白的。重要的區別。 – skaffman 2012-02-27 14:41:12

回答

3

這是在Javadoc稍微不鮮明:

URI - 在空間URI,如果元素沒有名稱空間URI,如果不是正在執行或名稱空間處理,空字符串

的localName - 本地名稱(沒有前綴),或者如果沒有正在執行名稱空間處理

QNAME空字符串 - 合格域名(前綴),或第如果限定名不可用,則爲空字符串

atts - 附加到元素的屬性。如果沒有屬性,它應該是一個空的屬性對象。這個對象的startElement返回後的值是未定義的

隨着名稱空間處理關斷時,僅qName被返回(無前綴)。

+0

是的,qName參數的描述很糟糕。這種解釋可能來自他們在那裏的描述,但其理解將受益於標點符號的變化 – 2012-02-27 14:41:02

+0

也許:「qName - 限定名稱(帶有前綴,如果限定名稱不可用,則爲空字符串)」 – 2012-02-27 14:41:52