怎样去除xml文件头中的encoding和standalone属性

chenxiaohua 2008-10-16 08:52:44
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationLS.hpp>
#include <xercesc/dom/DOMWriter.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/util/XMLUni.hpp>
#include "DOMTreeErrorReporter.hpp"
#include "DOMPrintFilter.hpp"
#include "DOMPrintErrorHandler.hpp"
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/dom/deprecated/DOMString.hpp>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static char* gXmlFile = 0;
static bool gDoNamespaces = false;
static bool gDoSchema = false;
static bool gSchemaFullChecking = false;
static bool gDoCreate = false;

static char* goutputfile = 0;
static XMLCh* gOutputEncoding = 0;

static bool gSplitCdataSections = true;
static bool gDiscardDefaultContent = true;
static bool gUseFilter = false;
static bool gFormatPrettyPrint = false;
static bool gWriteBOM = false;

static XercesDOMParser::ValSchemes gValScheme = XercesDOMParser::Val_Auto;

void usage();
void usage()
{
XERCES_STD_QUALIFIER cout << "\nUsage:\n"
" DOMPrint [options] <XML file>\n\n"
"This program invokes the DOM parser, and builds the DOM tree.\n"
"It then asks the DOMWriter to serialize the DOM tree.\n"
"Options:\n"
" -e create entity reference nodes. Default is no expansion.\n"
" -v=xxx Validation scheme [always | never | auto*].\n"
" -n Enable namespace processing. Default is off.\n"
" -s Enable schema processing. Default is off.\n"
" -f Enable full schema constraint checking. Defaults is off.\n"
" -wenc=XXX Use a particular encoding for output. Default is\n"
" the same encoding as the input XML file. UTF-8 if\n"
" input XML file has not XML declaration.\n"
" -wfile=xxx Write to a file instead of stdout.\n"
" -wscs=xxx Enable/Disable split-cdata-sections. Default on\n"
" -wddc=xxx Enable/Disable discard-default-content. Default on\n"
" -wflt=xxx Enable/Disable filtering. Default off\n"
" -wfpp=xxx Enable/Disable format-pretty-print. Default off\n"
" -wbom=xxx Enable/Disable write Byte-Order-Mark Default off\n"
" -? Show this help.\n\n"
" * = Default if not provided explicitly.\n\n"
"The parser has intrinsic support for the following encodings:\n"
" UTF-8, USASCII, ISO8859-1, UTF-16[BL]E, UCS-4[BL]E,\n"
" WINDOWS-1252, IBM1140, IBM037, IBM1047.\n"
<< XERCES_STD_QUALIFIER endl;
}


int main(int argC, char* argV[])
{
int retval = 0;

try
{
XMLPlatformUtils::Initialize();
}

catch(const XMLException &toCatch)
{
XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"
<< " Exception message:"
<< StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
return 1;
}

if (argC < 2)
{
usage();
XMLPlatformUtils::Terminate();
return 1;
}

int parmInd;
for (parmInd = 1; parmInd < argC; parmInd++)
{
if (argV[parmInd][0] != '-')
break;

if (!strcmp(argV[parmInd], "-?"))
{
usage();
XMLPlatformUtils::Terminate();
return 2;
}
else if (!strncmp(argV[parmInd], "-v=", 3)
|| !strncmp(argV[parmInd], "-V=", 3))
{
const char* const parm = &argV[parmInd][3];

if (!strcmp(parm, "never"))
gValScheme = XercesDOMParser::Val_Never;
else if (!strcmp(parm, "auto"))
gValScheme = XercesDOMParser::Val_Auto;
else if (!strcmp(parm, "always"))
gValScheme = XercesDOMParser::Val_Always;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -v= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strcmp(argV[parmInd], "-n")
|| !strcmp(argV[parmInd], "-N"))
{
gDoNamespaces = true;
}
else if (!strcmp(argV[parmInd], "-s")
|| !strcmp(argV[parmInd], "-S"))
{
gDoSchema = true;
}
else if (!strcmp(argV[parmInd], "-f")
|| !strcmp(argV[parmInd], "-F"))
{
gSchemaFullChecking = true;
}
else if (!strcmp(argV[parmInd], "-e")
|| !strcmp(argV[parmInd], "-E"))
{
gDoCreate = true;
}
...全文
1186 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq2008666 2009-03-17
  • 打赏
  • 举报
回复
once_and_again 2008-10-17
  • 打赏
  • 举报
回复
fputs("<?xml version='1.0'?><a.elem xmi.version='2.0'/>", textfile);
是文件写进去的,
allenpettle 2008-10-17
  • 打赏
  • 举报
回复
好长哪~~
没看完,不过我想LZ可以自己写一个Filter。
OutIT人 2008-10-17
  • 打赏
  • 举报
回复
<?...?>
匹配<?和?>
之間內容
如果是 刪除
否则 空动作
星羽 2008-10-16
  • 打赏
  • 举报
回复
so long
chenxiaohua 2008-10-16
  • 打赏
  • 举报
回复
我根据xerces-c中的一个例子,代码如上,将dom的内容输出到一个文件中,我不想要文件头中的内容encoding和standalone,该怎么处理.
感觉下面的代码,得到的文件头是<?xml version="1.0" encoding="GBK" standalone="no"?>,如果我想得到<?xml version="1.0" encoding="GBK" ?>或者<?xml version="1.0" ?>这样的文件头,该怎么处理?
chenxiaohua 2008-10-16
  • 打赏
  • 举报
回复
else if (!strncmp(argV[parmInd], "-wenc=", 6))
{
// Get out the encoding name
gOutputEncoding = XMLString::transcode( &(argV[parmInd][6]) );
}
else if (!strncmp(argV[parmInd], "-wfile=", 7))
{
goutputfile = &(argV[parmInd][7]);
}
else if (!strncmp(argV[parmInd], "-wddc=", 6))
{
const char* const parm = &argV[parmInd][6];

if (!strcmp(parm, "on"))
gDiscardDefaultContent = true;
else if (!strcmp(parm, "off"))
gDiscardDefaultContent = false;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -wddc= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}

}
else if (!strncmp(argV[parmInd], "-wscs=", 6))
{
const char* const parm = &argV[parmInd][6];

if (!strcmp(parm, "on"))
gSplitCdataSections = true;
else if (!strcmp(parm, "off"))
gSplitCdataSections = false;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -wscs= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strncmp(argV[parmInd], "-wflt=", 6))
{
const char* const parm = &argV[parmInd][6];

if (!strcmp(parm, "on"))
gUseFilter = true;
else if (!strcmp(parm, "off"))
gUseFilter = false;
else
{
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strncmp(argV[parmInd], "-wfpp=", 6))
{
const char* const parm = &argV[parmInd][6];

if (!strcmp(parm, "on"))
gFormatPrettyPrint = true;
else if (!strcmp(parm, "off"))
gFormatPrettyPrint = false;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -wfpp= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else if (!strncmp(argV[parmInd], "-wbom=", 6))
{
const char* const parm = &argV[parmInd][6];

if (!strcmp(parm, "on"))
gWriteBOM = true;
else if (!strcmp(parm, "off"))
gWriteBOM = false;
else
{
XERCES_STD_QUALIFIER cerr << "Unknown -wbom= value: " << parm << XERCES_STD_QUALIFIER endl;
XMLPlatformUtils::Terminate();
return 2;
}
}
else
{

}
}

if (parmInd + 1 != argC)
{
usage();
XMLPlatformUtils::Terminate();
return 1;
}
gXmlFile = argV[parmInd];

XercesDOMParser *parser = new XercesDOMParser;
parser->setValidationScheme(gValScheme);
parser->setDoNamespaces(gDoNamespaces);
parser->setDoSchema(gDoSchema);
parser->setValidationSchemaFullChecking(gSchemaFullChecking);
parser->setCreateEntityReferenceNodes(gDoCreate);
DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
parser->setErrorHandler(errReporter);


bool errorsOccured = false;
try
{
parser->parse(gXmlFile);
}
catch (const OutOfMemoryException&)
{
XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
errorsOccured = true;
}
catch (const XMLException& e)
{
XERCES_STD_QUALIFIER cerr << "An error occurred during parsing\n Message: "
<< StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
errorsOccured = true;
}

catch (const DOMException& e)
{
errorsOccured = true;
}

catch (...)
{
XERCES_STD_QUALIFIER cerr << "An error occurred during parsing\n " << XERCES_STD_QUALIFIER endl;
errorsOccured = true;
}

if (!errorsOccured && !errReporter->getSawErrors())
{
DOMPrintFilter *myFilter = 0;

try
{
XMLCh tempStr[100];
XMLString::transcode("LS", tempStr, 99);
printf("gOutputEncoding=%s\n", gOutputEncoding);
printf("tempStr=%s\n", tempStr);
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
DOMWriter *theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();

theSerializer->setEncoding(gOutputEncoding);

if (gUseFilter)
{
myFilter = new DOMPrintFilter(DOMNodeFilter::SHOW_ELEMENT |
DOMNodeFilter::SHOW_ATTRIBUTE |
DOMNodeFilter::SHOW_DOCUMENT_TYPE);
theSerializer->setFilter(myFilter);
}

DOMErrorHandler *myErrorHandler = new DOMPrintErrorHandler();
theSerializer->setErrorHandler(myErrorHandler);

if (theSerializer->canSetFeature(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections))
theSerializer->setFeature(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections);

if (theSerializer->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent))
theSerializer->setFeature(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent);

if (theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint))
theSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint);

if (theSerializer->canSetFeature(XMLUni::fgDOMWRTBOM, gWriteBOM))
theSerializer->setFeature(XMLUni::fgDOMWRTBOM, gWriteBOM);

XMLFormatTarget *myFormTarget;
if (goutputfile)
myFormTarget = new LocalFileFormatTarget(goutputfile);
else
myFormTarget = new StdOutFormatTarget();

DOMNode *doc = parser->getDocument();

theSerializer->writeNode(myFormTarget, *doc);
delete theSerializer;

delete myFormTarget;
delete myErrorHandler;

if (gUseFilter)
delete myFilter;

}
catch (const OutOfMemoryException&)
{
retval = 5;
}
catch (XMLException& e)
{
retval = 4;
}

}
else
retval = 4;

delete errReporter;

delete parser;

XMLPlatformUtils::Terminate();

XMLString::release(&gOutputEncoding);

return retval;
}

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧