昨天公司老大突然说将vs项目中的数据用xml保存起来,叫我用xerces-c++库,说很快可以搞定的,我当场就慌了,因为我还没有试过将库应用在vs项目中,于是查了各种资料,最后总结了C++各种库的应用。
现在先来讲一下如何将xerces-c++库应用在vs项目上,在xerces-c++官网下载xerces-c++库http://xerces.apache.org/xerces-c/download.cgi
解压。
新建vs的win32空项目,打开项目属性,配置属性->c/c++->常规->附加包含目录
在解压后的文件夹中找到xerces-c-3.1.2\xerces-c-3.1.2\src的所在目录,将目录加进去,这样你就可以在项目中包含xerces库的头文件,但包含头文件还不能用,因为头文件大部分是接口声明,源代码是封装在了dll和lib里面(dll和lib的作用可以上网查)。
所以添加头文件后还要添加dll和lib,再次打开项目属性,配置属性->链接器->常规->附加库目录
在解压的文件夹中搜索xerces-c_3D.lib文件,将lib文件路径加进去,配置属性->链接器->输入->附加依赖项
添加xerces-c_3D.lib。
接下来还差dll文件,在解压到文件夹中搜索xerces-c_3_1D.dll文件,将dll文件放到系统目录中(32位电脑的系统目录是C:\Windows\System32,64位电脑的系统目录是C:\Windows\SysWOW64),或者将dll文件放到项目当前目录中。
接下来测试一下代码,代码运行后会在项目当前目录生成一个outfile.xml文件。
#include <iostream>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
XERCES_CPP_NAMESPACE_USE
using namespace std;
// StrXML class copied from Xerces-2.8 distro.
class StrXML
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
StrXML(const char* const toTranscode)
{
// Call the private transcoding method
fLocalForm = XMLString::transcode(toTranscode);
}
~StrXML()
{
XMLString::release(&fLocalForm);
}
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
const XMLCh* utf16() const
{
return fLocalForm;
}
private :
// -----------------------------------------------------------------------
// Private data members
//
// fLocalForm
// This is the local code page form of the string.
// -----------------------------------------------------------------------
XMLCh* fLocalForm;
};
int main (int , char **) {
XMLPlatformUtils::Initialize();
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(StrXML("").utf16());
if (impl == NULL)
throw string("Implementation is NULL in create document.");
// Create the document with one tag
DOMDocument *doc = impl->createDocument(0, StrXML("session").utf16(),
impl->createDocumentType(StrXML("dummy").utf16(),
0, 0));
DOMLSSerializer *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
DOMLSOutput *theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
DOMConfiguration *configuration = theSerializer->getDomConfig();
//a small document tree .begin
DOMElement* root = doc->getDocumentElement();
DOMElement* e1 = doc->createElement(StrXML("FirstElement").utf16());
e1->setAttribute(StrXML("attributeName").utf16(),StrXML("attributeValue").utf16());
root->appendChild(e1);
DOMText* textNode = doc->createTextNode(StrXML("aTextNode").utf16());
e1->appendChild(textNode);
//a small document tree .end
// Have a nice output
if (configuration->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
configuration->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
// Add the declaration?
if (configuration->canSetParameter(XMLUni::fgDOMXMLDeclaration, true))
configuration->setParameter(XMLUni::fgDOMXMLDeclaration, true);
LocalFileFormatTarget *myFormTarget = new LocalFileFormatTarget(StrXML("outfile.xml").utf16());
theOutput->setByteStream(myFormTarget);
if (doc->getDoctype() == NULL) {
cerr << "The doc type is null./n";
exit(-1);
}
theSerializer->write(doc, theOutput);
DOMRange *range = doc->createRange();
range->release();
theOutput->release();
theSerializer->release();
XMLPlatformUtils::Terminate();
}
这就是xerces-c++库的使用,在我看来,大多数C++外部库都是这样使用的,添加头文件所在路径,再添加dll和lib就可以写代码了。
如果我说得不对请指出,大家试过有什么问题欢迎提问。