You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following matches a few xml2 functions used in mschart. Not sure if we really have a need for any of this, but I was curious what is used and not currently supported by us. We have a couple of functions that provide similar things, but a) I've only implemented nesting of 3 nodes (mschart requires more).
// xml_find_node// @param node xml_node a child is searched in// @param child the xml_node to search// @param pointer bool if pointer should be returned// @param escapes bool if escapes should be used// @export// [[Rcpp::export]]
SEXP xml_find_node(XPtrXML node, std::string chld) {
structLocal {
Local(std::string chld) {this->chld = chld;}
// // find attribute// bool operator()(pugi::xml_attribute attr) const {// return strcmp(attr.name(), "e") == 0;// }booloperator()(pugi::xml_node node) const {
std::string xml_name = node.name();
return xml_name.compare(chld.c_str()) == 0;
}
std::string chld;
};
unsignedint pugi_format_flags = pugi_format(node);
pugi::xml_node found = node->find_node(Local(chld));
// could return the value instead
std::ostringstream oss;
found.print(oss, "", pugi_format_flags);
SEXP name = Rcpp::wrap(Rcpp::String(oss.str()));
Rcpp::CharacterVector out = Rcpp::wrap(Rcpp::String(found.path()));
out.attr("names") = name;
return out;
}
// xml_remove_child// @param node xml_node a child is searched in// @param path the xpath to the node to remove// @param pointer bool if pointer should be returned// @export// [[Rcpp::export]]
SEXP xml_remove_child(XPtrXML node, std::string path, bool pointer) {
unsignedint pugi_format_flags = pugi_format(node);
pugi::xml_node got = node->select_node(path.c_str()).node();
node->select_node(path.c_str()).parent().remove_child(got);
if (pointer) {
return (node);
} else {
std::ostringstream oss;
node->print(oss, "", pugi_format_flags);
returnRcpp::wrap(Rcpp::String(oss.str()));
}
}
// xml_replace_node// @param node xml_node a child is searched in// @param child the xml_node to search// @param pointer bool if pointer should be returned// @param escapes bool if escapes should be used// @export// [[Rcpp::export]]
SEXP xml_replace_child(XPtrXML node, std::string path, std::string replacement, bool escapes, bool pointer) {
// pugi::parse_default without escapes flagunsignedint pugi_parse_flags = pugi::parse_cdata | pugi::parse_wconv_attribute | pugi::parse_ws_pcdata | pugi::parse_eol;
if (escapes) pugi_parse_flags |= pugi::parse_escapes;
unsignedint pugi_format_flags = pugi_format(node);
pugi::xml_node got = node->select_node(path.c_str()).node();
pugi::xml_document rplcmnt;
pugi::xml_parse_result result = rplcmnt.load_string(replacement.c_str(), pugi_parse_flags);
// check if result is a valid xml_nodeif (result) {
node->select_node(path.c_str()).parent().insert_copy_after(rplcmnt.first_child(), got);
} else {
Rf_error("oops");
}
node->select_node(path.c_str()).parent().remove_child(got);
if (pointer) {
return (node);
} else {
std::ostringstream oss;
node->print(oss, "", pugi_format_flags);
returnRcpp::wrap(Rcpp::String(oss.str()));
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The following matches a few
xml2
functions used inmschart
. Not sure if we really have a need for any of this, but I was curious what is used and not currently supported by us. We have a couple of functions that provide similar things, but a) I've only implemented nesting of 3 nodes (mschart
requires more).Beta Was this translation helpful? Give feedback.
All reactions