1

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
    ^

私はかなり長い間これを理解することができず、アイデアがありません。どなたかお持ちでしたら、シェアしていただけるとありがたいです。

4

1 に答える 1

5

sizeofxml データのサイズを決定するために使用しています。常に 4 を返す char ポインターの場合。おそらく必要なのはstrlen.

doc = xmlReadMemory(XMLFile, strlen(XMLFile), "noname.xml", NULL, 0);
于 2013-07-18T20:48:25.417 に答える