c++ xml_XML ++

c++ xml

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++.

本文的目的是为您提供一个易于使用的C ++ XML包装器,并提供一些可能与MS C ++一起使用的有趣技术。

The reason I built this class is to ease the pain of using XML files with C++, since there is no native XML support, but only through COM it is not always trivial to untrained programmer.

之所以创建该类,是为了减轻使用C ++使用XML文件的痛苦,因为它不提供本机XML支持,但是仅通过COM,对于未经培训的程序员来说并不总是那么琐碎。

First the header file for the class:

首先是该类的头文件:

File: xml++.h

文件:xml ++。h

#pragma once

#import "MSXML3.dll" named_guids 
#include <string>

class xmldocument;
class xmlnode;
class xmlnodelist;

class xmldocument
{
public:
	/// Constructor
	xmldocument()
	{
		CoInitialize(NULL);
		m_xmldoc.CreateInstance(MSXML2::CLSID_DOMDocument30);
		MSXML2::IXMLDOMDocument2Ptr doc = m_xmldoc;
		doc->setProperty("SelectionLanguage","XPath");
	}

	/// Loading
	bool load(std::string url)
	{
		if (m_xmldoc!=NULL) 
		{
			_variant_t v(url.c_str());
			m_xmldoc->put_async(VARIANT_FALSE);
			VARIANT_BOOL success = m_xmldoc->load(v);
			return success == VARIANT_TRUE;
		}
		return false;
	}
	bool load(std::wstring url)
	{
		if (m_xmldoc!=NULL) 
		{
			_variant_t v(url.c_str());
			m_xmldoc->put_async(VARIANT_FALSE);
			VARIANT_BOOL success = m_xmldoc->load(v);
			return success == VARIANT_TRUE;
		}
		return false;
	}

	bool save(std::string url)
	{
		_variant_t v(url.c_str());
		return m_xmldoc->save(v) == S_OK;
	}
	bool save(std::wstring url)
	{
		_variant_t v(url.c_str());
		return m_xmldoc->save(v) == S_OK;
	}

	// Access operators
	MSXML2::IXMLDOMNodePtr operator[](int index)
	{
		if (m_xmldoc!=NULL) return  m_xmldoc->childNodes->item[index];
		else return NULL;
	}
	MSXML2::IXMLDOMNodePtr operator[](std::string name)
	{
		if (m_xmldoc!=NULL) return m_xmldoc->selectSingleNode(name.c_str());
		else return NULL;
	}
	MSXML2::IXMLDOMNodePtr operator[](std::wstring name)
	{
		if (m_xmldoc!=NULL) return m_xmldoc->selectSingleNode(name.c_str());
		else return NULL;
	}

	MSXML2::IXMLDOMNodeListPtr operator()(std::string xpath)
	{
		if (m_xmldoc!=NULL) return m_xmldoc->selectNodes(xpath.c_str());
		else return NULL;
	}
	MSXML2::IXMLDOMNodeListPtr operator()(std::wstring xpath)
	{
		if (m_xmldoc!=NULL) return m_xmldoc->selectNodes(xpath.c_str());
		else return NULL;
	}

	MSXML2::IXMLDOMNodeListPtr children()
	{
		if (m_xmldoc!=NULL) return m_xmldoc->childNodes;
		else return NULL;
	}

	/// Cast operators / extractors
	operator MSXML2::IXMLDOMNodePtr()
	{
		if (m_xmldoc!=NULL) return m_xmldoc->documentElement;
		else return NULL;
	}

private:
	MSXML2::IXMLDOMDocumentPtr m_xmldoc;
};

class xmlnode
{
public:
	struct xmlvalue
	{
		_bstr_t value;
		xmlvalue() {}
		xmlvalue(_bstr_t val) { value = val; }
		
		operator std::string()
		{
			return (const char*)value;
		}
		operator std::wstring()
		{
			return (const wchar_t*)value;
		}

		operator bool()
		{
			
			if (_stricmp(value, "yes") == 0 || _stricmp(value, "true") == 0 || _stricmp(value, "1") == 0)
				return true;
			else return false;			
			return false;
		}
		operator short()
		{
			return atoi(value);
		}
		operator int()
		{
			return atoi(value);
		}
		operator long()
		{
			return atol(value);
		}
		operator unsigned short()
		{
			return atoi(value);
		}
		operator unsigned long()
		{
			return atol(value);
		}
		operator float()
		{
			return (float)atof(value);
		}
		operator double()
		{
			return atof(value);
		}
	};
public:
	xmlnode(MSXML2::IXMLDOMNodePtr source)
	{
		m_xmlnode = source;
	}	
	void operator=(MSXML2::IXMLDOMNodePtr source)
	{
			m_xmlnode = source;
	}

	xmlnode operator[](int index)
	{
		if (m_xmlnode!=NULL) return  m_xmlnode->childNodes->item[index];
		else return NULL;
	}
	xmlnode operator[](std::string name)
	{
		if (m_xmlnode!=NULL) return m_xmlnode->selectSingleNode(name.c_str());
		else return NULL;
	}
	xmlnode operator[](std::wstring name)
	{
		if (m_xmlnode!=NULL) return m_xmlnode->selectSingleNode(name.c_str());
		else return NULL;
	}

	MSXML2::IXMLDOMNodeListPtr operator()(std::string xpath)
	{
		if (m_xmlnode!=NULL) return m_xmlnode->selectNodes(xpath.c_str());
		else return NULL;
	}
	MSXML2::IXMLDOMNodeListPtr operator()(std::wstring xpath)
	{
		if (m_xmlnode!=NULL) return m_xmlnode->selectNodes(xpath.c_str());
		else return NULL;
	}


	__declspec(property(get=Getvalue,put=Putvalue))
		xmlvalue value;
	xmlvalue Getvalue()
	{
		if (m_xmlnode!=NULL) return xmlvalue(m_xmlnode->text);
		return xmlvalue();
	}
	void Putvalue(xmlvalue newVal)
	{
		if (m_xmlnode!=NULL) m_xmlnode->text = newVal.value;
	}


	
private:
	MSXML2::IXMLDOMNodePtr m_xmlnode;
};

class xmlnodelist
{
public:
	xmlnodelist(MSXML2::IXMLDOMNodeListPtr source)
	{
		m_xmlnodelist = source;
	}
	void operator=(MSXML2::IXMLDOMNodeListPtr source)
	{
		m_xmlnodelist = source;
	}

	int length()
	{
		if (m_xmlnodelist!=NULL) return m_xmlnodelist->length;
		else return 0;
	}

	xmlnode operator[] (int index)
	{
		if (m_xmlnodelist!=NULL) return m_xmlnodelist->item[index];
		else return NULL;
	}

private:
	MSXML2::IXMLDOMNodeListPtr m_xmlnodelist;
};

1.用法示例 (1. Example usage)

In order to use the classes you need only to include the above heade.

为了使用这些类,您只需要包括上述标题。

Sample usage might look like this:

用法示例可能如下所示:

#include "stdafx.h"

#include "xml.h"

int _tmain(int argc, _TCHAR* argv[])
{
	xmldocument doc;
	doc.load("c:\\inventory.xml");
	
	// Cast to node
	xmlnode root(doc);

	// Retrievers
	xmlnode magazine = doc["magazine"];
	magazine = root["magazine"];

	xmlnodelist books = doc("book");
	books = root("book");

	// Iteraring and getting value
	for (int i = 0; i < books.length(); i++)
	{
		std::string firstname = books[i]["author"]["first-name"].value;
		std::wstring lastname = books[i]["author"]["last-name"].value;
		int price = books[i]["price"].value;
	}

	doc.save("c:\\inventory.xml");
	return 0;
}

2.班级成员 (2. Class members)

The header contains tree classes:

标头包含树类:

class xmldocument;

xmldocument类;

class xmlnode;

类xmlnode;

class xmlnodelist;

xmlnodelist类;

xmldocument - represents the document

xmldocument-代表文件

xmlnode - represents single node

xmlnode-表示单个节点

xmlnodelist - represents a list of nodes

xmlnodelist-代表节点列表

xmldocument members:

xmldocument成员:

load - loads a document from file or XML string

加载-从文件或XML字符串加载文档

save - saves a document to a file

保存-将文档保存到文件

operator[] - extracts single child node

operator []-提取单个子节点

operator() - performs XPATH query and returns list of nodes

operator()-执行XPATH查询并返回节点列表

xmlnode members:

xmlnode成员:

operator[] - extracts single child node

operator []-提取单个子节点

operator() - performs XPATH query and returns list of nodes

operator()-执行XPATH查询并返回节点列表

value - this is the value property that has __declspec(property) declared for it, this is Microsoft specific technique that allows to deglare getter and setter for a property. This property returns the inner text of the node in form of xmlvalue struct that has automatic cast operators to any native type.

value-这是为其声明了__declspec(property)的value属性,这是Microsoft特定的技术,允许对属性进行getter和setter眩光处理。 此属性以xmlvalue结构的形式返回节点的内部文本,该结构具有对任何本机类型的自动强制转换运算符。

xmlnodelist members:

xmlnodelist成员:

length() - returns the length of the list (number of nodes)

length()-返回列表的长度(节点数)

operator[] - get the n'th node from the list

operator []-从列表中获取第n个节点

3.结论 (3. Conclusion)

So there were two interesting points in this implementation:

因此,此实现中有两个有趣的观点:

The usage of __declspec(property)  that allowed me to declare a "property" with get and set accessors and the different operators overloading technique. You are welcome to experiment on the code and extend it further to meet your needs, I hope it gives a good starting point.

__declspec(property)的使用使我可以使用get和set访问器以及不同的运算符重载技术来声明“属性”。 欢迎您尝试该代码,并进一步扩展以满足您的需求,我希望它为您提供一个良好的起点。

翻译自: https://www.experts-exchange.com/articles/341/XML.html

c++ xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值