建立本地XML文件输入数据的方法
XMLPlatformUtils::Initialize();
////////////////////////////////////////////////////////////////////////////////
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation( XMLString::transcode("Test"));
//生成本地文件,已存在的会被清空,不存在的会自动创建
XMLFormatTarget * Targe;
Targe = new LocalFileFormatTarget( "testXML.xml" );
//输出流指向文件
DOMLSOutput * Output = impl->createLSOutput();
Output->setByteStream( Targe );
//生成序列器
DOMLSSerializer * Serializer = impl->createLSSerializer();
//文档操作
DOMDocument * doc = impl->createDocument( 0 , XMLString::transcode("Test"), 0 );
//......
//写入到文件
Serializer->write( doc , Output );
delete Targe;
Output ->release();
doc ->release()
Serializer->release();
////////////////////////////////////////////////////////////////////////////////
XMLPlatformUtils::Terminate();
//从本地XML文件中读取数据的方法,还有很多SETTER可以设置解释器的属性
XercesDOMParser * parser = new XercesDOMParser();
parser->parse("testXML.xml");
DOMDocument * doc = parser->getDocument();
或
DOMLSParser * myParser = impl->createLSParser( DOMImplementationLS::MODE_SYNCHRONOUS , 0 );
DOMDocument * doc = myParser->parseURI( XMLString::transcode( "testXML.xml" ) );
//出现错误将会抛出异常(错误信息),并返回NULL,正常则返回有效 DOMDocument(根)
//同一层次的访问
DOMNode * ChildNode = Node->getFirstChild();
for( ; ChildNode != NULL ; ChildNode = ChildNode ->getNextSibling() )
{
char *strValue = XMLString::transcode( ChildNode ->getNodeName() ); //返回的是动态生成的,需要自行释放
std::cout << strValue <<endl;
XMLString::release(&strValue); //释放
}
//对整棵树的遍历
DOMTreeWalker * Walker = doc->createTreeWalker( root ,DOMNodeFilter::SHOW_ELEMENT , NULL , false );
for( DOMNode *Node = Walker->nextNode() ; Node != NULL ; Node = Walker->nextNode() )
{
char *strValue = XMLString::transcode( Node->getNodeName() );
std::cout << strValue <<endl;
XMLString::release(&strValue);
}