2013-01-13 72 views
0

所以我有我的XML文件加載as3,我可以看到它正確加載,它是正確的文件,因爲我可以跟蹤它,但是當我嘗試跟蹤特定節點時,我的輸出窗口仍然空着。這裏是我的代碼:無法訪問as3中的XML節點

var _textes:XML; 
loader = new URLLoader(); 
requete = new URLRequest("texte_fr.xml"); 
loader.load(requete); 
loader.addEventListener(Event.COMPLETE, finChargement); 

function finChargement(pEvt:Event){ 
    _textes= new XML(pEvt.target.data); 
} 

如果我跟蹤_textes,我可以看到我所有的XML代碼,但只要我嘗試跟蹤我的XML文件中的一個節點,我什麼都看不到。例如,如果我試圖追蹤_textes.instructions,則什麼都不會出現。我究竟做錯了什麼? 這裏是我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<textes version="1" xmlns="http://xspf.org/ns/0/"> 
<instructions> 
    Some text 
</instructions> 
<niveau1> 
    <reussi> 
     Some other text 
    </reussi> 
    <fail> 
     Some other text 
    </fail> 
</niveau1> 
<niveau2> 
    <reussi> 
     Some other text 
    </reussi> 
    <fail> 
     Some other text 
    </fail> 
</niveau2> 
<niveau3> 
    <reussi> 
     Some other text 
    </reussi> 
    <fail> 
     Some other text 
    </fail> 
</niveau3> 
<perdu> 
    Some other text 
</perdu> 
<general> 
    Some other text 
</general> 
<boutons> 
    Some other text 
</boutons> 
</textes> 

回答

0

編輯: @fsbmain打我給它一個妥善的解決辦法,而我是編輯我的回答:)這裏是我的修訂答案反正...

你沒有獲取該節點的內容,因爲您沒有使用xml文檔的名稱空間引用該節點。

這是你應該如何訪問XML節點:

function finChargement(pEvt:Event){ 
    _textes= new XML(pEvt.target.data); 
    // The namespace in your xml document: 
    var ns:Namespace = new Namespace("http://xspf.org/ns/0/"); 
    default xml namespace = ns; 
    trace(_textes.instructions); 
} 

欲瞭解更多信息請參閱this page

命名空間用來分隔或識別數據。在XML中,它們被用於 以將一個或多個節點與某個URI(統一資源 標識符)相關聯。具有名稱空間的元素可以具有與其他標記相同的標記名稱,但由於它們與URI的關聯關係而仍然與它們分開。

下面是一個例子:

說你有這個XML數據:

<?xml version="1.0" encoding="utf-8"?> 
<textes> 
    <instructions xmlns="http://xspf.org/ns/0/"> 
     Node 1 
    </instructions> 
    <instructions xmlns="http://xspf.org/ns/1/"> 
     Node 2 
    </instructions> 
</textes> 

如果您訪問使用的第一個節點的命名空間「指令」節點,你會得到第一節點的內容。

function finChargement(pEvt:Event){ 
    _textes= new XML(pEvt.target.data); 
    var ns:Namespace = new Namespace("http://xspf.org/ns/0/"); 
    default xml namespace = ns; 
    trace(_textes.instructions); 
    // Will output "Node 1" 
} 

如果使用第二個節點的命名空間,你會得到第二個節點的內容:

function finChargement(pEvt:Event){ 
    _textes= new XML(pEvt.target.data); 
    var ns:Namespace = new Namespace("http://xspf.org/ns/1/"); 
    default xml namespace = ns; 
    trace(_textes.instructions); 
    // Will output "Node 2" 
} 
+0

請參閱我的修訂答案。 – bwroga

0

您可以設置默認的命名空間,以便使用E4X清空無這段代碼指定命名空間: var xml:XML =部分文字

var ns:Namespace = xml.namespace(""); 
    default xml namespace = ns; 
    trace(xml.instructions.toString()); //output "Some text"