⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlwrapper.cpp

📁 电力故障信息采集,主要是针对南自的保护装置,这个程序用在Linux操作系统下
💻 CPP
字号:
//// C++ Implementation: xmlwrapper//// Description:////// Author: root <root@localhost.localdomain>, (C) 2007//// Copyright: See COPYING file that comes with this distribution////#include "xmlwrapper.hpp"#include <string.h>#include <errno.h>using namespace std;#ifdef NR_LIBXML2_DEBUG///aux debug functionsostream& operator<<(ostream& o,xmlNodePtr pNode){	o<<"[xmlNodePtr info][type="<<pNode->type<<"]";	o<<"[name="<<string((pNode->name==NULL)?"NULL":((char*)pNode->name))<<"]";	return o;}#endif#ifdef GBK_ENVstatic convertor inConv=convertor("GBK","UTF-8");static convertor outConv=convertor("UTF-8","GBK");#endifnamespace XMLWrapper{  Document Node::getOwnerDocument()  {    return ((xmlNodePtr)pNode)->doc;	}	Node::operator Document()	{		if(((xmlNodePtr)pNode)->type!=DOCUMENT_NODE) throw DOMException("bad_cast from Node to Document");		return Document((xmlDocPtr)pNode);	}	Node XPathProcessor::selectSingleNode(Node& refNode,const char* expr)	{		xmlNodePtr pNode=refNode.impl<xmlNodePtr>();		pContext->node=pNode;		xmlXPathObjectPtr pResult=xmlXPathEval((xmlChar*)expr,pContext);#ifdef XMLWRAPPER_DEBUG      assert(pResult->type==XPATH_NODESET);#endif			if(NULL==pResult)			{				throw DOMException("xpath selection error");			}			Node n((NULL!=pResult->nodesetval&&0<pResult->nodesetval->nodeNr)?pResult->nodesetval->nodeTab[0]:NULL);			xmlXPathFreeObject(pResult);			return n;	}	NodeSet XPathProcessor::selectNodes(Node& refNode,const char* expr)	{		xmlNodePtr pNode=refNode.impl<xmlNodePtr>();		pContext->node=pNode;#ifdef GBK_ENV		xmlXPathObjectPtr pResult=xmlXPathEval((xmlChar*)inConv.convert(expr).c_str(),pContext);#else		xmlXPathObjectPtr pResult=xmlXPathEval((xmlChar*)expr,pContext);#endif#ifdef XMLWRAPPER_DEBUG      assert(pResult->type==XPATH_NODESET);#endif			if(NULL==pResult)			{				throw DOMException("xpath selection error");			}			NodeSet ns;			for(size_t i=0;i<pResult->nodesetval->nodeNr;i++)			{				ns.push_back(Node(pResult->nodesetval->nodeTab[i]));			}			xmlXPathFreeObject(pResult);			return ns;	}	string Node::value()const	{        /// @todo implement me		xmlChar* pValue=xmlNodeGetContent((xmlNodePtr)pNode);#ifdef GBK_ENV			string str=outConv.convert((char*)pValue);#else			string str((const char*)pValue);#endif			xmlFree(pValue);			return str;	}}/*!    \fn XMLWrapper::Node::operator Element */ XMLWrapper::Node::operator XMLWrapper::Element(){    /// @todo implement me#ifdef NR_LIBXML2_DEBUG	cout<<"[DEBUG][Node::operator Element()]pNode="<<(void*)pNode<<endl;#endif	if(((xmlNodePtr)pNode)->type!=ELEMENT_NODE)throw DOMException("invalid cast, Node to Element");	return Element((xmlElementPtr)pNode);}/*!    \fn XMLWrapper::Document::getRootElement */XMLWrapper::Element XMLWrapper::Document::getRootElement(){    /// @todo implement me#ifdef NR_LIBXML2_DEBUG	cout<<"[DEBUG][Document::getRootElement()]"<<(xmlNodePtr)pNode<<endl;	cout<<"[DEBUG][Document::getRootElement()]root element info "<<xmlDocGetRootElement((xmlDocPtr)this->pNode)<<endl;#endif	return (Element)Node(xmlDocGetRootElement((xmlDocPtr)this->pNode));}/*!    \fn XMLWrapper::Node::name()const */string XMLWrapper::Node::name()const{    /// @todo implement me#ifdef NR_LIBXML2_DEBUG	cout<<"[DEBUG][Node::name()]"<<(void*)pNode<<endl;	cout<<"[DEBUG][Node::name()]"<<(xmlNodePtr)pNode<<endl;#endif#ifdef GBK_ENV	cout<<"define GBK_ENV"<<endl;	return outConv.convert((const char*)((xmlNodePtr)pNode)->name);#else	return string((char*)((xmlNodePtr)pNode)->name);#endif}/*!    \fn XMLWrapper::Element::Element(const Element& src) */ XMLWrapper::Element::Element(const Element& src){    /// @todo implement me	pNode=src.pNode;}/*!    \fn XMLWrapper::Node::Node(const Node& src) */ XMLWrapper::Node::Node(const Node& src){    /// @todo implement me	pNode=src.pNode;}/*!    \fn XMLWrapper::Document::outputEncoding()const */string XMLWrapper::Document::encoding()const{    /// @todo implement me	return string((char*)((xmlDocPtr)pNode)->encoding);}/*!    \fn XMLWrapper::Node::praent()const */XMLWrapper::Node XMLWrapper::Node::parent()const{    /// @todo implement me	return Node(((xmlNodePtr)pNode)->parent);}/*!    \fn XMLWrapper::Node::previousSibling()const */XMLWrapper::Node XMLWrapper::Node::previousSibling()const{    /// @todo implement me	return Node(((xmlNodePtr)pNode)->prev);}/*!    \fn XMLWrapper::Element::getAttributeValue(const string& attrName) */string XMLWrapper::Element::getAttributeValue(const string& attrName){    /// @todo implement me#ifdef GBK_ENV	xmlChar* pValue=xmlGetProp((xmlNodePtr)pNode,(const xmlChar*)inConv.convert(attrName.c_str()).c_str());#else	xmlChar* pValue=xmlGetProp((xmlNodePtr)pNode,(const xmlChar*)attrName.c_str());#endif	if(NULL==pValue)	{		char msgBuff[300];		bzero(msgBuff,300);		snprintf(msgBuff,300,"get attribute named %s's value error,node path=%s",attrName.c_str(),xmlGetNodePath((xmlNodePtr)pNode));		throw DOMException(msgBuff);	}	string str((const char*)pValue);#ifdef GBK_ENV	str=outConv.convert(str.c_str());#endif	xmlFree(pValue);	return str;}/*!    \fn XMLWrapper::Element::setAttributeValue(const string& name,const string& value) */void XMLWrapper::Element::setAttributeValue(const string& name,const string& value){    /// @todo implement me#ifdef GBK_ENV	xmlSetProp((xmlNodePtr)pNode,(xmlChar*)inConv.convert(name.c_str()).c_str(),(xmlChar*)inConv.convert(value.c_str()).c_str());#else	xmlSetProp((xmlNodePtr)pNode,(xmlChar*)name.c_str(),(xmlChar*)value.c_str());#endif}/*!    \fn XMLWrapper::Element::addChild(const string& name) */XMLWrapper::Element XMLWrapper::Element::addChildElement(const string& name){    /// @todo implement me#ifdef GBK_ENV		xmlNodePtr newChild=xmlNewNode(NULL,(const xmlChar*)inConv.convert(name.c_str()).c_str());#else		xmlNodePtr newChild=xmlNewNode(NULL,(const xmlChar*)name.c_str());#endif		return (Element)Node(xmlAddChild((xmlNodePtr)pNode,newChild));}/*!    \fn XMLWrapper::Node::childrenCount() */size_t XMLWrapper::Node::childrenCount(){    /// @todo implement me	if(NULL==((xmlNodePtr)pNode)->children)return 0;	return ((xmlNodePtr)pNode)->children-((xmlNodePtr)pNode)->last+1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -