Xercesc使用指南
作者:Kagula
日期:2009/10/14
读者对象
有在VS2008中,使用C++语言开发的初步经验。
简介:
利用示例,表述Xercesc3.07软件开发包DOM API的使用
摘要:
Xercesc DOM
环境:
[1]VisualStudio2008 with SP1 C++
[2]Xercesc3.01
正文:
第一部分:初始化与Terminate
假设Xercesc安装在D:/SDK/xerces-c-3.0.1-x86-windows-vc-9.0位置
在VS2008中设置D:/SDK/xerces-c-3.0.1-x86-windows-vc-9.0/include为头文件路径
在VS2008中设置D:/SDK/xerces-c-3.0.1-x86-windows-vc-9.0/lib为库文件路径
复制D:/SDK/xerces-c-3.0.1-x86-windows-vc-9.0/bin位置下的xerces-c_3_0.dll文件和xerces-c_3_0D.dll文件到你项目目录中。
下面的代码为:Xercesc初始化和Terminate的例子
#ifndef _DEBUG
#pragma comment( lib, "xerces-c_3.lib" )
#else
#pragma comment( lib, "xerces-c_3D.lib" )
#endif
#include <xercesc/util/PlatformUtils.hpp>
// Other include files, declarations, and non-Xerces-C++ initializations.
using namespace xercesc;
int main(int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
第二部分:读取xml文件
本节包含内容:XML文件格式、判断节点类型、取节点名称、取节点值。
XML文件的典型格式,如下
<节点名称1>
<节点名称1-1 属性名1=属性值1 属性名2=属性值2>
<节点名称1-1-1> <!- 节点名为 节点名称1-1-1节点值无 -->
Context <!- 节点名为#text 节点值为Context -->
<!- 若Context为空,则没有#text节点 -->
</节点名称1-1-1>
</节点名称1-1>
<节点名称 1-2>
</节点名称1-2>
</节点名称1>
参考Domcount例程,得到DOMNode对象
首先判断DOMNode对象的节点类型、然后,对节点内容和值分别存储。
if (static_cast<DOMNode *>(n)->getNodeType() == DOMNode::ELEMENT_NODE)
{
//取节点名称。这里取节点值是没有意义的
char *name = XMLString::transcode(static_cast<DOMNode *>(n)->getNodeName());
pThis->m_strName = name; //存放节点名称
XMLString::release(&name);
//如果本节点有属性,存储属性信息到pThis对象.begin
if(static_cast<DOMNode *>(n)->hasAttributes()) {
// get all the attributes of the node
DOMNamedNodeMap *pAttributes = static_cast<DOMNode *>(n)->getAttributes();
const XMLSize_t nSize = pAttributes->getLength();
std::string strKey,strValue; //用来存放属性名和属性值
for(XMLSize_t i=0;i<nSize;++i) {
DOMAttr *pAttributeNode = (DOMAttr*) pAttributes->item(i);
// get attribute name
char *name = XMLString::transcode(pAttributeNode->getName());
// get attribute type
char *value = XMLString::transcode(pAttributeNode->getValue());
//存放属性名和属性值
strKey = name,strValue=value,pThis->m_mapAttr[strKey]=strValue;
XMLString::release(&name);
XMLString::release(&value);
}
//如果本节点有属性,存储属性信息到pThis对象.end
}
Else if (static_cast<DOMNode *>(n)->getNodeType() == DOMNode::TEXT_NODE)
{
//节点名为#text的节点,可以取其节点值
//… omit some code …
char *value = XMLString::transcode(static_cast<DOMNode *>(n)->getNodeValue());
//… omit some code …
}
备注:Xercesc直接支持UTF-8编码的中文。
第三部分:根据内存中的数据产生xml文件
这里通过一个完整的例子来举例
#include <iostream>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#pragma comment(lib,"xerces-c_3")
XERCES_CPP_NAMESPACE_USE
using namespace std;
// StrXML class copied from Xerces-2.8 distro.
class StrXML
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------