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 回答