#include <iostream>
using namespace std;
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/dom/DOM.hpp>
#pragma comment(lib,"xerces-c_3.lib")
using namespace xercesc;
int main()
{
// 一、xml文档解析之“建立本地XML文件输入数据的方法”
// 初始化XML类
XMLPlatformUtils::Initialize();
// 获取DOM实例
XMLCh tempStr[100];
XMLString::transcode("Range", tempStr, 99);
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
//生成本地文件,已存在的会被清空,不存在的会自动创建
XMLFormatTarget* Targe = NULL;
Targe = new LocalFileFormatTarget( "testXML.xml" );
//输出流指向文件
DOMLSOutput* Output = impl->createLSOutput();
Output->setByteStream( Targe );
//生成序列器
DOMLSSerializer * Serializer = impl->createLSSerializer();
//创建xml文档
XMLString::transcode("root", tempStr, 99);
DOMDocument* doc = impl->createDocument(0, tempStr, 0);
//获取xml文档根节点
DOMElement* root = doc->getDocumentElement();
// 创建文档节点
XMLString::transcode("FirstElement", tempStr, 99);
DOMElement* e1 = doc->createElement(tempStr);
//追加到根节点下面
root->appendChild(e1);
XMLString::transcode("SecondElement", tempStr, 99);
DOMElement* e2 = doc->createElement(tempStr);
XMLCh tempStr1[100];
XMLString::transcode("Name", tempStr1, 99);
XMLCh tempStr2[100];
XMLString::transcode("Value", tempStr2, 99);
e2->setAttribute(tempStr1, tempStr2);
//追加到根节点下面,和e1同级
root->appendChild(e2);
/*
XMLString::transcode("ThreeElement", tempStr, 99);
DOMElement* e3 = doc->createElement(tempStr);
XMLCh tempStr3[100];
XMLString::transcode("SecondElement", tempStr2, 99);
root->insertBefore(e3, e1);
XMLString::transcode("aTextNode", tempStr, 99);
DOMText* textNode = doc->createTextNode(tempStr);
e1->appendChild(textNode);
cout << XMLString::transcode(textNode->getNodeValue()) << endl;
cout << XMLString::transcode(e2->getTagName()) << endl;
//optionally, call release() to release the resource associated with the range after done
DOMRange* range = doc->createRange();
range->release();
//removedElement is an orphaned node, optionally call release() to release associated resource
DOMNode* removedElement = root->removeChild(e2);
removedElement->release();
XMLString::transcode("*", tempStr, 99);
DOMNodeList* nodeList = doc->getElementsByTagName(e1->getTagName());
if (NULL != nodeList)
{
XMLSize_t size = nodeList->getLength();
XMLSize_t szBegin = 0;
for (; szBegin < size; ++szBegin)
{
DOMNode* pDOMNode = nodeList->item(szBegin);
if (NULL != pDOMNode)
{
cout << "pDOMNode->getNodeName() = " << XMLString::transcode(pDOMNode->getNodeName()) << endl;
if (NULL != pDOMNode->getNodeValue())
{
cout << "pDOMNode->getNodeValue() = " << XMLString::transcode(pDOMNode->getNodeValue())<< endl;
}
else
{
cout << "pDOMNode->getNodeValue() is NULL." << endl;
}
}
}
}
*/
//写入到文件
Serializer->write( doc , Output );
delete Targe;
Output->release();
doc->release();
Serializer->release();
XMLPlatformUtils::Terminate();
return 0;
}
using namespace std;
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/dom/DOM.hpp>
#pragma comment(lib,"xerces-c_3.lib")
using namespace xercesc;
int main()
{
// 一、xml文档解析之“建立本地XML文件输入数据的方法”
// 初始化XML类
XMLPlatformUtils::Initialize();
// 获取DOM实例
XMLCh tempStr[100];
XMLString::transcode("Range", tempStr, 99);
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
//生成本地文件,已存在的会被清空,不存在的会自动创建
XMLFormatTarget* Targe = NULL;
Targe = new LocalFileFormatTarget( "testXML.xml" );
//输出流指向文件
DOMLSOutput* Output = impl->createLSOutput();
Output->setByteStream( Targe );
//生成序列器
DOMLSSerializer * Serializer = impl->createLSSerializer();
//创建xml文档
XMLString::transcode("root", tempStr, 99);
DOMDocument* doc = impl->createDocument(0, tempStr, 0);
//获取xml文档根节点
DOMElement* root = doc->getDocumentElement();
// 创建文档节点
XMLString::transcode("FirstElement", tempStr, 99);
DOMElement* e1 = doc->createElement(tempStr);
//追加到根节点下面
root->appendChild(e1);
XMLString::transcode("SecondElement", tempStr, 99);
DOMElement* e2 = doc->createElement(tempStr);
XMLCh tempStr1[100];
XMLString::transcode("Name", tempStr1, 99);
XMLCh tempStr2[100];
XMLString::transcode("Value", tempStr2, 99);
e2->setAttribute(tempStr1, tempStr2);
//追加到根节点下面,和e1同级
root->appendChild(e2);
/*
XMLString::transcode("ThreeElement", tempStr, 99);
DOMElement* e3 = doc->createElement(tempStr);
XMLCh tempStr3[100];
XMLString::transcode("SecondElement", tempStr2, 99);
root->insertBefore(e3, e1);
XMLString::transcode("aTextNode", tempStr, 99);
DOMText* textNode = doc->createTextNode(tempStr);
e1->appendChild(textNode);
cout << XMLString::transcode(textNode->getNodeValue()) << endl;
cout << XMLString::transcode(e2->getTagName()) << endl;
//optionally, call release() to release the resource associated with the range after done
DOMRange* range = doc->createRange();
range->release();
//removedElement is an orphaned node, optionally call release() to release associated resource
DOMNode* removedElement = root->removeChild(e2);
removedElement->release();
XMLString::transcode("*", tempStr, 99);
DOMNodeList* nodeList = doc->getElementsByTagName(e1->getTagName());
if (NULL != nodeList)
{
XMLSize_t size = nodeList->getLength();
XMLSize_t szBegin = 0;
for (; szBegin < size; ++szBegin)
{
DOMNode* pDOMNode = nodeList->item(szBegin);
if (NULL != pDOMNode)
{
cout << "pDOMNode->getNodeName() = " << XMLString::transcode(pDOMNode->getNodeName()) << endl;
if (NULL != pDOMNode->getNodeValue())
{
cout << "pDOMNode->getNodeValue() = " << XMLString::transcode(pDOMNode->getNodeValue())<< endl;
}
else
{
cout << "pDOMNode->getNodeValue() is NULL." << endl;
}
}
}
}
*/
//写入到文件
Serializer->write( doc , Output );
delete Targe;
Output->release();
doc->release();
Serializer->release();
XMLPlatformUtils::Terminate();
return 0;
}