Class: XPather
- Inherits:
-
Object
- Object
- XPather
- Defined in:
- lib/xpather.rb,
ext/xpather/xpather.c
Class Method Summary collapse
Instance Method Summary collapse
- #get(xpathExpr) ⇒ Object
- #initialize(xmlStr) ⇒ Object constructor
- #search(xpathExpr) ⇒ Object
- #xml_str ⇒ Object
Constructor Details
#initialize(xmlStr) ⇒ Object
99 100 101 102 103 |
# File 'ext/xpather/xpather.c', line 99
static VALUE initialize(VALUE self, VALUE xmlStr)
{
rb_iv_set(self, "@xml_str", xmlStr);
return self;
}
|
Class Method Details
.new(xmlStr) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'ext/xpather/xpather.c', line 110
VALUE constructor(VALUE self, VALUE xmlStr)
{
xmlDocPtr doc;
const char *xmlCStr = StringValueCStr(xmlStr);
VALUE argv[1];
VALUE t_data;
doc = xmlParseMemory(xmlCStr, (int)strlen(xmlCStr));
if (doc == NULL) {
fprintf(stderr, "Error: unable to parse xml\n");
return Qnil;
}
t_data = Data_Wrap_Struct(self, 0, xml_free, doc);
argv[0] = xmlStr;
rb_obj_call_init(t_data, 1, argv);
return t_data;
}
|
Instance Method Details
#get(xpathExpr) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'ext/xpather/xpather.c', line 34
VALUE get(VALUE self, VALUE xpathExpr)
{
VALUE results = rb_ary_new();
xmlDocPtr doc;
xmlXPathObjectPtr xpathObj;
xmlNodeSetPtr nodes;
xmlNodePtr current;
int size, i;
Data_Get_Struct(self, xmlDoc, doc);
xpathObj = eval_and_search(doc, StringValueCStr(xpathExpr));
if (xpathObj == NULL) { return Qnil; }
nodes = xpathObj->nodesetval;
size = (nodes) ? nodes->nodeNr : 0;
if (size == 0) { return Qnil; }
if (size == 1) {
results = rb_str_new2(xmlNodeGetContent(nodes->nodeTab[0]));
} else {
for (i = 0; i < size; i++) {
current = nodes->nodeTab[i];
rb_ary_push(results, rb_str_new2(xmlNodeGetContent(current)));
}
}
xmlXPathFreeObject(xpathObj);
return results;
}
|
#search(xpathExpr) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'ext/xpather/xpather.c', line 68
VALUE search(VALUE self, VALUE xpathExpr)
{
VALUE results = rb_ary_new();
xmlDocPtr doc;
xmlXPathObjectPtr xpathObj;
xmlNodeSetPtr nodes;
xmlNodePtr cur;
xmlBufferPtr nodeBuffer;
int size, i;
Data_Get_Struct(self, xmlDoc, doc);
xpathObj = eval_and_search(doc, StringValueCStr(xpathExpr));
if (xpathObj == NULL) { return Qnil; }
nodes = xpathObj->nodesetval;
size = (nodes) ? nodes->nodeNr : 0;
for (i = 0; i < size; ++i) {
nodeBuffer = xmlBufferCreate();
xmlNodeDump(nodeBuffer, doc, nodes->nodeTab[i], 0, 1);
rb_ary_push(results, rb_str_new2(nodeBuffer->content));
xmlBufferFree(nodeBuffer);
}
xmlXPathFreeObject(xpathObj);
return results;
}
|
#xml_str ⇒ Object
105 106 107 108 |
# File 'ext/xpather/xpather.c', line 105
static VALUE xml_str(VALUE self)
{
return rb_iv_get(self, "@xml_str");
}
|