Class: XPather
- Inherits:
-
Object
- Object
- XPather
- Defined in:
- lib/xpather.rb,
ext/xpather/xpather.c
Class Method Summary collapse
Instance Method Summary collapse
Constructor Details
#initialize(xmlStr) ⇒ Object
101 102 103 104 105 |
# File 'ext/xpather/xpather.c', line 101
static VALUE initialize(VALUE self, VALUE xmlStr)
{
rb_iv_set(self, "@xml_str", xmlStr);
return self;
}
|
Class Method Details
.new(xmlStr) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'ext/xpather/xpather.c', line 107
VALUE constructor(VALUE self, VALUE xmlStr)
{
xmlDocPtr doc;
const char *xmlCStr = StringValueCStr(xmlStr);
VALUE argv[1];
VALUE t_data;
doc = xmlParseMemory(xmlCStr, 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
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'ext/xpather/xpather.c', line 12
VALUE get(VALUE self, VALUE xpathExpr)
{
VALUE results = rb_ary_new();
xmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
xmlNodeSetPtr nodes;
xmlNodePtr cur;
int size;
int i;
Data_Get_Struct(self, xmlDoc, doc);
xpathCtx = xmlXPathNewContext(doc);
if (xpathCtx == NULL) {
rb_raise(rb_eArgError, "Error: unable to create new XPath context\n");
return Qnil;
}
xpathObj = xmlXPathEvalExpression(StringValueCStr(xpathExpr), xpathCtx);
if (xpathObj == NULL) {
rb_raise(rb_eArgError, "Error: unable to evaluate xpath expression \"%s\"\n", StringValueCStr(xpathExpr));
xmlXPathFreeContext(xpathCtx);
return Qnil;
}
nodes = xpathObj->nodesetval;
size = (nodes) ? nodes->nodeNr : 0;
if (size == 1) {
results = rb_str_new2(xmlNodeGetContent(nodes->nodeTab[0]));
} else if (size > 1) {
for (i = 0; i < size; ++i) {
cur = nodes->nodeTab[i];
rb_ary_push(results, rb_str_new2(xmlNodeGetContent(cur)));
}
} else {
return Qnil;
}
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
return results;
}
|
#search(xpathExpr) ⇒ Object
58 59 60 61 62 63 64 65 66 67 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 98 99 |
# File 'ext/xpather/xpather.c', line 58
VALUE search(VALUE self, VALUE xpathExpr)
{
VALUE results = rb_ary_new();
xmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
xmlNodeSetPtr nodes;
xmlNodePtr cur;
xmlBufferPtr nodeBuffer;
int size;
int i;
Data_Get_Struct(self, xmlDoc, doc);
xpathCtx = xmlXPathNewContext(doc);
if (xpathCtx == NULL) {
rb_raise(rb_eArgError, "Error: unable to create new XPath context\n");
return Qnil;
}
xpathObj = xmlXPathEvalExpression(StringValueCStr(xpathExpr), xpathCtx);
if (xpathObj == NULL) {
rb_raise(rb_eArgError, "Error: unable to evaluate xpath expression \"%s\"\n", StringValueCStr(xpathExpr));
xmlXPathFreeContext(xpathCtx);
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);
xmlXPathFreeContext(xpathCtx);
return results;
}
|