Class WsMan::XmlNode
In: rwsman_xmlattr.c
Parent: Object

This class represents an XML node within the SOAP document.

Methods

attr   attr?   attr_count   child   child_count   doc   each_attr   each_child   find   method_missing   name   ns   parent   rawxml   text   to_s  

Public Instance methods

Retrieve node attribute by name.

[Source]

/*
 * call-seq:
 *   node.attr( namespace, name ) -> XmlAttr
 *
 * Retrieve node attribute by name.
 *
 */

static VALUE
xmlnode_attr( VALUE self, VALUE ns, VALUE name )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );
    char *nsUri = NIL_P( ns ) ? NULL : StringValuePtr( ns );
    char *localName = NIL_P( name ) ? NULL : StringValuePtr( name );

    WsXmlAttrH attr = ws_xml_find_node_attr( node, nsUri, localName );
    if (attr) {
        return rwsman_xmlattr_new( attr );
    }

    return Qnil;
}

Check attribute existance.

[Source]

/*
 * call-seq:
 *   node.attr?( namespace, name )
 *
 * Check attribute existance.
 *
 */

static VALUE
xmlnode_attr_exists( VALUE self, VALUE ns, VALUE name )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );
    char *nsUri = NIL_P( ns ) ? NULL : StringValuePtr( ns );
    char *localName = NIL_P( name ) ? NULL : StringValuePtr( name );

    if (ws_xml_find_attr_bool( node, nsUri, localName ))
        return Qtrue;

    return Qfalse;
}

Return number of attributes.

[Source]

/*
 * call-seq:
 *   node.attr_count -> integer
 *
 * Return number of attributes.
 */

static VALUE
xmlnode_attr_count( VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );

    return INT2FIX( ws_xml_get_node_attr_count( node ) );
}

child gives access to the direct childs of an XmlNode. Either by index (to access the first, second, etc. child) or by namespace and name.

If no parameters are given, the first child is returned. If an integer parameter is given, the n‘th + 1 child ( 0 = first, 1 = second, … ) is returned. If namespace and name are given, only children matching the namespace and name are considered. In case of multiple children with identical namespace and name, the integer parameter can be used to sort them out. namespace and name can be set to nil which is interpreted as a wildcard.

[Source]

/*
 * <code>child</code> gives access to the direct childs
 * of an XmlNode.
 * Either by index (to access the first, second, etc. child)
 * or by namespace and name.
 *
 * call-seq:
 *   node.child -> XmlNode
 *   node.child( 2 ) -> XmlNode
 *   node.child( 0, namespace, name ) -> XmlNode
 *   node.child( 1, namespace, name ) -> XmlNode
 *
 * If no parameters are given, the first child is returned.
 * If an integer parameter is given, the n'th + 1 child ( 0 = first, 1 = second, ... ) is returned.
 * If namespace and name are given, only children matching the namespace and name are considered.
 * In case of multiple children with identical namespace and name, the integer parameter can be used to sort them out.
 * namespace and name can be set to <code>nil</code> which is interpreted as a wildcard.
 * 
 */

static VALUE
xmlnode_get_child( int argc, VALUE *argv, VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );
    int idx = 0;
    char *nsUri = NULL;
    char *localName = NULL;
    if (argc > 0) idx = NUM2INT( argv[0] );
    if (argc > 1) nsUri = NIL_P( argv[1] ) ? NULL : StringValuePtr( argv[1] );
    if (argc > 2) localName = NIL_P( argv[2] ) ? NULL : StringValuePtr( argv[2] );
    WsXmlNodeH child = ws_xml_get_child( node, idx, nsUri, localName );
    if (child) return rwsman_xmlnode_new( child );
    return Qnil;
}

Count number of childs in node.

[Source]

/*
 * call-seq: node.child_count -> integer
 *
 * Count number of childs in node.
 */

static VALUE
xmlnode_child_count( VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );

    return INT2FIX( ws_xml_get_child_count( node ) );
}

Returns the XML document this node belongs to.

[Source]

/*
 * call-seq: node.doc -> XmlDoc
 *
 * Returns the XML document this node belongs to.
 */

static VALUE
xmlnode_doc( VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );
    WsXmlDocH doc = ws_xml_get_node_doc( node );

    return rwsman_xmldoc_new( doc );
}

iterate over all attributes of the node.

[Source]

/*
 * call-seq:
 *   node.each_attr { |attr| ... }
 *
 * iterate over all attributes of the node.
 *
 */

static VALUE
xmlnode_each_attr( VALUE self )
{
    VALUE result = Qnil;
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );
    int count = ws_xml_get_node_attr_count( node );
    int i = 0;
    for (; i < count; i++) {
        WsXmlAttrH attr = ws_xml_get_node_attr( node, i );
        if (!attr) {
            rb_raise( rb_eArgError, "ws_xml_get_node_attr failed" );
        }
        result = rb_yield( rwsman_xmlattr_new( attr ) );
        if (result == Qfalse)
            break;
    }
    return result;
}

[Source]

/*
 * call-seq:
 *   node.each_child { |child| ... } -> nil
 *
 */

static VALUE
xmlnode_each_child( VALUE self )
{
    VALUE result = Qnil;
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );
    ws_xml_enum_children( node, _xmlnode_each_child_callback, &result, 0 );
    return result;
}

[Source]

/*
 * call-seq:
 *   node.find ( namespace, name, recursive_p )
 *
 */

static VALUE
xmlnode_find( VALUE self, VALUE nsuri, VALUE lclname, VALUE recursive )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );
    char *nsUri = NIL_P( nsuri ) ? NULL : StringValuePtr( nsuri );
    char *localName = NIL_P( lclname ) ? NULL : StringValuePtr( lclname );

    WsXmlNodeH child = ws_xml_find_in_tree( node, nsUri, localName, RTEST( recursive ) );
    if (child) return rwsman_xmlnode_new( child );
    return Qnil;
}

method_missing is used to access node children by name.

[Source]

/*
 * <code>method_missing</code> is used to access node children by name.
 *
 */

static VALUE
xmlnode_method_missing( int argc, VALUE *argv, VALUE self )
{
#if 0
{
fprintf( stderr, "xmlnode_method_missing\n" );
fprintf( stderr, "xmlnode_method_missing( argc = %d )\n", argc );
int _argc = argc;
VALUE *_argv = argv;
while (_argc-- > 0)
fprintf( stderr, "xmlnode_method_missing( %s )\n", rb_obj_classname( *_argv++ ) );
}

Return name of node.

[Source]

/*
 * call-seq:
 *   node.name -> string
 *
 * Return name of node.
 */

static VALUE
xmlnode_name( VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );

    return rb_str_new2( ws_xml_get_node_local_name( node ) );
}

Return namespace of node.

[Source]

/*
 * call-seq:
 *   node.ns -> string
 *
 * Return namespace of node.
 */

static VALUE
xmlnode_ns( VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );

    return rb_str_new2( ws_xml_get_node_name_ns( node ) );
}

Returns the parent of the node within the XML document.

[Source]

/*
 * call-seq: node.parent -> XmlNode
 *
 * Returns the parent of the node within the XML document.
 */

static VALUE
xmlnode_parent( VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );

    return rwsman_xmlnode_new( ws_xml_get_node_parent( node ) );
}

Returns XML representation of the node as string.

[Source]

/*
 * call-seq: node.rawxml -> string
 *
 * Returns XML representation of the node as string.
 */

static VALUE
xmlnode_rawxml( VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );
    char *buf = NULL; int size;

    ws_xml_dump_memory_node_tree( node, &buf, &size );
    return rb_str_new( buf, size );
}

Returns a string representation of the nodes contents. If the node reports xsi:nil = true as one of its attributes, nil is returned.

[Source]

/*
 * call-seq:
 *   node.text -> nil
 *   node.text -> string
 *
 * Returns a string representation of the nodes contents.
 * If the node reports <tt>xsi:nil = true</tt> as one of its attributes, <code>nil</code> is returned.
 */

static VALUE
xmlnode_text( VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );

    if (ws_xml_find_attr_bool( node, XML_NS_SCHEMA_INSTANCE, XML_SCHEMA_NIL ))
        return Qnil;
    return rb_str_new2( ws_xml_get_node_text( node ) );
}

Returns a string representation of the nodes contents. If the node reports xsi:nil = true as one of its attributes, nil is returned.

[Source]

/*
 * call-seq:
 *   node.text -> nil
 *   node.text -> string
 *
 * Returns a string representation of the nodes contents.
 * If the node reports <tt>xsi:nil = true</tt> as one of its attributes, <code>nil</code> is returned.
 */

static VALUE
xmlnode_text( VALUE self )
{
    WsXmlNodeH node = rwsman_xmlnode_unpack( self );

    if (ws_xml_find_attr_bool( node, XML_NS_SCHEMA_INSTANCE, XML_SCHEMA_NIL ))
        return Qnil;
    return rb_str_new2( ws_xml_get_node_text( node ) );
}

[Validate]