/*
 * call-seq:
 *    context.node -> node
 * 
 * Obtain the root node of this context.
 */
VALUE
ruby_xml_parser_context_node_get(VALUE self) {
  ruby_xml_parser_context *rxpc;
  Data_Get_Struct(self, ruby_xml_parser_context, rxpc);

  if (rxpc->ctxt->node == NULL)
    return(Qnil);
  else
    return(ruby_xml_node2_wrap(cXMLNode,
			      rxpc->ctxt->node));
}
Example #2
0
/*
 * call-seq:
 *    node.first -> XML::Node
 * 
 * Returns this node's first child node if any.
 */
VALUE
ruby_xml_node_first_get(VALUE self) {
  xmlNodePtr xnode;

  Data_Get_Struct(self, xmlNode, xnode);

  if (xnode->children)
    return(ruby_xml_node2_wrap(cXMLNode, xnode->children));
  else
    return(Qnil);
}
Example #3
0
/*
 * call-seq:
 *    document.child -> node
 *
 * Get this document's child node.
 */
VALUE
ruby_xml_document_child_get(VALUE self) {
  ruby_xml_document_t *rxd;

  Data_Get_Struct(self, ruby_xml_document_t, rxd);

  if (rxd->doc->children == NULL)
    return(Qnil);

  return ruby_xml_node2_wrap(cXMLNode, rxd->doc->children);
}
Example #4
0
/*
 * call-seq:
 *    node.each -> XML::Node
 * 
 * Iterates over this node's children, including text 
 * nodes, element nodes, etc.  If you wish to iterate
 * only over child elements, use XML::Node#each_element.
 *
 *  doc = XML::Document.new('model/books.xml')
 *  doc.root.each {|node| puts node}
 */
VALUE
ruby_xml_node_each(VALUE self) {
  xmlNodePtr xnode;
  xmlNodePtr xchild;
  Data_Get_Struct(self, xmlNode, xnode);

  xchild = xnode->children;
  
  while (xchild) {
    rb_yield(ruby_xml_node2_wrap(cXMLNode, xchild));
    xchild = xchild->next;
  }
  return Qnil;
}
Example #5
0
/*
 * underlying for child_set and child_add, difference being
 * former raises on implicit copy, latter does not.
 */
VALUE
ruby_xml_node_child_set_aux(VALUE self, VALUE rnode) {
  xmlNodePtr pnode, chld, ret;

  if (rb_obj_is_kind_of(rnode, cXMLNode) == Qfalse)
    rb_raise(rb_eTypeError, "Must pass an XML::Node object");

  Data_Get_Struct(self,  xmlNode, pnode);
  Data_Get_Struct(rnode, xmlNode, chld);
  
  if ( chld->parent != NULL || chld->doc != NULL )
    rb_raise(rb_eRuntimeError, "Cannot move a node from one document to another with child= or <<.  First copy the node before moving it.");

  ret = xmlAddChild(pnode, chld);
  if (ret == NULL) {
    rb_raise(eXMLNodeFailedModify, "unable to add a child to the document");
  } else if ( ret==chld ) {
    /* child was added whole to parent and we need to return it as a new object */
    return ruby_xml_node2_wrap(cXMLNode,chld);
  }
  /* else */
  /* If it was a text node, then ret should be parent->last, so we will just return ret. */
  return ruby_xml_node2_wrap(cXMLNode,ret);
}