[xsd-users] Why this error?

Boris Kolpackov boris at codesynthesis.com
Thu Mar 8 02:42:07 EST 2007


Hi Al,

Al Niessner <Al.Niessner at jpl.nasa.gov> writes:

> I then wrote a bunch of C++ code that looks very similar to my Java and
> Python code to parse some XML. It did not work at all. I get this error
> message:
>
> terminate called after throwing an instance of
> 'xsd::cxx::tree::parsing<char>'
>   what(): instance document parsing failed.

It is usually a good idea to catch the exception and print it to get
a better idea about what went wrong. Here is what I did:

  try
  {
    msg = ExternalTool (ss, xml_schema::flags::dont_validate);
  }
  catch (const xml_schema::exception& e)
  {
    std::cerr << e << std::endl;
  }

After this change I got a more descriptive error:

1:13 error: Unterminated processing instruction

For some reason it does not like your XML declaration
(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>) since that's
the only processing instruction in this document.

Because you are first reading the contents of the file into a string
buffer, let's check if the XML is still good in that buffer. I added

  std::cout << fullText << std::endl;

Before the above code. It prints:

<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?><ExternalTool>...

The XML declaration does not look right anymore: all whitespaces are
gone. Looking at the code that fills the buffer we can see what's
going on:

  while (!fin.eof())
    {
      fin >> buffer;
      fullText += buffer;
    }

fin >> buffer reads one word at a time, stripping leading and trailing
whitespaces. Here is a version that reads in an exact copy of the file.
It is also a bit more efficient:

  fin.open ("msg.xml", std::ios::in | std::ios::ate);
  std::size_t size (fin.tellg ());
  fin.seekg (0, std::ios::beg);

  fullText.reserve (size);

  while (size != 0 && !fin.eof ())
  {
    char buf[1024];
    fin.read (buf, size < sizeof (buf) ? size : sizeof (buf));
    std::size_t n = fin.gcount ();

    fullText.append (buf, n);
    size -= n;
  }

  fin.close();

After making this change the file parses without any problems.
You probably know that, but just in case, you can parse the file
directly without first reading it into a buffer. Both:

  msg = ExternalTool ("msg.xml", xml_schema::flags::dont_validate);

and

  fin.open ("msg.xml");
  msg = ExternalTool (fin, xml_schema::flags::dont_validate);
  fin.close();

work.


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20070308/0cc308cd/attachment.pgp


More information about the xsd-users mailing list