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

📄 xml.cpp

📁 电力故障信息采集,主要是针对南自的保护装置,这个程序用在Linux操作系统下
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "xml.h"//#include <qfile.h> //#include <qtextcodec.h> //#include <qtextstream.h> #include <string.h>#include <vector>using namespace std;inline char* strlwr(char *src){    if (src == NULL)        return NULL;    char *point = src ;    while (*src && *src>='A' && *src<='Z')    {        *src = *src + ('a'-'A');        src++;    }//	printf("strcmp_lower:  strlwr :%s\n", point);    return point ;}inline int strcmp_lower(const char* pszStr1, string& str2){    if (! pszStr1 || str2.empty())    {        printf("str1 or str2 is empty\n");        return -1;    }   string str1 = pszStr1;   strlwr(const_cast<char*>(str1.c_str()));   strlwr(const_cast<char*>(str2.c_str()));   return strcmp(str1.c_str(), str2.c_str());}inline int strcmp_lower( string& str1, string& str2){    char* pszStr1 = strlwr(const_cast<char*>(str1.c_str()));    char* pszStr2 = strlwr(const_cast<char*>(str2.c_str()));    return strcmp(pszStr1, pszStr2);}CXMLReader::CXMLReader(const char* pszFileName): m_NodeCurrent((EXML_HANDLE)EXML_INVALID_HANDLE){    m_strFileName = pszFileName;}CXMLReader::~CXMLReader(){}//==================================================================================================////= Begin: 兼容老版本接口:与老版本程序接口名称及参数保持一致//==================================================================================================////打开XML文件bool CXMLReader::OpenDomXMLFile(){    return m_DocXML.LoadXMLFromFile(m_strFileName.c_str());}//保存XML文件bool CXMLReader::SaveXmlFile(){    return m_DocXML.SaveXMLToFile(m_strFileName.c_str());}bool CXMLReader::GetSomeNode(const QString& qstrHead, const QString& qstrAttriName, const QString& qstrValue){    return GetSomeNode(qstrHead.data(), qstrAttriName.data(), qstrValue.data());}QString CXMLReader::GetCurrentNodeAttribValue(const QString& qstrName){    return GetCurrentNodeAttribValue(qstrName.data());        }bool CXMLReader::GetNodeAttr(QDomNode& node, const QString& qstrAttriName, QString& qstrAttriValue){    return GetAttributeValue(node, qstrAttriName.data(), qstrAttriValue);}bool CXMLReader::GetNodeAttr(QDomNode& node, const QString& qstrAttriName, int& nAttrValue){    return GetAttributeValue(node, qstrAttriName.data(), nAttrValue);}// int GetChildNodeCount(QDomNode& node);QDomNode CXMLReader::InsertDomNode(const QString& qstrParentName, const QString& qstrChildName){    return InsertNode(qstrParentName.data(), qstrChildName.data());}QDomNode CXMLReader::InsertChildNode(QDomNode& node, const QString& qstrChildName){    return InsertChildNode(node, qstrChildName.data());}//在Node节点中添加或修改属性AttrName,值为AttrValuebool CXMLReader::InsertNodeAttr(QDomNode& node, const QString& qstrAttriName, const QString& qstrAttriValue){    return InsertAttribute(node, qstrAttriName.data(), qstrAttriValue.data());}//==================================================================================================////= End: 兼容老版本接口:与老版本程序接口名称及参数保持一致//==================================================================================================////typedef queue<QDomNode> QueueEXMLNode;void CXMLReader::GetAllNode(QDomNode& parent, QueueEXMLNode& qNodes){    if (! parent.IsValidNode())        return;    qNodes.push(parent);    for (int nNode=0; nNode<parent.GetChildCount(); nNode++)    {        QDomNode node = parent.GetChildNode(nNode);        GetAllNode(node, qNodes);    }}// 得到一个Head标题头下的属性=value的接点bool CXMLReader::GetSomeNode(const char* pszHead, const char* pszAttriName, const char* pszAttriValue){    //printf("CXMLReader::GetSomeNode enter, pszHead=%s, attri_name=%s, attri_value=%s\n", pszHead, pszAttriName, pszAttriValue);    QDomNode root = m_DocXML.GetRootNode();    if (! root.IsValidNode())	return false;    /// 查找根节点下名为strHead的子节点    for (int nNode=0; nNode<root.GetChildCount(); nNode++)    {        QDomNode child = root.GetChildNode(nNode);        if (! child.IsValidNode())	    continue;	        string strName = child.GetName();        if (0 != strcmp_lower(pszHead, strName))            continue;        QueueEXMLNode qNodes;        GetAllNode(child, qNodes);	 //printf("queue=%d\n", qNodes.size());        while (qNodes.size() > 0)        {            QDomNode node = qNodes.front();	     qNodes.pop();	     	     //printf("node:%s\n", node.GetName());            for (int nAttri=0; nAttri<node.GetAttributeCount(); nAttri++)            {                IEXMLAttribute attri = node.GetAttribute(nAttri);		  //printf("node: attri_name:%s, value=%s\n",  attri.GetName(), attri.GetValue());                if (! attri.IsValidAttribute())		    continue;		                string strName = attri.GetName();                string strValue = attri.GetValue();                if (   0 == strcmp_lower(pszAttriName, strName)                  && 0 == strcmp_lower(pszAttriValue, strValue) )                {		      //printf("strcmp--------------\n");		      //printf("node: attri_name:%s, value=%s\n",  attri.GetName(), attri.GetValue());                    m_NodeCurrent = node;                    return true;                }            }        }    }    m_NodeCurrent = QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);    return false;}QDomNode CXMLReader::FindChildNode(QDomNode& parent, const char* pszAttriName, const char* pszAttriValue){    if (! parent.IsValidNode())        return QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);        /// 搜寻父节点下所有子节点    for (int nChild=0; nChild<parent.GetChildCount(); nChild++)    {        QDomNode child = parent.GetChildNode(nChild);        if ( child.IsValidNode() )        {            /// 搜索子节点下的所有属性            for (int nAttri=0; nAttri<child.GetAttributeCount(); nAttri++)            {                IEXMLAttribute attri = child.GetAttribute(nAttri);                string strName = attri.GetName();                string strValue = attri.GetValue();                if (   0 == strcmp_lower(pszAttriName, strName)                    && 0 == strcmp_lower(pszAttriValue, strValue) )                {                    /// 匹配                    return child;                }            }        }        /// 继续在该子节点下寻找        QDomNode node = FindChildNode(child, pszAttriName, pszAttriValue);    }        return QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);}const char* CXMLReader::GetCurrentNodeAttribValue(const char* pszAttriName){    if (! m_NodeCurrent.IsValidNode())        return "";    /// 查找当前节点下的属性    for (int nAttri=0; nAttri<m_NodeCurrent.GetAttributeCount(); nAttri++)    {        IEXMLAttribute attri = m_NodeCurrent.GetAttribute(nAttri);                string strName = attri.GetName();                if (0 == strcmp_lower(pszAttriName, strName))            return attri.GetValue();    }        return "";}QDomNode CXMLReader::FindChildNode(const char* pszParentName, const char* pszChildName){    QDomNode root = m_DocXML.GetRootNode();    if (! root.IsValidNode())    {        return QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);    }    string strQuery = /*string(pszParentName) + string("/") + */ pszChildName;pszParentName=pszParentName;    return root.FindChild(strQuery.c_str());}//==================================================================================================////= Begin: 兼容老版本接口:与老版本程序接口名称及参数保持一致//==================================================================================================////取得父节点szDomName下的一个子节点szNodeName,匹配在第一个找到的节点QDomNode CXMLReader::GetDomNode(QString szDomName, QString szNodeName){    QDomNode root = m_DocXML.GetRootNode();    if (! root.IsValidNode())    {        return QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);    }    	QDomNode parent, child;		for (int nFirst=0; nFirst<root.GetChildCount(); nFirst++)	{		QDomNode parent = root.GetChildNode(nFirst);	    if (! parent.IsValidNode())	    {	        return QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);	    }	    	    string strParentName = parent.GetName();	    if (0 != strcmp_lower(szDomName.data(), strParentName))            continue;            		for (int nSecond=0; nSecond<parent.GetChildCount(); nSecond++)		{			QDomNode child = parent.GetChildNode(nSecond);					    if (! child.IsValidNode())		    {		        return QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);		    }		    		    string strChildName = child.GetName();		    if (0 != strcmp_lower(szNodeName.data(), strChildName))	            continue;	            			return child;		}    	}		return QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);}//此函数的"..."部分,带入:属性1,值1,属性2,值2,...,NULL,NULLQDomNode CXMLReader::GetDomNode(char* pszNodeName, ...){    vector<ATTRI_PART> aryAttri;    va_list args;    va_start(args, pszNodeName);    string strNodeName = pszNodeName;    char* pszAttriName    = va_arg(args,char*);    char* pszAttriValue = va_arg(args,char*);        while (pszAttriName &&pszAttriValue)    {        struct ATTRI_PART attri;        attri.strName = pszAttriName;        attri.strValue= pszAttriValue;        aryAttri.push_back(attri);        pszAttriName    = va_arg(args,char*);        pszAttriValue = va_arg(args,char*);    }    va_end(args);            //查找接点    QDomNode root = m_DocXML.GetRootNode();    if (! root.IsValidNode())    {        return QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);    }    for (int nFirst=0; nFirst<root.GetChildCount(); nFirst++)    {        QDomNode child_first = root.GetChildNode(nFirst);        if (! child_first.IsValidNode())        {            return QDomNode((EXML_HANDLE)EXML_INVALID_HANDLE);        }        string strName = child_first.GetName();        if (0 == strcmp_lower(strName, strNodeName))        {	  //add by HYK	   if(0==aryAttri.size())return child_first;            for (int nSecond=0; nSecond<child_first.GetChildCount(); nSecond++)

⌨️ 快捷键说明

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