0

Fantom の XParser クラスで問題が発生しています。XML のタグ間のデータを取得しようとしています。データの型を取得するものがありますが、実際のデータを取得するものに問題があります。ありがとうございました!

4

1 に答える 1

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 を検索することに慣れている場合は、Alien-Factory のSizzleを試してみてください。

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
    }
}
于 2014-12-04T10:39:56.177 に答える