I'm trying to write a function that will get me the attribute of a set of XML nodes in a document using XPath with the TinyXPath library, but I cannot seem to figure it out. I find the documentation on TinyXPath is not very enlightening either. Can someone assist me?
std::string XMLDocument::GetXPathAttribute(const std::string& attribute)
{
TiXmlElement* Root = document.RootElement();
std::string attributevalue;
if (Root)
{
int pos = attribute.find('@'); //make sure the xpath string is for an attribute search
if (pos != 0xffffffff)
{
TinyXPath::xpath_processor proc(Root,attribute.c_str());
TinyXPath::expression_result xresult = proc.er_compute_xpath();
TinyXPath::node_set* ns = xresult.nsp_get_node_set(); // Get node set from XPath expression, however, I think it might only get me the attribute??
_ASSERTE(ns != NULL);
TiXmlAttribute* attrib = (TiXmlAttribute*)ns->XAp_get_attribute_in_set(0); // This always fails because my node set never contains anything...
return attributevalue; // need my attribute value to be in string format
}
}
}
usage:
XMLDocument doc;
std::string attrib;
attrib = doc.GetXPathAttribute("@Myattribute");
sample XML:
<?xml version="1.0" ?>
<Test />
<Element>Tony</Element>
<Element2 Myattribute="12">Tb</Element2>