一。說明
這裏要講的是在win32平臺下怎麼使用vc來調用apache xerces c++接口或者直接把這個項目用到你的項目中去。linux 下的安裝與調用會在下次中給出。
二。 獲取xerces c++ 的軟件包。
· 如果你只想直接調用
xerces c++的包而不想自己編譯的話,可以在清單一獲取二進制包
· 如果你想自己編譯xerces c++的包的話,可以在清單二獲取二進制包
三。使用xerces c++二進制包加到您的項目中
爲了完成這個示例我說明一下我的文件夾路徑的安排。
Myxml----
|------lib
|------include
|------bin
|------myprojects
我用myxml文件夾來存放我們要實驗的所有東西。
Lib 使用來存放xerces c++ 的lib 文件
Include 用來存放xerces 的頭文件。
Bin 用來存放你的項目的中間代碼和exe或者dll文件。
Myprojects 裏面就是你的所有項目。
- 現在我們用vc在Myprojects文件夾下創建一個DomTest的console application空項目項目。
- 解壓縮 Xerces 源 zip文件到一個工作目錄。把//xerces-c-src_2_7_0//lib文件夾的所有文件放到Myxml/lib中。把//xerces-c-src_2_7_0//src文件夾下的xercesc複製到Myxml/include,把//xerces-c-src_2_7_0//lib下的xerces-c_2_7D.dll文件放到myxml/bin中。
- 在Domtest工程中創建兩個文件CXML.CPP和CXML.h,代碼在本文的後面。
- 這樣我們就進行了一半了。一切文件準備就緒。
- 設置Domtest的project ->settings.
1).在general面板上設置intermediate files 爲../../bin。Output files:爲../../bin。
2).在c/c++面板上Category 選擇Preprocessor,在Additional include directories中輸入../../include 。
3).在link面板上Category選擇input,在object/library modules中去掉所有東西,輸入kernel32.lib user32.lib xerces-c_2D.lib.在Additonal library path中輸入../../lib。
3)其他設置保持不變。確定之後build 一下你的項目。如果要運行你的程序一定別忘了在bin下有xerces-c_2_7D.dll文件存在。
這樣你就輕鬆完成apache xerces c++在你項目中的使用了。
以上的這個文件路徑的安排可以按你自己的設置喜好去做,前提是你要明白
Vc中的這個三個設置的意義,您有什麼不理解的地方可以告訴我。
四.編譯xerces c++源代碼並加到您的項目中
這個過程其實多了一個自己編譯代碼,而在這裏提到是因爲很多人直接編譯他碰到了問題,而且把xerces 加到你的項目中也有兩種方式。我的建議是就使用第三點提到的方法,因爲如果你直接把xerces工程加到你的項目中去,編譯比較慢。好了廢話少說。
- 1. 下載完xerces-c-current.zip之後, 解壓縮 Xerces的zip文件到一個工作目錄。Xerces-C++ 有自己的目錄結構,所以應保證在這一步中保持相對路徑名。
- 用 Windows 資源管理器或者習慣使用的文件管理器進入到 //xerces-c-src_2_7_0//Projects//Win32//VC6//xerces-all// 文件夾並單擊 xerces-all.dsw workspace 文件以啓動 Microsoft Developer Studio。
注:這些指導假定您是在 Visual Studio 6 中編譯 Win32 應用程序。對於 Visual Studio dot-NET 或者 Win64 應用程序,在 Win64 或者 VC7 各自的目錄中重複步驟 1和2。
- 在 Developer Studio 中,讓 XercesLib 成爲當前活躍的項目,並按 F7 以編譯 DLL。這個時候可能有三個錯誤,一個在project->settings->link中就是IA64這個改成IX86,在c/c++中去掉/machine:IA64 在category:general的Preprocessor definitions 的win64改成win32再按 F7 以編譯 DLL
- 生成的文件在xerces-c-src_2_3_0下相應文件名夾中找到。並按上面第三大點進行使用。
注:如果你在上面的編譯是有這兩個錯誤:F:/Downloads/xerces-c-current/xerces-c-src_2_7_0/src/xercesc/util/Platforms/Win32/Win32PlatformUtils.cpp(690) : error C2039: 'InterlockedCompareExchangePointer' : is not a member of '`global namespace''
F:/Downloads/xerces-c-current/xerces-c-src_2_7_0/src/xercesc/util/Platforms/Win32/Win32PlatformUtils.cpp(690) : error C2065: 'InterlockedCompareExchangePointer' : undeclared identifier
的話,那是因爲apache在這個project中默認是win64的平臺。所以您只要Project ->setting左邊選中XercesLib 項目,再在右邊的C++選項中 category中選中General 後,在下面的倒數第二個Preprocessor definitions的第一個win64改成win32 確定之後,clean你的XercesLib 項目,Rebuild。
就可以在xerces-c-src_2_7_0/Build/Win64/VC6/Release裏面找到你要使用的dll和lib了,
代碼一
//CXML.cpp
#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <list>
#include "CXML.h"
CXML::CXML()
{
try
{
// Initialize Xerces-C++ library
XMLPlatformUtils::Initialize();
}
catch(xercesc::XMLException & excp)
{
char* msg = XMLString::transcode(excp.getMessage());
printf("XML toolkit initialization error: %s/n", msg);
XMLString::release(&msg);
}
//創建 XercesDOMParser 對象,用於解析文檔
m_DOMXmlParser = new XercesDOMParser;
}
CXML::~CXML()
{
try
{
XMLPlatformUtils::Terminate();
}
catch(XMLException& excp)
{
char* msg = XMLString::transcode(excp.getMessage());
printf("XML toolkit terminate error: %s/n", msg);
XMLString::release(&msg);
}
}
void CXML::xmlParser(string & xmlFile) throw( std::runtime_error )
{
//獲取文件信息狀態
//配置DOMParser
m_DOMXmlParser->setValidationScheme( XercesDOMParser::Val_Auto );
m_DOMXmlParser->setDoNamespaces( false );
m_DOMXmlParser->setDoSchema( false );
m_DOMXmlParser->setLoadExternalDTD( false );
try
{
//調用 Xerces C++ 類庫提供的解析接口
m_DOMXmlParser->parse(xmlFile.c_str()) ;
//獲得DOM樹
DOMDocument* xmlDoc = m_DOMXmlParser->getDocument();
DOMElement *pRoot = xmlDoc->getDocumentElement();
if (!pRoot )
{
throw(std::runtime_error( "empty XML document" ));
}
// create a walker to visit all text nodes.
/**********************************************
DOMTreeWalker *walker =
xmlDoc->createTreeWalker(pRoot, DOMNodeFilter::SHOW_TEXT, NULL, true);
// use the tree walker to print out the text nodes.
std::cout<< "TreeWalker:/n";
for (DOMNode *current = walker->nextNode(); current != 0; current = walker->nextNode() )
{
char *strValue = XMLString::transcode( current->getNodeValue() );
std::cout <<strValue;
XMLString::release(&strValue);
}
std::cout << std::endl;
*************************************************/
// create an iterator to visit all text nodes.
DOMNodeIterator* iterator = xmlDoc->createNodeIterator(pRoot,
DOMNodeFilter::SHOW_TEXT, NULL, true);
// use the tree walker to print out the text nodes.
std::cout<< "iterator:/n";
for ( DOMNode * current = iterator->nextNode();
current != 0; current = iterator->nextNode())
{
string strValue =XMLString::transcode(current->getNodeValue());
std::cout <<strValue<<endl;
}
std::cout<< std::endl;
}
catch( xercesc::XMLException& excp )
{
char* msg = xercesc::XMLString::transcode( excp.getMessage() );
ostringstream errBuf;
errBuf << "Error parsing file: " << msg << flush;
XMLString::release( &msg );
}
}
int main(int argc ,char* argv[])
{
if(argc!=2)
{
return 0;
}
string xmlFile = argv[1];
CXML cxml;
cxml.xmlParser(xmlFile);
return 0;
}
代碼二
//CXML.h
#ifndef XML_PARSER_HPP
#define XML_PARSER_HPP
#include <xercesc/util/TransService.hpp>
#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/dom/DOMAttr.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <xercesc/framework/XMLFormatter.hpp>
#include <xercesc/util/XMLString.hpp>
#include <stdlib.h>
#include <string>
#include <vector>
#include <stdexcept>
using namespace std;
using namespace xercesc;
class CXML
{
public:
CXML();
~CXML();
XMLTransService::Codes tranServiceCode;
void xmlParser(string&) throw(std::runtime_error);
private:
xercesc::XercesDOMParser *m_DOMXmlParser; //定義解析對象
};
#endif