2009年5月8日星期五

xerces-c环境配置以及测试

xerces-c环境配置。
编译xerces projects目录下的lib工程会生成2个文件dll和lib。
然后添加src目录。
include D:\vs2005\xerces-c-3.0.1\src
library D:\vs2005\xerces-c-3.0.1\Build\Win32\VC9\Release
就是编译生成lib和dll的那个目录

然后在projects-property下链接xerces-c_3.lib
或者#pragma comment(lib,"xerces-c_3.lib")也行

好配置成功了。

下面该做练习了。
我随意在google找了个代码,自己敲上去,有bug,修改了下,
成功解析。
代码还是蛮简单的。


游戏编程精粹里面有讲到数据驱动的,以后数据就存xml。。。

上代码和发图才是王道

sample.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<ApplicationSettings option_a="10" option_b="24">
</ApplicationSettings>

<OtherStuff option_x="500">
</OtherStuff>
</root>




parser.h
#ifndef XML_PARSER_H
#define XML_PARSER_H


#include <xercesc\dom\DOM.hpp>
#include <xercesc\dom\DOMDocument.hpp>
#include <xercesc\dom\DOMDocumentType.hpp>
#include <xercesc\dom\DOMElement.hpp>
#include <xercesc\dom\DOMImplementation.hpp>
#include <xercesc\dom\DOMImplementationLS.hpp>
#include <xercesc\dom\DOMNodeIterator.hpp>
#include <xercesc\dom\DOMNodeList.hpp>
#include <xercesc\dom\DOMText.hpp>
#include <xercesc\parsers\XercesDOMParser.hpp>
#include <xercesc\util\XMLUni.hpp>

#include <string>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

enum
{
ERROR_ARGS = 1,
ERROR_XERCES_INIT,
ERROR_PARSE,
ERROR_EMPTY_DOCUMENT
};

class GetConfig
{
public:
GetConfig();
~GetConfig();
void readConfigFile(std::string&) throw(std::runtime_error);

char* getOptionA() {return m_optionA;}
char* getOptionB() {return m_optionB;}
private:
xercesc::XercesDOMParser* m_ConfigFileParser;
char* m_optionA;
char* m_optionB;

XMLCh* TAG_root;
XMLCh* TAG_ApplicationSettings;
XMLCh* ATTR_OptionA;
XMLCh* ATTR_OptionB;
};

parser.cpp

#include "Parser.h"

using namespace std;
using namespace xercesc;

GetConfig::GetConfig()
{
try
{
XMLPlatformUtils::Initialize();
}
catch(XMLException& e)
{
char* message = XMLString::transcode(e.getMessage());
cerr << "XML toolKit initializtion error:"
<< message << endl;
XMLString::release(&message);
}

TAG_root = XMLString::transcode("root");
TAG_ApplicationSettings = XMLString::transcode("ApplicationSettings");


ATTR_OptionA = XMLString::transcode("option_a");
ATTR_OptionB = XMLString::transcode("option_b");

m_ConfigFileParser = new XercesDOMParser;
}

GetConfig::~GetConfig()
{
try
{
XMLPlatformUtils::Terminate();
}
catch(xercesc::XMLException& e)
{
char* message = XMLString::transcode(e.getMessage());
cerr << "XML toolkit teerdown error" << message << endl;
XMLString::release(&message);
}
}

void GetConfig::readConfigFile(std::string& configfile)
throw(std::runtime_error)
{
struct stat fileStatus;

int iretStat = stat(configfile.c_str(),&fileStatus);
if (iretStat == ENOENT)
throw(std::runtime_error("Path name does not exsit"));else if(iretStat == ENOTDIR)
throw(std::runtime_error("a component of the path is not a directory"));
//else if(iretStat == ELOOP)
// throw(std::runtime_error("Too many symbolic links encountered while
traversing the path"));
else if(iretStat == EACCES)
throw(std::runtime_error("Permission denied"));
else if(iretStat == ENAMETOOLONG)
throw(std::runtime_error("File can not be read\n"));

m_ConfigFileParser->setValidationScheme(XercesDOMParser::Val_Never);
m_ConfigFileParser->setDoNamespaces(false);
m_ConfigFileParser->setDoSchema(false);
m_ConfigFileParser->setLoadExternalDTD(false);

try
{
m_ConfigFileParser->parse(configfile.c_str());

DOMDocument* xmlDoc = m_ConfigFileParser->getDocument();

DOMElement* elementroot = xmlDoc->getDocumentElement();

if(! elementroot) throw(std::runtime_error("empty XML document"));

DOMNodeList* children = elementroot->getChildNodes();
const XMLSize_t nodeCount = children->getLength();

for(XMLSize_t xx = 0; xx < nodeCount; xx++)
{
DOMNode* currentNode = children->item(xx);

if (currentNode->getNodeType() &&
currentNode->getNodeType() == DOMNode::ELEMENT_NODE)
{
DOMElement* currentElement = dynamic_cast<xercesc::DOMElement*>(currentNode);
if (XMLString::equals(currentElement->getTagName(),TAG_ApplicationSettings))
{
const XMLCh* xmlch_OptionA = currentElement->getAttribute(ATTR_OptionA);
m_optionA = XMLString::transcode(xmlch_OptionA);
const XMLCh* xmlch_OptionB = currentElement->getAttribute(ATTR_OptionB);
m_optionB = XMLString::transcode(xmlch_OptionB);
break;
}
}
}
}

catch(xercesc::XMLException& e)
{
char* message = xercesc::XMLString::transcode(e.getMessage());
ostringstream errBuf;
errBuf << "Error parseing file" << message << flush;
XMLString::release(&message);
}
}

test.cpp
#include <iostream>
#include "Parser.h"
#include <string>
using namespace std;


int main()
{
string configfilename = "sample.xml";
GetConfig appConfig;
appConfig.readConfigFile(configfilename);

cout << "Application Option A=" << appConfig.getOptionA() << endl;
cout << "Application Option B=" << appConfig.getOptionB() << endl;
return 0;

}

1 条评论:

匿名 说...

http://hi.baidu.com/superql/blog/item/5d114b2c17aec8e48b139958.html
Qt的评论