0

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

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Mic Hans
  • 27
  • 1
  • 5

1 Answers1

1

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • xml_node GCC_XML = doc.child("GCC_XML"); { for (xml_node Argument = GCC_XML.child("Argument");Argument; Argument= Argument.next_sibling("Argument")) { cout<<"Argument\n"<<"Name= "< – Mic Hans Jun 29 '12 at 00:29
  • @MicHans: OK, so... what does that mean? – Nicol Bolas Jun 29 '12 at 00:34
  • I am asking if that's what you meant to call for the Argument node attributes (name,Type)? Am newbie to programming generally – Mic Hans Jun 29 '12 at 00:42
  • @Mic: Please stop posting code in comments. Why don't you try the code yourself, see if it works, and then ask about it in your *question* if it doesn't. – Nicol Bolas Jun 29 '12 at 15:33
  • @NicolBolas your clear explaination of this helped me pass a failing test. – Greg M. Krsak Mar 30 '13 at 18:25