apache xerces c++ windows 下编译与vc6实例

来源:互联网 发布:黑楼孤魂知乎 编辑:程序博客网 时间:2024/06/03 15:51
今天要讲的是怎么使用apache xerces c++ 在vc中的安装使用。
 
一。说明
 这里要讲的是在win32平台下怎么使用vc来调用apache xerces c++接口或者直接把这个项目用到你的项目中去。linux 下的安装与调用会在下次中给出。
 
二。 获取xerces c++ 的软件包。
 
· 如果你只想直接调用xerces c++的包而不想自己编译的话,可以在清单一获取二进制包
清单一 xerces-c_2_7_0-windows_2000-msvc_60.zip
 
· 如果你想自己编译xerces c++的包的话,可以在清单二获取二进制包
 清单二  xerces-c-current.zip
三。使用xerces c++二进制包加到您的项目中
   
 下栽xerces-c_2_7_0-windows_2000-msvc_60.zip完毕之后我们开始下面的内容: 
    
     为了完成这个示例我说明一下我的文件夹路径的安排。
     Myxml----
              |------lib
              |------include
              |------bin
              |------myprojects
          我用myxml文件夹来存放我们要实验的所有东西。
Lib 使用来存放xerces c++ 的lib 文件
Include 用来存放xerces 的头文件。
Bin 用来存放你的项目的中间代码和exe或者dll文件。
Myprojects 里面就是你的所有项目。
  1. 现在我们用vc在Myprojects文件夹下创建一个DomTest的console application空项目项目。
  2. 解压缩 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中。
  3. Domtest工程中创建两个文件CXML.CPP和CXML.h,代码在本文的后面。
  4. 这样我们就进行了一半了。一切文件准备就绪。
  5. 设置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. 1. 下载完xerces-c-current.zip之后, 解压缩 Xerces的zip文件到一个工作目录。Xerces-C++ 有自己的目录结构,所以应保证在这一步中保持相对路径名。
  2. 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。
  1. Developer Studio 中,让 XercesLib 成为当前活跃的项目,并按 F7 以编译 DLL。这个时候可能有三个错误,一个在project->settings->link中就是IA64这个改成IX86,在c/c++中去掉/machine:IA64 在category:general的Preprocessor definitions 的win64改成win32再按 F7 以编译 DLL
  2. 生成的文件在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
 
本文实现吸收了http://www-128.ibm.com/developerworks/cn/xml/x-xercc
和http://www.huisoft.com.cn/redirect.php?tid=151&goto=lastpost
 
如果您有什么问题可以和我交流我最近也是在使用这个东西,主要还是dom的解析,以后帖出关于dom的使用文章。
原创粉丝点击