Re: [xml] xmlReadMemory : error "Extra content at the end of the document"
- From: Andrew Hartley <andrew hartley bcs org uk>
- To: xml gnome org
- Subject: Re: [xml] xmlReadMemory : error "Extra content at the end of the document"
- Date: Fri, 14 Nov 2008 01:01:08 -0800 (PST)
Ok, sorry. I am using VS2005 and the code is in a Regular DLL using shared
MFC. The version of LibXml2 that I am statically linking to is: 2-2.6.32+
I am loading the Xml from a resource within the DLL in the InitInstance():
------------------------------------------------------------------------------
CWinApp::InitInstance();
char szResName[] = "#7001";
char szResType[] = "TAXONOMYXML";
HMODULE hMod = AfxGetResourceHandle();
// Load the Xml resource:
HRSRC hRes = FindResource(hMod, szResName, szResType);
if (hRes)
{
HGLOBAL hBytes = LoadResource(hMod, hRes);
LPVOID lpData = LockResource(hBytes);
LPBYTE lpbData = (LPBYTE)lpData;
m_pXbrlTaxonomy = new CXbrlTaxonomy(reinterpret_cast<const
char*>(lpbData));
}
return TRUE;
------------------------------------------------------------------------------
Which as you can see passes the string read to my classes CXbrlTaxonomy
constructor the code for which is:
------------------------------------------------------------------------------
CXbrlTaxonomy::CXbrlTaxonomy(const string sXml)
{
ReadInXml(sXml);
}
------------------------------------------------------------------------------
The ReadInXml(...) [part] is:
------------------------------------------------------------------------------
void CXbrlTaxonomy::ReadInXml(const string sXml)
{
xmlDocPtr pXmlDoc = xmlReadMemory(sXml.c_str(), (int)sXml.size(), "xml",
NULL, 0);
if (pXmlDoc == NULL)
{
xmlErrorPtr pErr = xmlGetLastError();
AfxMessageBox(pErr->message);
}
else
{
xmlXPathContextPtr pXPathCtx = xmlXPathNewContext(pXmlDoc);
...
------------------------------------------------------------------------------
The xmlReadMemory(...) returns a valid pointer to an xmlDoc object when I
run the DLL in Debug mode, but when I switch to Release mode it returns
NULL. This is probably down to the Xml being read from the resource and I
will add some code to stream this out to a file so that I may investigate
that. The reason why I had parsed the sXml.c_str() to a char* and apended a
LF (0x0a) and then a null terminator was just attempting to see if the
std::string c_str() function was returning some trailing chars.
As requested if anyone can see a flaw in my code leading to the
xmlReadMemory(...) returning NULL PLEASE let me know ASAP. If I find
anything I will post my findings back here, so that if anyone else has a
similar issue they will know how to resolve it.
Elvis Stansvik wrote:
Hi,
First of all, please always state your platform and libxml2 version
when asking for help. Also provide a complete standalone code example
of your problem.
2008/11/13 Andrew Hartley <andrew hartley bcs org uk>:
I have written a DLL with a large (size = 1318109 chars) XML stored as a
resource. I read the XML into a std::string and load it with a call to
xmlReadMemory:
xmlDocPtr pXmlDoc = xmlReadMemory(sXml, sXml.length(), "xml", NULL, 0);
This will never work. libxml2 is C not C++ and the API only works with
NULL terminated sequences of UTF-8 bytes, not STL strings.
This works fine in Debug mode. But when I switch to Release mode I get
NULL
returned and the error text is "Extra content at the end of the
document".
I have tried parsing the std::string content into a char*, and setting
the
argv[3] to "UTF-8":
int nLen = (int)sXml.size();
char* pszContent = new char[nLen +2];
strcpy_s(pszContent, nLen +2, sXml.c_str());
pszContent[nLen] = '\n';
pszContent[nLen +1] = '\0';
xmlDocPtr pXmlDoc = xmlReadMemory(pszContent, nLen, "xml",
"UTF-8", 0);
delete[] pszContent;
pszContent = NULL;
But I still get the same error!
Your example works here (libxml2 on GNU/Linux):
#include <string.h>
#include <libxml/parser.h>
#include <iostream>
int main (int argc, char *argv[])
{
LIBXML_TEST_VERSION
std::string sXml("<foo/>");
int nLen = (int)sXml.size();
char* pszContent = new char[nLen + 2];
strncpy(pszContent, sXml.c_str(), nLen + 2);
pszContent[nLen] = '\n';
pszContent[nLen + 1] = '\0';
xmlDocPtr pXmlDoc = xmlReadMemory(pszContent, nLen, "xml", "UTF-8",
0);
if (pXmlDoc == NULL)
return(1);
delete[] pszContent;
pszContent = NULL;
xmlFreeDoc(pXmlDoc);
xmlCleanupParser();
return(0);
}
[astan franz ~]$ g++ -o test `xml2-config --libs --cflags` test.cpp
[astan franz ~]$ ./test
[astan franz ~]$ echo $?
0
[astan franz ~]$
Though the reason you are adding an extra newline after the XML eludes
me at the moment. Note that I had to use strncpy instead of strcpy_s,
as I'm on GNU/Linux where strcpy_s doesn't exist.
Regards,
Elvis
I have not resorted to attemting to debug through LibXml2 yet, and would
appreciate any advice.
--
View this message in context:
http://www.nabble.com/xmlReadMemory-%3A-error-%22Extra-content-at-the-end-of-the-document%22-tp20484750p20484750.html
Sent from the Gnome - Lib - Xml - General mailing list archive at
Nabble.com.
_______________________________________________
xml mailing list, project page http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml
_______________________________________________
xml mailing list, project page http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml
--
View this message in context:
http://www.nabble.com/xmlReadMemory-%3A-error-%22Extra-content-at-the-end-of-the-document%22-tp20484750p20496939.html
Sent from the Gnome - Lib - Xml - General mailing list archive at Nabble.com.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]