2014-12-04 63 views

回答

0

你試圖實現的一個例子是有用的(比如一個XML片段顯示你試圖隔離的數據),因爲問題不太清楚。

在Fantom中選擇XML的默認方法是非常基本的,並且涉及遍歷直接子節點的列表。具體見XElem.elems()XElem.elem(Str name)

使用例子是這樣的:

using xml 

class Example { 
    Void main() { 
     root := XParser("<root> 
          <thingy> 
           <wotsit>My Text</wotsit> 
          </thingy> 
         </root>".in).parseDoc.root 

     // find by traversing element lists 
     wotsit := root.elems[0].elems[0] 
     echo(wotsit.writeToStr) // --> <wotsit>My Text</wotsit> 

     // find by element name 
     wotsit = root.elem("thingy").elem("wotsit") 
     echo(wotsit.writeToStr) // --> <wotsit>My Text</wotsit> 

     // get wotsit text 
     echo(wotsit.text.val) // --> My Text 
    } 
} 

如果你熟悉使用CSS選擇器來查找XML,那麼你可能想嘗試Sizzle了Alien-廠:

using xml 
using afSizzle 

class Example { 
    Void main() { 
     sizzleDoc := SizzleDoc("<root><thingy><wotsit/></thingy></root>") 

     // find by CSS selector 
     wotsit = sizzleDoc.select("wotsit").first 
     echo(wotsit.writeToStr) // --> <wotsit>My Text</wotsit> 

     // get wotsit text 
     echo(wotsit.text.val) // --> My Text 
    } 
} 
+0

謝謝!這解決了這個問題。 – 2014-12-06 01:16:30