01-31-2008 08:27 AM - edited 01-31-2008 08:30 AM
<?xml version="1.0"?>
<tag XML1>
<child tag xml 1>
..
</child tag xml1>
</tag XML 1>
<?xml version="1.0"?>
<tag XML1>
<child tag xml 1>
..
</child tag xml1>
</tag XML 1>
02-01-2008 11:14 AM
03-31-2008 10:37 AM
In the above IndentXML function, there are two problems.
1) The file must be opened in binary mode.
2) It seems that the IMXWriter output always has UTF-16 encoding. This can be incorrect. So it is better to remove the XML header line ("<?xml ...?>") or replace it with one's own.
The following is a modified better version:
static void IndentXML(void)
{
FILE *outputFile = 0;
char *output = 0, *out;
VARIANT vtOutput, vtWriter;
CAObjHandle writer = 0;
CAObjHandle reader = 0;
HRESULT error;
CA_VariantSetEmpty(&vtOutput);
CA_VariantSetEmpty(&vtWriter);
errChk(MSXML2_NewSAXXMLReaderISAXXMLReader(NULL, 1, LOCALE_NEUTRAL, 0, &reader));
errChk(MSXML2_NewMXXMLWriterIMXWriter(NULL, 1, LOCALE_NEUTRAL, 0, &writer));
errChk(MSXML2_IMXWriterSetindent(writer, NULL, VTRUE));
errChk(MSXML2_IMXWriterSetstandalone(writer, NULL, VTRUE));
errChk(MSXML2_ISAXXMLReaderputContentHandler(reader, NULL, writer));
errChk(CA_VariantSetObjHandle(&vtWriter, writer, CAVT_UNKNOWN));
errChk(MSXML2_ISAXXMLReaderputProperty(reader, NULL,
L"http://xml.org/sax/properties/lexical-handler", vtWriter));
errChk(MSXML2_ISAXXMLReaderparseURL(reader, NULL, (unsigned short *)XML_FILE_PATH));
errChk(MSXML2_IMXWriterGetoutput(writer, NULL, &vtOutput));
errChk(CA_VariantGetCString(&vtOutput, &output));
nullChk(outputFile = fopen(XML_FILE_PATH, "wb"));
out = strstr(output, "\r\n");
out += 2;
fwrite(out, sizeof(char), strlen(out), outputFile);
Error:
if (outputFile)
fclose(outputFile);
CA_DiscardObjHandle(reader);
CA_DiscardObjHandle(writer);
CA_FreeMemory(output);
CA_VariantClear(&vtOutput);
CA_VariantClear(&vtWriter);
if (FAILED(error))
{
char errorBuffer[512];
CA_GetAutomationErrorString(error, errorBuffer, sizeof(errorBuffer) - 1);
fprintf(stderr, "Error: %s\n", errorBuffer);
}
}