Lynn Allan - 2002-06-21

Odd behavior if attribute has both ' and " in it

Hi Lee,

I'm getting ready to incorporate the non-stl version of TinyXml 2.1.3 in my project. It is looking pretty good :-)

I'm encountering some strange behavior when an attribute has both " and ' in it and the doc.SaveFile() and doc.LoadFile() is used.  (I can send you the modified xmltest.cpp and .dsw files.)

Here is the xmltest.cpp program (don't know how it will appear), followed by the output. TinyXml is apparently switching around attrib=" has 'apos' " and attrib=' has "quote" ' and perhaps getting confused. Or maybe I'm doing something wrong? Line 5 is getting truncated when it encounteres the apostrophe.

// ********** tinyxml.cpp  ********************

#include "tinyxml.h"

int main()
{
  const char* passages =
    "<?xml version=\"1.0\" standalone=\"no\" ?>"
    "<passages count=\"005\" formatversion=\"20020620\">"
    "<psg key=\"010010034\" vCount=\"02\" ctxLines=\"02\" context=\"Line 1 is basic (no quotes or apostrophes).\"> </psg>"
    "<psg key=\"020340064\" vCount=\"01\" ctxLines=\"04\" context=\"Line 2  has   spaces.\"> </psg>"
    "<psg key=\"510040194\" vCount=\"01\" ctxLines=\"03\" context=\"Line 3 has &quot;quotatation marks&quot;.\"> </psg>"
    "<psg key=\"510050464\" vCount=\"03\" ctxLines=\"04\" context=\"Line 4 has &apos;apostrophe marks&apos;.\"> </psg>"
    "<psg key=\"510060314\" vCount=\"04\" ctxLines=\"05\" context=\"Line 5 has &quot;quotation marks&quot; and &apos;apostrophe marks&apos;.\"> </psg>"
    "<psg key=\"540030168\" vCount=\"02\" ctxLines=\"02\" context=\"Line 6 is basic (no quotes or apostrophes).\"> </psg>"
    "</passages>";

  TiXmlBase::SetCondenseWhiteSpace(false);
  TiXmlDocument doc( "passages.xml" );
  doc.Parse( passages );

  if ( doc.Error() ) {
    printf( "Error in %s: %s\n", doc.Value(), doc.ErrorDesc() );
    exit( 1 );
  }
  int count = 0;
  TiXmlNode* node = doc.FirstChild("passages");
  TiXmlElement* passagesElement = node->ToElement();
  printf("Type: %d  count: %s  version: %s\n",
         passagesElement->Type(),
         passagesElement->Attribute("count"),
         passagesElement->Attribute("formatversion"));

  count = 0;
  TiXmlElement* psgElement = passagesElement->FirstChildElement();
  while (psgElement != 0) {
    count++;
    printf("\nType: %d  Key: %s  vCount: %s  ctxLines: %s  Context: %s\n",
           psgElement->Type(),
           psgElement->Attribute("key"),
           psgElement->Attribute("vCount"),
           psgElement->Attribute("ctxLines"),
           psgElement->Attribute("context"));
    psgElement = psgElement->NextSiblingElement();
  }
  printf( "\nThe 'passages' element contains %d elements. (6)\n", count );

  doc.SaveFile();
  doc.LoadFile();
  doc.Print();

  count = 0;
  node = doc.FirstChild("passages");
  passagesElement = node->ToElement();
  printf("Type: %d  count: %s  version: %s\n",
         passagesElement->Type(),
         passagesElement->Attribute("count"),
         passagesElement->Attribute("formatversion"));

  count = 0;
  psgElement = passagesElement->FirstChildElement();
  while (psgElement != 0) {
    count++;
    printf("\nType: %d  Key: %s  vCount: %s  ctxLines: %s  Context: %s\n",
           psgElement->Type(),
           psgElement->Attribute("key"),
           psgElement->Attribute("vCount"),
           psgElement->Attribute("ctxLines"),
           psgElement->Attribute("context"));
    psgElement = psgElement->NextSiblingElement();
  }
  printf( "\nThe 'passages' element contains %d elements. (6)\n", count );
  return 0;
}

// ********** tinyxml.cpp output  ********************

Type: 1  count: 005  version: 20020620

Type: 1  Key: 010010034  vCount: 02  ctxLines: 02  Context: Line 1 is basic (no quotes or apostrophes).

Type: 1  Key: 020340064  vCount: 01  ctxLines: 04  Context: Line 2  has   spaces.

Type: 1  Key: 510040194  vCount: 01  ctxLines: 03  Context: Line 3 has "quotatation marks".

Type: 1  Key: 510050464  vCount: 03  ctxLines: 04  Context: Line 4 has 'apostrophe marks'.

Type: 1  Key: 510060314  vCount: 04  ctxLines: 05  Context: Line 5 has "quotation marks" and 'apostrophe marks'.

Type: 1  Key: 540030168  vCount: 02  ctxLines: 02  Context: Line 6 is basic (no quotes or apostrophes).

The 'passages' element contains 6 elements. (6)
<?xml version="1.0" standalone="no" ?>
<passages count="005" formatversion="20020620">
    <psg key="010010034" vCount="02" ctxLines="02" context="Line 1 is basic (no quotes or apostrophes)." />
    <psg key="020340064" vCount="01" ctxLines="04" context="Line 2  has   spaces." />
    <psg key="510040194" vCount="01" ctxLines="03" context='Line 3 has "quotatation marks".' />
    <psg key="510050464" vCount="03" ctxLines="04" context="Line 4 has 'apostrophe marks'." />
    <psg key="510060314" vCount="04" ctxLines="05" context='Line 5 has "quotation marks" and ' />
</passages>
Type: 1  count: 005  version: 20020620

Type: 1  Key: 010010034  vCount: 02  ctxLines: 02  Context: Line 1 is basic (no quotes or apostrophes).

Type: 1  Key: 020340064  vCount: 01  ctxLines: 04  Context: Line 2  has   spaces.

Type: 1  Key: 510040194  vCount: 01  ctxLines: 03  Context: Line 3 has "quotatation marks".

Type: 1  Key: 510050464  vCount: 03  ctxLines: 04  Context: Line 4 has 'apostrophe marks'.

Type: 1  Key: 510060314  vCount: 04  ctxLines: 05  Context: Line 5 has "quotation marks" and

The 'passages' element contains 5 elements. (6)