10 #include <libxml/debugXML.h>
11 #include <libxml/xpath.h>
12 #include <libxml/xpathInternals.h>
40 : _filename(from->filename())
41 , _root(new
XMLNode(*from->root()))
42 , _doc (xmlCopyDoc (from->_doc, 1))
43 , _compression(from->compression())
85 xmlParserCtxtPtr ctxt = NULL;
87 xmlKeepBlanksDefault(0);
91 ctxt = xmlNewParserCtxt();
95 _doc = xmlCtxtReadFile(ctxt,
_filename.c_str(), NULL, XML_PARSE_DTDVALID);
103 xmlFreeParserCtxt(ctxt);
108 if (validate && ctxt->valid == 0) {
109 xmlFreeParserCtxt(ctxt);
118 xmlFreeParserCtxt(ctxt);
134 doc = xmlParseMemory(const_cast<char*>(buffer.c_str()), buffer.length());
153 xmlKeepBlanksDefault(0);
157 result = xmlSaveFormatFileEnc(
_filename.c_str(), doc,
"UTF-8", 1);
160 xmlErrorPtr xerr = xmlGetLastError ();
162 std::cerr <<
"unknown XML error during xmlSaveFormatFileEnc()." << std::endl;
164 std::cerr <<
"xmlSaveFormatFileEnc: error"
165 <<
" domain: " << xerr->domain
166 <<
" code: " << xerr->code
167 <<
" msg: " << xerr->message
184 #ifdef LIBXML_DEBUG_ENABLED
188 xmlKeepBlanksDefault(0);
192 xmlDebugDumpDocument (out, doc);
200 static string retval;
206 xmlKeepBlanksDefault(0);
210 xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
281 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
282 add_property((*curprop)->name().c_str(), (*curprop)->value());
286 for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
320 if ((*cur)->name() ==
name) {
342 if ((*cur)->name() == n) {
373 xmlXPathContext* ctxt;
379 ctxt = xmlXPathNewContext(doc);
381 ctxt = xmlXPathNewContext(
_doc);
387 xmlXPathFreeContext(ctxt);
400 assert(children.size() == 1);
402 assert(child->is_content());
416 map<string,XMLProperty*>::iterator iter;
428 map<string,XMLProperty*>::iterator iter;
441 map<string,XMLProperty*>::iterator iter;
444 iter->second->set_value (v);
471 snprintf(str,
sizeof(str),
"%ld", value);
492 (*i)->remove_property_recursively (n);
505 if ((*i)->name() == n) {
521 if ((*i)->name() == n) {
540 prop = (*i)->property(propname);
541 if (prop && prop->
value() == val) {
555 for (
size_t i = 0; i <
_name.length(); ++i) {
556 if (
_name[i] ==
'_') {
569 string name, content;
575 name = (
const char*)node->
name;
580 for (attr = node->properties; attr; attr = attr->next) {
582 if (attr->children) {
583 content = (
char*)attr->children->content;
594 for (child = node->children; child; child = child->next) {
611 node = doc->children = xmlNewDocNode(doc, 0, (
const xmlChar*) n->
name().c_str(), 0);
613 node = xmlNewChild(p, 0, (
const xmlChar*) n->
name().c_str(), 0);
617 node->type = XML_TEXT_NODE;
618 xmlNodeSetContentLen(node, (
const xmlChar*)n->
content().c_str(), n->
content().length());
622 for (curprop = props.begin(); curprop != props.end(); ++curprop) {
623 xmlSetProp(node, (
const xmlChar*) (*curprop)->name().c_str(), (
const xmlChar*) (*curprop)->value().c_str());
627 for (curchild = children.begin(); curchild != children.end(); ++curchild) {
634 xmlXPathObject* result = xmlXPathEval((
const xmlChar*)xpath.c_str(), ctxt);
637 xmlXPathFreeContext(ctxt);
638 xmlFreeDoc(ctxt->doc);
643 if (result->type != XPATH_NODESET) {
644 xmlXPathFreeObject(result);
645 xmlXPathFreeContext(ctxt);
646 xmlFreeDoc(ctxt->doc);
648 throw XMLException(
"Only nodeset result types are supported.");
651 xmlNodeSet* nodeset = result->nodesetval;
654 for (
int i = 0; i < nodeset->nodeNr; ++i) {
662 xmlXPathFreeObject(result);
672 s << p <<
" " <<
content() <<
"\n";
674 s << p <<
"<" <<
_name;
675 for (XMLPropertyList::const_iterator i =
_proplist.begin(); i !=
_proplist.end(); ++i) {
676 s <<
" " << (*i)->name() <<
"=\"" << (*i)->value() <<
"\"";
681 (*i)->dump (s, p +
" ");
684 s << p <<
"</" << _name <<
">\n";
XMLNodeList::iterator XMLNodeIterator
std::list< boost::shared_ptr< XMLNode > > XMLSharedNodeList
const XMLPropertyList & properties() const
const std::string & content() const
const std::string & value() const
bool read_internal(bool validate)
const std::string & write_buffer() const
boost::shared_ptr< XMLSharedNodeList > find(const std::string xpath, XMLNode *=0) const
const std::string & name() const
const std::string & name() const
std::list< XMLProperty * > XMLPropertyList
XMLNode * add_child_copy(const XMLNode &)
const XMLNodeList & children(const std::string &str=std::string()) const
XMLNode * add_child(const char *)
XMLNodeList _selected_children
bool read_buffer(const std::string &)
void dump(std::ostream &, std::string p="") const
std::list< XMLNode * > XMLNodeList
XMLProperty * property(const char *)
XMLNode(const std::string &name)
static XMLSharedNodeList * find_impl(xmlXPathContext *ctxt, const string &xpath)
static void writenode(xmlDocPtr, XMLNode *, xmlNodePtr, int)
XMLNode & operator=(const XMLNode &other)
std::string attribute_value()
void remove_nodes(const std::string &)
XMLProperty * add_property(const char *name, const std::string &value)
void add_child_nocopy(XMLNode &)
XMLNode * add_content(const std::string &s=std::string())
XMLPropertyList _proplist
void remove_property(const std::string &)
const std::string & set_content(const std::string &)
void remove_property_recursively(const std::string &)
static XMLNode * readnode(xmlNodePtr)
XMLNode * child(const char *) const
XMLPropertyList::iterator XMLPropertyIterator
void remove_nodes_and_delete(const std::string &)
XMLNodeList::const_iterator XMLNodeConstIterator
XMLProperty(const std::string &n, const std::string &v=std::string())