Class: RXerces::XML::Document
- Inherits:
-
Object
- Object
- RXerces::XML::Document
- Defined in:
- ext/rxerces/rxerces.cpp
Class Method Summary collapse
-
.parse(str) ⇒ Object
RXerces::XML::Document.parse(string).
Instance Method Summary collapse
-
#root ⇒ Object
document.root.
-
#to_s ⇒ Object
document.to_s / document.to_xml.
-
#to_xml ⇒ Object
document.to_s / document.to_xml.
-
#xpath(path) ⇒ Object
document.xpath(path).
Class Method Details
.parse(str) ⇒ Object
RXerces::XML::Document.parse(string)
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'ext/rxerces/rxerces.cpp', line 176
static VALUE document_parse(VALUE klass, VALUE str) {
if (!xerces_initialized) {
try {
XMLPlatformUtils::Initialize();
xerces_initialized = true;
} catch (const XMLException& e) {
rb_raise(rb_eRuntimeError, "Xerces initialization failed");
}
}
Check_Type(str, T_STRING);
const char* xml_str = StringValueCStr(str);
XercesDOMParser* parser = new XercesDOMParser();
parser->setValidationScheme(XercesDOMParser::Val_Never);
parser->setDoNamespaces(false);
parser->setDoSchema(false);
try {
MemBufInputSource input((const XMLByte*)xml_str, strlen(xml_str), "memory");
parser->parse(input);
DOMDocument* doc = parser->getDocument();
DocumentWrapper* wrapper = ALLOC(DocumentWrapper);
wrapper->doc = doc;
wrapper->parser = parser;
VALUE rb_doc = TypedData_Wrap_Struct(rb_cDocument, &document_type, wrapper);
return rb_doc;
} catch (const XMLException& e) {
CharStr message(e.getMessage());
delete parser;
rb_raise(rb_eRuntimeError, "XML parsing error: %s", message.localForm());
} catch (const DOMException& e) {
CharStr message(e.getMessage());
delete parser;
rb_raise(rb_eRuntimeError, "DOM error: %s", message.localForm());
} catch (...) {
delete parser;
rb_raise(rb_eRuntimeError, "Unknown XML parsing error");
}
return Qnil;
}
|
Instance Method Details
#root ⇒ Object
document.root
223 224 225 226 227 228 229 230 231 232 233 |
# File 'ext/rxerces/rxerces.cpp', line 223
static VALUE document_root(VALUE self) {
DocumentWrapper* wrapper;
TypedData_Get_Struct(self, DocumentWrapper, &document_type, wrapper);
if (!wrapper->doc) {
return Qnil;
}
DOMElement* root = wrapper->doc->getDocumentElement();
return wrap_node(root, self);
}
|
#to_s ⇒ Object
document.to_s / document.to_xml
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'ext/rxerces/rxerces.cpp', line 236
static VALUE document_to_s(VALUE self) {
DocumentWrapper* wrapper;
TypedData_Get_Struct(self, DocumentWrapper, &document_type, wrapper);
if (!wrapper->doc) {
return rb_str_new_cstr("");
}
try {
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XStr("LS").unicodeForm());
DOMLSSerializer* serializer = ((DOMImplementationLS*)impl)->createLSSerializer();
XMLCh* xml_str = serializer->writeToString(wrapper->doc);
CharStr utf8_str(xml_str);
VALUE result = rb_str_new_cstr(utf8_str.localForm());
XMLString::release(&xml_str);
serializer->release();
return result;
} catch (...) {
rb_raise(rb_eRuntimeError, "Failed to serialize document");
}
return Qnil;
}
|
#to_xml ⇒ Object
document.to_s / document.to_xml
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'ext/rxerces/rxerces.cpp', line 236
static VALUE document_to_s(VALUE self) {
DocumentWrapper* wrapper;
TypedData_Get_Struct(self, DocumentWrapper, &document_type, wrapper);
if (!wrapper->doc) {
return rb_str_new_cstr("");
}
try {
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XStr("LS").unicodeForm());
DOMLSSerializer* serializer = ((DOMImplementationLS*)impl)->createLSSerializer();
XMLCh* xml_str = serializer->writeToString(wrapper->doc);
CharStr utf8_str(xml_str);
VALUE result = rb_str_new_cstr(utf8_str.localForm());
XMLString::release(&xml_str);
serializer->release();
return result;
} catch (...) {
rb_raise(rb_eRuntimeError, "Failed to serialize document");
}
return Qnil;
}
|
#xpath(path) ⇒ Object
document.xpath(path)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'ext/rxerces/rxerces.cpp', line 265
static VALUE document_xpath(VALUE self, VALUE path) {
DocumentWrapper* doc_wrapper;
TypedData_Get_Struct(self, DocumentWrapper, &document_type, doc_wrapper);
if (!doc_wrapper->doc) {
NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
wrapper->nodes_array = rb_ary_new();
return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);
}
Check_Type(path, T_STRING);
const char* xpath_str = StringValueCStr(path);
try {
DOMElement* root = doc_wrapper->doc->getDocumentElement();
if (!root) {
NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
wrapper->nodes_array = rb_ary_new();
return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);
}
DOMXPathNSResolver* resolver = doc_wrapper->doc->createNSResolver(root);
DOMXPathExpression* expression = doc_wrapper->doc->createExpression(
XStr(xpath_str).unicodeForm(), resolver);
DOMXPathResult* result = expression->evaluate(
doc_wrapper->doc->getDocumentElement(),
DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
VALUE nodes_array = rb_ary_new();
XMLSize_t length = result->getSnapshotLength();
for (XMLSize_t i = 0; i < length; i++) {
result->snapshotItem(i);
DOMNode* node = result->getNodeValue();
if (node) {
rb_ary_push(nodes_array, wrap_node(node, self));
}
}
expression->release();
resolver->release();
result->release();
NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
wrapper->nodes_array = nodes_array;
return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);
} catch (const DOMXPathException& e) {
CharStr message(e.getMessage());
rb_raise(rb_eRuntimeError, "XPath error: %s", message.localForm());
} catch (const DOMException& e) {
CharStr message(e.getMessage());
rb_raise(rb_eRuntimeError, "DOM error: %s", message.localForm());
} catch (...) {
rb_raise(rb_eRuntimeError, "Unknown XPath error");
}
NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
wrapper->nodes_array = rb_ary_new();
return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);
}
|