Go to main content

xml

xml functions

XML

NAME

xml - xml functions

SYNOPSIS

xml_attr xml_child xml_content xml_each xml_free xml_hash_child xml_name xml_nbchild xml_new xml_next xml_parent xml_prev xml_type

DESCRIPTION

This module is based on libxml2 : http://www.xmlsoft.org/.

MODULES

 

Module xml {
        Library : "/usr/local/lib/libsnxml.so"
        Init : "snxml_init"
}

EXAMPLES

res=<{
        %include "/includes/xml.sn";
        "A1="; XML_ELEMENT_NODE;
        "A2="; XML_ELEMENT_NODE;
}>. return res=A1=1A2=1.)

res=<{
        xml=xml_new(error:t,file:"/html/test.xml");
        %include "/includes/xml.sn";

        function xml_traverse(node)
                arr = [];
                for(cur_node = node; cur_node != null; cur_node = cur_node.xml_next())
                        if cur_node.xml_type() == XML_ELEMENT_NODE then
                                name = cur_node.xml_name();
                                t = xml_traverse(cur_node.xml_child(
                                a = cur_node.xml_attr();
                                arr.push({name:t, attr:a});
                        else
                                textval = cur_node.xml_text();
                                if ! isspace(textval) then
                                        arr.push({'text':textval});
                                endif
                        endif
                endfor
                return arr;
        endf

        xml_traverse(xml);
        xml_free(xml);
}>. return res=\[{"attr":\[\],"sncode":\[{"fn":\[{"text":"laplante"}\],"attr":\[{"id":"1"},{"class":"pierre"}\]},{"fn":\[{"text":"bilodeau"}\],"attr":\[{"id":"2"},{"class":"chantal"}\]}\]}\].)

res=<{xml_new(); }>. return .*XML or file must be specified.*
res=<{xml_new(file:"a",xml:"b"); }>. return .*XML or file must be specified, not both.*
res=<{xml_new(error:t,xml:""); t.errcode; t.errmsg;}>. return res=21At line=1,errcode=21 Invalid XML..
res=<{xml=xml_new(error:t,file:"/html/cyberpresse.xml"); t.errcode; t.errmsg;}>. return res=0.
res=<{xml=xml_new(error:t,xml:'  a'); t.errcode; t.errmsg;}>. return res=0.

res=<{xml=xml_new(error:t,file:"/html/cyberpresse.xml"); xml_free(xml:xml); t.errcode}>. return res=0.
res=<{xml=xml_new(error:t,file:"/html/cyberpresse.xml"); xml_free(xml); t.errcode}>. return res=0.
res=<{xml=xml_new(error:t,file:"/html/cyberpresse.xml"); xml_free(1); t.errcode}>. return .*is not valid in xml_free.*

res=<{xml=xml_new(error:t,file:"/html/cyberpresse.xml"); xml_type(xml); xml_name(xml); t.errcode}>. return res=1mediafilm0.
res=<{xml=xml_new(error:t,file:"/html/cyberpresse.xml");
        %include "/includes/xml.sn";
        "1="; xml_name(xml);                    // mediafilm
        child = xml_child(xml);
        child = skip_text_node(child);
        "2="; xml_name(child);                  // films
        child = xml_child(child);
        child = skip_text_node(child);
        xml_attr(child, attr:"id");             // film id="18245"
        child = xml_child(child);
        node = skip_text_node(child);
        while node != null do
                name = xml_name(node);                          // childs of film
                child = xml_child(node);
                if child != null then
                        if name eq "annee" then
                                xml_text(child);
                        endif
                endif
                node = xml_next(node);          // next node of film
        endw
}>. return res=1=mediafilm2=films182451987.

res=<{xml=xml_new(error:t,file:"/html/cyberpresse.xml");
        %include "/includes/xml.sn";
        "1="; xml_name(xml);                    // mediafilm
        child = xml_child(xml);
        child = skip_text_node(child);
        "2="; xml_name(child);                  // films
        child = xml_child(child);
        child = skip_text_node(child);
        xml_attr(child, attr:"id");             // film id="18245"
        child = xml_child(child);
        node = skip_text_node(child);
        xml_name(node);                         // maj
        node = xml_next(node);
        node = skip_text_node(node);
        xml_name(node);                         // id_cg
        node = xml_prev(node);
        while xml_type(node) == XML_TEXT_NODE do
                node = xml_prev(node);
        endw
        xml_name(node);                         // maj
        node = xml_parent(node);
        xml_name(node);                         // film
        xml_free(xml);
}>. return res=1=mediafilm2=films18245majid_cgmajfilm.

res=<{xml=xml_new(error:t,file:"/html/cyberpresse.xml");
        %include "/includes/xml.sn";
        child = xml_child(xml);                  // mediafilm
        child = skip_text_node(child);
        child = xml_child(child);
        child = skip_text_node(child);
        xml_name(child);
        xml_nbchild(child);
        xml_nbchild(child,text_node:true);
        child.xml_has_child();
        xml_free(xml);
}>. return res=film1837true.

res=<{xml=xml_new(error:t,file:"/html/cyberpresse.xml");
        %include "/includes/xml.sn";

        function xml_traverse(node)
                nbnode = 0;
                nbfilm = 0;
                for(cur_node = node; cur_node != null; cur_node = cur_node.xml_next())
                        if cur_node.xml_type() == XML_ELEMENT_NODE then
                                if (cur_node.xml_name() eq "film") then
                                        nbfilm++;
                                endif
                                nbnode++;
                        endif
                        t = xml_traverse(cur_node.xml_child(
                        nbnode += t[0];
                        nbfilm += t[1];
                endfor
                return [ nbnode, nbfilm ];
        endf

        xml_traverse(xml);
        xml_free(xml);
}>. return res=\[13482,1131\].

SEE ALSO

AUTHOR

Written by Pierre Laplante, <laplante@sednove.com>

MODIFICATIONS

1.0 2014-09-09 21:24:14 laplante@sednove.com

Edit

© 2024 extenso Inc. All rights reserved.