Menu

How to insert new lines?

Lucian
2005-03-15
2013-05-20
  • Lucian

    Lucian - 2005-03-15

    Hi,

    I used the following code, a part of it, to get some data with new lines from an XML file
    <XML>
      <FIELD name='Data''>Line One
    Line Two
    AnotherLine</FIELD>
    </XML>

    //****
    .....
    for (tNode = p_Node; tNode; tNode = tNode->NextSiblingElement())
    {
      LPSTR nodeName = NULL;
      LPSTR data = NULL;

    nodeName = (char *)tNode->Attribute("name");
    data = (char *)tNode->FirstChild()->Value();
    ....
    ....
    //*******

    data will be data ="Line One Line Two AnotherLine", with no \r\n chars? Has something to do with TiXmlParsingData::Stamp() ?

    How could I obtained data with new lines inside?

    Bets regards,
    Lucian.

    P.S. I'm still a beginner with TinyXml

     
    • Lucian

      Lucian - 2005-03-15

      Do the following change inside tinyxmlparser.cpp

      TiXmlBase::ReadText()
      ..
      if ( *p == '\r' || *p == '\n' )
         {
          //whitespace = true;
          (*text) += '\r';
          //(*text) += '\n';
         ++p;
      }

      and now I have multilines into my field.

      Maybe you could help with a simpler solution.

      Thanks

       
    • Ellers

      Ellers - 2005-03-16

      The short answer is to include this before you parse the file:

            TiXmlBase::SetCondenseWhiteSpace( false );

      The longer answer is that with this input file:

      <?xml version="1.0" ?>
      <poetry>
      <verse>
      Alas
      Great Whatever
      Alas (again)
      </verse>
      </poetry>

      and this C++ program (sourceforge removes my nice indenting...)

      [code]
      #include "tinyxml.h"

      const char * getTypeName( int nodeType )
      {
          switch ( nodeType )
          {
          case TiXmlNode::DOCUMENT:
              return "DOCUMENT";
          case TiXmlNode::ELEMENT:
              return "ELEMENT";
          case TiXmlNode::COMMENT:
              return "COMMENT";
          case TiXmlNode::UNKNOWN:
              return "UNKNOWN";
          case TiXmlNode::TEXT:
              return "TEXT";
          case TiXmlNode::DECLARATION:
              return "DECLARATION";
          default:
              return "I don't know";
          }
      }

      const char * getIndent( unsigned int numIndents )
      {
          static const char * pINDENT = "                                      ";
          static const unsigned int LENGTH = strlen( pINDENT );

          if ( numIndents > LENGTH ) numIndents = LENGTH;

          return &pINDENT[ LENGTH-numIndents ];
      }

      void visit( TiXmlNode * pParent, unsigned int indent = 0 )
      {
          if ( !pParent ) return;

          int t = pParent->Type();
          printf( "%s%s", getIndent( indent), getTypeName( t ));

          switch ( t )
          {
          case TiXmlNode::DOCUMENT:
              break;

          case TiXmlNode::ELEMENT:
              printf( " %s", pParent->Value() );
              break;

          case TiXmlNode::COMMENT:
              printf( ": %s", pParent->Value());
              break;

          case TiXmlNode::UNKNOWN:
              break;

          case TiXmlNode::TEXT:
              {
                  TiXmlText*    pText = pParent->ToText();
                  printf( "[%s]\n", pText->Value() );
              }    break;
          case TiXmlNode::DECLARATION:
              break;
          default:
              break;
          }
          printf( "\n" );

          TiXmlNode * pChild;

          for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
          {
              //printf( "Child type: %s\n", getTypeName(pChild->Type()));
              visit( pChild, indent+2 );
          }
      }

      int main(int argc, char* argv[])
      {
          printf("Walk the tree:\n");

          TiXmlBase::SetCondenseWhiteSpace( false );

          TiXmlDocument doc( "demo.xml" );
          bool loadOkay = doc.LoadFile();

          printf( "Load: %d\n", loadOkay );

          TiXmlElement * pElement;

          pElement = doc.RootElement();

          visit( pElement );
          return 0;
      }
      [/code]

      you get this output:

      Walk the tree:
      Load: 1
      ELEMENT poetry
        ELEMENT verse
          TEXT[
      Alas
      Great Whatever
      Alas (again)
      ]

       
    • Lucian

      Lucian - 2005-03-17

      thanks for your answer very much.

       

Log in to post a comment.