haXe

Using Xml

This code parses an XML string str into an object structure x, and then accesses properties of the object to build a new string out :

    var str = "<hello where=\"world\">haXe</hello>";
    var x : Xml = Xml.parse(str).firstElement();
    var out = x.nodeName + " " + x.get("where")+" "+x.firstChild().nodeValue;

The difference between firstChild and firstElement is that the second function will return the first child with the type Xml.Element. You can as well use other methods to iterator either over children or elements :

    for( child in xml ) {
        // iterate all childs
    }
    for( elt in xml.elements() ) {
        // iterate all elements
    }
    for( user in xml.elementsNamed("user") ) {
        // iterate all elements with a nodeName "user" 
    }
    for( att in xml.attributes() ) {
        // iterator over all attributes
    }

You can read the Xml API for complete documentation.

version #1052, modified 2008-05-02 23:13:39 by ncannasse