0

I'm trying to create an XML signature mechanism. my XML can be something like below:

<Main>
   <ChildType1>... </ChildType1>
   <ChildType2>... </ChildType2>
</Main>

In fact it would be agnostic to the kind of data below the Main node. And I only want to compute the CRC of data include into Main Node and after add a signature attribute on the Main.

But for now I wrote

ostringstream ss;
{
  boost::archive::text_oarchive oa(ss);
  ss.str("");
  try
  {
    TiXmlBase::SetCondenseWhiteSpace(false);

    tinyxml::Document doc;
    doc.LoadFile(filename.c_str());

    tinyxml::Element* schemes = tinyxml::findSingleElement<SchemesSignatureException>(doc, "Main");
    if (schemes == 0) throw logica_error("No Main found");

    tinyxml::TinyXmlFwdIterator<tinyxml::Element> child;
    for (child = child.begin(schemes); child != child.end(); ++child)
    {
      //the question is here, how to get the XML text of each child of the Main...
    }

  }
  catch (exception& e)
  {
    throw ;
  }

}
string data(ss.str());
boost::crc_32_type crc;
crc.process_bytes(data.data(), data.size());

As I iterate over the child how can I get the XML text string, as for my example, first iteration will flush "... " in oaand second iteration "... " in oa ?

I tried GetText but it always return NULL ! Have you an idea ?

alexbuisson
  • 7,699
  • 3
  • 31
  • 44
  • Calling `GetText()` on a `TiXmlElement` object should retrieve the text of an element. What version of tinyxml are you using? – Samhain Feb 18 '14 at 14:17
  • I don't see the datatypes you are referencing(`tinyxml::Element`) in the source, so it looks like you must be doing something extra. Calling `GetText()` on the `TiXmlElement` is the correct usage. – Samhain Feb 18 '14 at 15:38
  • thank for your help, in fact I use some namespace alias so that's why my code is not matching the tinyXML API. But finally I solved my issue. – alexbuisson Feb 18 '14 at 16:21

1 Answers1

1

Finally I found that overload the <<operator and recursively treat all node is a correct option, so I used the following code to drill-down into the node and dump into an output stream with the format I want. I lost all the angle-bracket signalization, but it's fine for me.

std::ostream& operator << ( std::ostream & out, tinyxml::Node * pParent)
{
  using namespace tinyxml;

  if ( !pParent ) return out;

  TiXmlText *pText;
  TiXmlAttribute * pAttrib;
  int t = pParent->Type();

  switch ( t )
  {
  case TiXmlNode::TINYXML_DOCUMENT:
    break;
  case TiXmlNode::TINYXML_ELEMENT:
    out << pParent->Value();
    pAttrib = ((tinyxml::Element*)(pParent))->FirstAttribute();
    while(pAttrib)    
    {
      out << pAttrib->Name() << pAttrib->Value();
      pAttrib = pAttrib->Next();
    }
  case TiXmlNode::TINYXML_COMMENT:
    break;
  case TiXmlNode::TINYXML_UNKNOWN:
    break;
  case TiXmlNode::TINYXML_TEXT:
    pText = pParent->ToText();
    out << pText->Value();
    break;
  case TiXmlNode::TINYXML_DECLARATION:
    break;
  default:
    break;
  }
  /*out << endl;*/
  TiXmlNode * pChild;
  for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) 
  {
    out << pChild;
  }

  return out;
}
alexbuisson
  • 7,699
  • 3
  • 31
  • 44