gpt4 book ai didi

c - libxml2 无法从缓冲区解析但从文件成功解析

转载 作者:太空宇宙 更新时间:2023-11-04 01:08:38 38 4
gpt4 key购买 nike

我有一个使用 libxml2 编写器将 XML 文档写入缓冲区的函数,但是当我尝试使用 xmlParseMemory 从内存中解析文档时,它只返回解析器错误。我还尝试将文档写入文件并使用 xmlParseFile 对其进行解析并成功解析。

这就是我为 xml 文档初始化编写器和缓冲区的方式。

  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);

这是 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 的字符指针。您可能需要的是 strlen

doc = xmlReadMemory(XMLFile, strlen(XMLFile), "noname.xml", NULL, 0);

关于c - libxml2 无法从缓冲区解析但从文件成功解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17732852/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com