Read directly from CString

2004-06-15
2013-05-20
  • Christian Schmalz

    hi all,

    i'm working on an little project using tinyxml. an i've got a question during working on this.

    how can i read directly from a cstring??

    in the last release i only can read from xml files....

     
    • Ellers

      Ellers - 2004-06-15

      Hi Christian,

      Do you mean a C-style string (ie const char*) or a CString as in the MFC class, or something else?

      From memory (I don't have the source here in front of me) there is a document.parse() method that takes a const char*.

      HTH
      Ellers

       
    • Christian Schmalz

      Hi,

      i mean a mfc style cstring.

       
      • Yves Berquin

        Yves Berquin - 2004-06-15

        You should do something like this :

        CString S;
        S = "<item />"; // or any other xml string
        TiXmlDocument d;
        d . Parse ((LPCTSTR) S);

         
    • Christian Schmalz

      i use this

      TiXmlDocument doc ;
      CString xml;

      doc.Parse((const char*)(LPCTSTR)xml);

      but there no value in the parser, the first child is null....

      where's my bug??

       
    • Ellers

      Ellers - 2004-06-16

      OK I'm not being smart, I just want to cover the obvious base: did you mean EXACTLY that code you posted?

      You didn't initialise the string?

      Assuming you meant that you did something like

        xml="<item/>"

      as Yves posted, then are you checking the result of the parse?

      eg (borrowed from the test program)

      if ( doc.Error() )
      {
          printf( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() );
          exit( 1 );
      }

      TiXmlElement* firstElement = doc.RootElement()->FirstChildElement();

       
    • Christian Schmalz

      i've got the bug....

      i use the above code in a windows ce project and windows ce use unicode...

       
      • B Sizer

        B Sizer - 2004-06-16

        I used to use TinyXML for the PocketPC 2002 and didn't have a problem. I just had to perform a little basic conversion on data I got back from TinyXML.

         
    • Christian Schmalz

      ...in a ppc application a const char* is a multibyte character (a character consists of two bytes). the first byte is the character himself and the second byte is 0.

      when i convert my cstring to const char* the parse function reads the first byte as the first character(for example a tag open '<'). after this the function reads the second byte as the second character, but the character is 0 because it's not the second character, it's the second byte of the character. that throws a null pointer exception. ignore the 0.

      the solution is not the best way and it's a little bit slowly but i can get the data out of my web service and that's what i want.

      TiXmlDocument *doc = new TiXmlDocument();
      char* buf;
      CString sXml = TEXT("<?xml version="1.0" encoding="iso-8859-1"?>")
         
      buf = (char*)malloc(sXml.GetLength());
      memset(buf, 0, sizeof(buf));
      for (int i=0; i< sXml.GetLength(); i++) buf[i] = sXml[i];//to parse only the character
      doc->Parse((char*)buf);

      if there's anybody who got a better way...please post it or send me an email....

       
    • Ellers

      Ellers - 2004-06-17

      Like B Sizer said, what about writing some simple conversion routines?

      Stuff like

        CString widexml = ...
        MySingleByteString xml( widexml )
        ...
        doc->Parse( xml.c_str( ))

      this stuff has been done 100s of times, code *must* be out there on in the ppc SDK somewhere.

      Alternatively do a unicode build of tinyxml, modifying/patching if required?  It could be as simple as changing TinyXml something like

         #if unicode
         typedef wchar TXML_CHAR
         #else
         typedef char TXML_CHAR
         #endif

      then all the char routines will probably work just fine, even with pChar++ etc.

       
  • Rajakumar S

    Rajakumar S - 2011-02-16

    **TiXmlDocument doc ;
    CStringA xml(<head><body/></head>);

    doc.Parse(xml.GetBuffer()l);

    try this one for CString usage to parse an xml string…….
    **

     
  • Rajakumar S

    Rajakumar S - 2011-02-16

    sorry little changes in previous one…..

    TiXmlDocument doc ;
    CStringA xml("<head><body/></head>");
    doc.Parse(xml.GetBuffer());

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks