c++ - How can I use POCO to parse an xml file and extract a particular node to a std::string? -


i want extract individual node using poco's libraries can't figure out how it. i'm new xml.

the xml looks (abbreviated):

<?xml version="1.0" encoding="utf-8"?> <!-- created xmlprettyprinter on 11/28/2012  --> <sbml xmlns = "http://www.sbml.org/sbml/level2/version4" level = "2" version = "4"> <model id = "cell">   <listofspecies> </listofspecies>   <listofparameters>      <parameter id = "kk1" value = "1"/>   </listofparameters>   <listofreactions>      <reaction id = "j1" reversible = "false"> ... stuff here ..   </listofreactions> </model> </sbml> 

i want extract in listofreactions node , store in std::string, later md5 hashing.

i have tried this:

ifstream in(joinpath(gtestdatafolder, "test_1.xml").c_str()); inputsource src(in); domparser parser; autoptr<document> pdoc = parser.parse(&src); nodeiterator it(pdoc, poco::xml::nodefilter::show_all); node* pnode = it.nextnode();  while(pnode) {     clog<<pnode->nodename()<<endl;     string elementid = "listofreactions";     if(pnode->nodename() == "listofreactions")     {          //extract in node... how???     }      pnode = it.nextnode(); } 

i ran similar problem myself. instance in case "poco::xml::nodefilter::show_all" filter applied, node types(element, text, cdatasection, etc) included when iteratering through xml document. found poco not include data in each node returns "nextnode()".

if 1 wants access xml nodes attributes, 1 first has query node check whether has attributes using "hasattributes()" , if does, iterate through each of these attributes find ones of interest.

xml example:

<?xml version="1.0"?> <reaction id="j1" reversible="false"> 

c++ example:

... poco::xml::namednodemap* attributes = null; poco::xml::node* attribute = null;  while(pnode) {  if( (pnode->nodename() == "reaction") && pnode->hasattributes())  {    attributes = pnode->attributes();     for(unsigned int = 0; < attributes->length(); i++)    {      attribute = attributes->item(i);      cout << attribute->nodename() << " : " << attribute->nodevalue() << endl    }   }   pnode = it.nextnode(); } ... 

should output:

id : j1 reversible : false 

if 1 wants access text between 2 xml tags, shown in xml example below, 1 first has find node name matches tag of interest, have done in example, , check next node calling "nextnode()" see if node has node name "#text" or "#cdata-section". if case, value of "next node" contain text between xml tags.

xml example:

<?xml version="1.0"?> <listofreactions>some text</listofreactions> 

c++ example:

... while(pnode) {  if(pnode->nodename() == "listofreactions")  {    pnode = it.nextnode();    if(pnode->nodename() != "#text")    {      continue; //no text node present    }    cout << "tag text: " << pnode->nodevalue() << endl;   }   pnode = it.nextnode(); } ... 

should output:

some text 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -