Getting the data between XML tags in Fantom

62 views Asked by At

I am having some trouble with the XParser class in Fantom, I am trying to get at the data between the tags in XML. There's one that gets the type of data but I'm having trouble with the one that will get the actual data. Thank you!

1

There are 1 answers

0
Steve Eynon On BEST ANSWER

An example of what you're trying to achieve would be useful (like an XML snippet showing the data you're trying to isolate), as the question is not too clear.

The default means to selecting XML in Fantom is pretty basic and involves you traversing lists of immediate child nodes. Specifically see XElem.elems() and XElem.elem(Str name).

Example usage looks like:

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
    }
}

If you are familiar with using CSS selectors to find XML then you may like to try Sizzle from Alien-Factory:

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
    }
}