[英]libxml2 fails to parse from buffer but parses successfully from file
我有一個使用libxml2 writer將XML文檔寫入緩沖區的函數,但是當我嘗試使用xmlParseMemory從內存中解析文檔時,它僅返回解析器錯誤。 我也嘗試將文檔寫入文件並使用xmlParseFile對其進行解析,並成功解析。
這就是我初始化xml文檔的writer和緩沖區的方式。
int rc, i = 0;
xmlTextWriterPtr writer;
xmlBufferPtr buf;
// Create a new XML buffer, to which the XML document will be written
buf = xmlBufferCreate();
if (buf == NULL)
{
printf("testXmlwriterMemory: Error creating the xml buffer\n");
return;
}
// Create a new XmlWriter for memory, with no compression.
// Remark: there is no compression for this kind of xmlTextWriter
writer = xmlNewTextWriterMemory(buf, 0);
if (writer == NULL)
{
printf("testXmlwriterMemory: Error creating the xml writer\n");
return;
}
// Start the document with the xml default for the version,
// encoding UTF-8 and the default for the standalone
// declaration.
rc = xmlTextWriterStartDocument(writer, NULL, ENCODING, NULL);
if (rc < 0)
{
printf
("testXmlwriterMemory: Error at xmlTextWriterStartDocument\n");
return;
}
我將xml文檔傳遞給要使用int ret = validateXML(buf->content);
進行驗證的另一個函數int ret = validateXML(buf->content);
這是validateXML的第一部分
int validateXML(char *buffer)
{
xmlDocPtr doc;
xmlSchemaPtr schema = NULL;
xmlSchemaParserCtxtPtr ctxt;
char *XSDFileName = XSDFILE;
char *XMLFile = buffer;
int ret = 1;
doc = xmlReadMemory(XMLFile, sizeof(XMLFile), "noname.xml", NULL, 0);
調用此函數后,doc始終為NULL,這意味着它無法解析文檔。
這是運行程序返回的錯誤
Entity: line 1: parser error : ParsePI: PI xm space expected
<?xm
^
Entity: line 1: parser error : ParsePI: PI xm never end ...
<?xm
^
Entity: line 1: parser error : Start tag expected, '<' not found
<?xm
^
我已經很長一段時間無法弄清這個問題了,我沒有想法。 如果有人有,請與我分享。
您正在使用sizeof
來確定xml數據的大小。 對於總是返回4的char指針strlen
是您可能需要的。
doc = xmlReadMemory(XMLFile, strlen(XMLFile), "noname.xml", NULL, 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.