MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot ;
// 创建DOMDocument对象
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if ( ! SUCCEEDED(hr))
{
MessageBox( " 无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库! " );
return ;
}
// 根节点的名称为Book
// 创建元素并添加到文档中
xmlRoot = pDoc -> createElement((_bstr_t) " Book " );
// 设置属性
xmlRoot -> setAttribute( " id " ,( const char * )m_strId);
pDoc -> appendChild(xmlRoot);
MSXML2::IXMLDOMElementPtr pNode;
// 添加“author”元素
pNode = pDoc -> createElement((_bstr_t) " Author " );
pNode -> Puttext((_bstr_t)( const char * )m_strAuthor);
xmlRoot -> appendChild(pNode);
// 添加“Title”元素
pNode = pDoc -> createElement( " Title " );
pNode -> Puttext(( const char * )m_strTitle);
xmlRoot -> appendChild(pNode);
// 保存到文件
// 如果不存在就建立,存在就覆盖
pDoc -> save( " d://he.xml " );
------------------------------------------------------------------------------------------------------------------------------------------------
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr;
hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if (FAILED(hr))
{
MessageBox( " 无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库! " );
return ;
}
// 加载文件
pDoc -> load( " d://he.xml " );
MSXML2::IXMLDOMNodePtr pNode;
// 在树中查找名为Book的节点," // "表示在任意一层查找
pNode = pDoc -> selectSingleNode( " //Book " );
MSXML2::DOMNodeType nodeType;
// 得到节点类型
pNode -> get_nodeType( & nodeType);
// 节点名称
CString strName;
strName = ( char * )pNode -> GetnodeName();
// 节点属性,放在链表中
MSXML2::IXMLDOMNamedNodeMapPtr pAttrMap = NULL;
MSXML2::IXMLDOMNodePtr pAttrItem;
_variant_t variantvalue;
pNode -> get_attributes( & pAttrMap);
long count;
count = pAttrMap -> get_length( & count);
pAttrMap -> get_item( 0 , & pAttrItem);
// 取得节点的值
pAttrItem -> get_nodeTypedValue( & variantvalue);
m_strId = ( char * )(_bstr_t)variantvalue;
// 添加整个文档的根节点
void AddRootNode( CString strRootNode , CString strText = "")
{
m_pElement = m_pDoc->createElement( (LPCTSTR)strRootNode );
m_pElement ->put_text( _bstr_t( (LPCTSTR)strText) );
m_pDoc->appendChild( m_pElement );
}
三种最流行的开放源码 XML 库 是 expat、libxml 和 Xerces。这三者都是跨平台的,每一种都充当 XSLT 库 实现的基础,一旦满足了基本 XML 需要之后,它就会给您一条成长途径。
- expat 是 James Clark 创始的开放源码面向事件的 XML 解析库 。他已经将该项目转让给了 SourceForge 中的一个小组。有一个 SAX 封装器可供使用。在许多项目中都可以找到 expat 解析器,如开放源码浏览器 Mozilla、XSLT 处理器 Transformiix 和 RDF 工具 repat。
- libxml 为类似于 SAX 和 DOM 的操作提供了双重方式 API。它支持对 DTD 的验证,并且在 Gnome 的 XSLT 处理器libxslt 中使用。libxml 经过重写,已作为 libxml(2) 发布,也许还称为 libxml2 。这个库 的用户应该确保他们拥有了当前版本。
- Xerces 是非常坚固的、拥有大量文档的库 ,它充当 IBM alphaWorksXML 4C 库 的基础。Xerces 还用于 Apache XSLT 处理器 Xalan 中。Xerces 支持 DOM、SAX,以及对 DTD 的验证。最新版本读取并解释了部分“W3CXML 模式推荐”(打算到 2001 年底实现完整的XML 模式支持)。
附:C/C++ 开发人员的解析器
库 | 供应商 | 事件 | 文档 | 特点 | 许可证 |
expat | James Clark/expat 小组 | 本机与 SAX | - | 带有本机 API 和 SAX 封装器的、非常快速的推模型解析器。 | LGPL( 免费 ) |
libxml | Gnome | SAX | DOM | 非常强壮;SAX 与 DOM 封装器;执行 DTD 验证 | LGPL(免费 ) |
MSXML | Microsoft | SAX | DOM | Win32 的 Microsoft XML 库 | EULA(免费 ) |
Xerces | Apache Software Foundation | SAX | DOM | 执行 SAX 及 DOM 级别 1 和 2;DTD 验证;增加的 XML 模式 | Apache(免费 ) |
XTL | Vivid Creations | SAX | DOM | 带 SAX 和 DOM 的基于 STL 的 XML 工具箱 | 商业 |
RXP | 爱丁堡大学 | - | 本机 | 验证以 C 编写的了解名称空间的 XML 解析器 | GPL(免费 ) |
XML 4C | IBM alphaWorks | SAX | DOM | IBM 发起的 Xerces 的变体 | Apache(免费 ) |
Oracle XDK 8i | Oracle | SAX | DOM | Oracle 主办的用于 C++ 的 XML 工具箱 | 非商业 |
Pull Parser | Extreme! Lab | - | 本机 | 印地安那大学发起的用于 C++ 的轻量型 XML 工具箱 | 非商业 |
XML Booster | PhiDaNi Software | - | 本机 | 解析器发生器,生成 C 源码解析器 | 商业 |
-------------------------------------------------------Boost读取XML配置文件-----------------------------------------------------------------------------------------------------------
前两天因工作需要写了个xml配置脚本解析的功能类,虽说有N种方式可以实现,但考虑到
Boost库在此方面的易操作性(虽支持不够健全,如Unicode支持等)所以封装了一下,具体如下:
//CProcessXmlConfigFile.h(此类由Dll导出)
#i nclude "stdafx.h"
#pragma once
#i nclude <boost/noncopyable.hpp>
#i nclude <boost/property_tree/ptree.hpp>
#i nclude <boost/property_tree/xml_parser.hpp>
#i nclude <boost/tuple/tuple.hpp>
#i nclude <boost/shared_ptr.hpp>
#i nclude <list>
#i nclude "header.h"
#define MAX_PANLIST_NUM
#define MAX_FNAME_LEN
using namespace boost::property_tree;
typedef ptree xmlParser;
typedef vector<string> panNames_t;
typedef boost::shared_ptr<string> sh_pstr;
typedef boost::tuples::tuple<sh_pstr, size_t, sh_pstr> node_t;
typedef std::list<node_t > ptree_nodes_t;
//
class AFX_EXT_CLASS CProcessXmlConfigFile :
{
public:
private:
private:
};
//CProcessXmlConfigFile.cpp
#i nclude "StdAfx.h"
#i nclude "ProcessXmlConfigFile.h"
#i nclude <iostream>
// #i nclude <boost/foreach.hpp>
#i nclude <boost/format.hpp>
#i nclude <boost/typeof/typeof.hpp>
#i nclude <boost/make_shared.hpp>
// #i nclude <boost/ref.hpp>
// #i nclude <boost/program_options/detail/convert.hpp>
// #i nclude <boost/program_options/detail/utf8_codecvt_facet.hpp>
// #i nclude <windows.h>
// #i nclude <stdlib.h>
using namespace std;
using namespace boost;
//参数默认值..
const int iDefaultInt = 0;
const string strNullString = "";
const char chflag = '/';
const string strPan = "doc/pans/pan";
const string strflag = "<xmlattr>";
const wstring win32_dir_splitchar = L"\\";
//
CProcessXmlConfig:CProcessXmlConfigFile(const string& xmlfile)
{
}
CProcessXmlConfig:~CProcessXmlConfigFile(void)
{
}
CProcessXmlConfigFile& CProcessXmlConfig:instance(const string& xmlfile)
{
}
void CProcessXmlConfig:getPanListInfo( void )
{
}
void CProcessXmlConfig:getTreeInfoOfPan( PanIndex _index )
{
}
bool CProcessXmlConfig:ICanWork( PanIndex _index )
{
}
bool CProcessXmlConfig:loadfile(const string& xmlfile)
{
//
//
//
}
//递归遍历xml节点目录树函数..
void CProcessXmlConfig:recursive_print( const string& nodepath )
{
//
//
//
//
//
}
//主程序文档类中用到了CViewTree来动态加载节点,具体调用如下:
//
//
FillClassView()方法主体如下: