Question

I made a code that parse Function but it won't parse the Argument (name,type) My Code:

xml_node GCC_XML = doc.child("GCC_XML");

{


for (xml_node Function = GCC_XML.child("Function");Function; Function= Function.next_sibling("Function"))
{    
    cout<<"Function\n"<<"id= "<<Function.attribute("id").value()<<" , name= "<<Function.attribute("name").value()<<" ,returns: "<<Function.attribute("returns").value()<<Function.child_value("Argument") <<endl;

}

}

That is the XML file i am trying to parse

<?xml version="1.0"?>
<GCC_XML>
  <Namespace id="_1" name="::" members="_2 _3 _4 "/>
  <Function id="_2" name="main" returns="_5" context="_1" location="f0:8"/>
  <Function id="_3" name="a_function" returns="_5" context="_1" location="f0:4">
    <Argument name="f" type="_6"/>
    <Argument name="e" type="_4"/>
  </Function>
  <Struct id="_4" name="EmptyClass" context="_1" location="f0:1" members="_7 _8 " bases=""/>
  <FundamentalType id="_5" name="int"/>
  <FundamentalType id="_6" name="float"/>
  <Constructor id="_7" name="EmptyClass" context="_4" location="f0:1">
    <Argument name="_ctor_arg" type="_9"/>
  </Constructor>
  <Constructor id="_8" name="EmptyClass" context="_4" location="f0:1"/>
  <ReferenceType id="_9" type="_4c"/>
  <File id="f0" name="example1.cxx"/>
</GCC_XML>

The problem is that it refuse to read the functions arguments my results are :

 Load result: No error 
Function 
id= _2 , name= main ,returns: _5
 Function 
id= _3 , name= a_function ,returns: _5

I also tried

cout<<"Function\n"<<"id= "<<Function.attribute("id").value()<<" , name= "<<Function.attribute("name").value()<<" ,returns: "<<Function.attribute("returns").value()<<Function.attribute("Argument") <<endl;

put it gave the same result just added 0 output in the Argument output which show it doesn't read it

Put i got the same results above

Was it helpful?

Solution

Argument is not an attribute. It is an element, a child element of Function. You access child elements the same way you access any element.

xml_node::child_value finds the XML element with the given name (Argument in this case), and returns the text of the first child text node of that element. Argument, in your XML file, is empty; it doesn't have child text nodes. It has attribute nodes, but that's not the same thing. If you want the attributes of Argument, you have to ask for them, exactly as you asked for the attributes of the Function node.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top