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

📄 parsexml.cpp

📁 一个简单的XML文件生成和解释程序,很实用.
💻 CPP
字号:
#include "stdafx.h"

#include<stdio.h>
#include "../include/parsexml.h"
//CXMLValue
CXMLValue::CXMLValue(){
	//m_strName=new CYGString();
	//m_strValue=new CYGString();
}
CXMLValue::CXMLValue(char *strName,char *strValue){
	m_strName=strName;
    m_strValue=strValue;
	//m_strName=new CYGString(strName);
//	m_strValue=new CYGString(strValue);
}
CXMLValue::~CXMLValue(){
	//delete m_strName;
	//delete m_strValue;
}
inline CYGString *CXMLValue::GetName(){
	 return &m_strName;
}
inline CYGString *CXMLValue::GetValue(){
	return &m_strValue;
}
inline void CXMLValue::SetName(char *strName){
	 m_strName.CopyBuffer(strName);
};
inline void CXMLValue::SetValue(char *strValue){
	 m_strValue.CopyBuffer(strValue);
};
//CXMLNode
CXMLNode::CXMLNode(CXMLNode *pParent){
	m_pParent=pParent;
	m_Property=new CNI_Stack();
	m_ChildNode=new CNI_Stack();
	m_Value=new CXMLValue();
}
CXMLNode::~CXMLNode(){
	CXMLNode *pNode;
	CXMLValue *pProperty;
	while(pNode=(CXMLNode *)m_ChildNode->Pop()){
		 if(!pNode)break;
		 delete pNode;
	}
	while(pProperty=(CXMLValue *)m_Property->Pop()){
		 if(!pProperty)break;
		 delete pProperty;
	}
	delete m_Property;
    delete m_ChildNode;
	delete m_Value;
	m_Property=NULL;
	m_ChildNode=NULL;
	m_Value=NULL;
}
CXMLNode *CXMLNode::GetParent(){
	return m_pParent;
}
void      CXMLNode::SetParent(CXMLNode *pParent){
	m_pParent=pParent;
}
CYGString*CXMLNode::GetName(){
      return m_Value->GetName();
}
void      CXMLNode::SetName(char *strName){
	  m_Value->GetName()->CopyBuffer(strName);
}
CYGString*CXMLNode::GetContext(){
	 return this->m_Value->GetValue();
}
void      CXMLNode::SetContext(char *strValue){
	this->m_Value->GetValue()->CopyBuffer(strValue);
}

CXMLNode *CXMLNode::CreateChildNode(char *strName){
	CXMLNode *pNode;
	pNode=new CXMLNode(this);
	pNode->SetName(strName);
	m_ChildNode->Push(pNode);
    return pNode;
}
CXMLNode *CXMLNode::GetChildNode(char *strName){
   	CXMLNode *pNode=NULL;
	int i;
	for(i=0;i<m_ChildNode->GetCount();i++){		   
		   pNode=(CXMLNode *)m_ChildNode->GetData(i);
		   if(pNode)
			      if(pNode->GetName()->Equals(strName))break;
           pNode=NULL;  
	}
    return pNode;
}
CXMLNode *CXMLNode::GetChildNode(int index){
   	CXMLNode *pNode=NULL;
	pNode=(CXMLNode *)m_ChildNode->GetData(index);
    return pNode;
}
int       CXMLNode::GetChildCount(){
	return this->m_ChildNode->GetCount();
}
bool      CXMLNode::RemoveChildNode(char *strName){
	bool bRes=false;
   	CXMLNode *pNode=NULL;
	int i;
	for(i=0;i<m_ChildNode->GetCount();i++){		   
		   pNode=(CXMLNode *)m_ChildNode->GetData(i);
		   if(pNode)
			   if(pNode->GetName()->Equals(strName)){
				                  bRes=m_ChildNode->DeleteByIndex(i);
								  delete pNode;
								  break;
			   }
           pNode=NULL;  
	}
	return bRes;
}
bool     CXMLNode::RemoveChildNode(int index){
   bool bRes;
  CXMLNode *pNode=NULL;
  if(m_ChildNode->GetCount()<=0)return false;
  pNode=(CXMLNode *)m_ChildNode->GetData(index);
  bRes=m_ChildNode->DeleteByIndex(index);
  if(pNode&&bRes)delete pNode;
  return bRes;
//   return m_ChildNode->DeleteByIndex(index);
}

CXMLValue*CXMLNode::CreateProperty(char *strName,char *strValue){
	CXMLValue *pPro;
	pPro=new CXMLValue();
	pPro->SetName(strName);
	pPro->SetValue(strValue);
	m_Property->Push(pPro);
    return pPro;
}
int       CXMLNode::GetPropertyCount(){
	return m_Property->GetCount();
}
CXMLValue*CXMLNode::GetProperty(char *strName){
   	CXMLValue *pNode=NULL;
	int i;
	for(i=0;i<m_Property->GetCount();i++){		   
		   pNode=(CXMLValue *)m_Property->GetData(i);
		   if(pNode)
			      if(pNode->GetName()->Equals(strName))break;
           pNode=NULL;  
	}
    return pNode;
}
CXMLValue*CXMLNode::GetProperty(int index){
   	CXMLValue *pNode=NULL;
	pNode=(CXMLValue *)m_Property->GetData(index);
    return pNode;
}
bool      CXMLNode::RemoveProperty(char *strName){
	bool bRes=false;
   	CXMLValue *pNode=NULL;
	int i;
	for(i=0;i<m_Property->GetCount();i++){		   
		   pNode=(CXMLValue *)m_Property->GetData(i);
		   if(pNode)
			   if(pNode->GetName()->Equals(strName)){
				                  bRes=m_Property->DeleteByIndex(i);
								  delete pNode;
								  break;
			   }
           pNode=NULL;  
	}
	return bRes;	
}
bool      CXMLNode::RemoveProperty(int index){
  bool bRes;
  CXMLValue *pNode=NULL;
  pNode=(CXMLValue *)m_Property->GetData(index);
  bRes=m_Property->DeleteByIndex(index);
  if(pNode&&bRes)delete pNode;
  return bRes;
//   return m_Property->DeleteByIndex(index);
}
//CParseXML
CParseXML::CParseXML()
{
m_Node=new CNI_Stack();
pCurNode=NULL;
//m_Context=new CYGString();
m_Property=new CNI_Stack();
}

CParseXML::~CParseXML()
{
    EmptyXML();
	delete m_Property;
//	delete m_Context;
}
bool  CParseXML::EmptyXML(){
	CXMLValue *pProperty;
	CXMLNode *pNode;
	while(pNode=(CXMLNode *)m_Node->Pop()){
		 if(!pNode)break;
		 delete pNode;
	}

	while(pProperty=(CXMLValue *)m_Property->Pop()){
		 if(!pProperty)break;
		 delete pProperty;
	}
	pCurNode=NULL;
	return true;
}
CXMLNode *CParseXML::CreateChildNode(char *strName){
	CXMLNode *pNode;
	pNode=new CXMLNode(NULL);
	pNode->SetName(strName);
	m_Node->Push(pNode);
    return pNode;
}
int       CParseXML::GetChildCount(){
	return this->m_Node->GetCount();
}
CXMLNode *CParseXML::GetChildNode(char *strName){
   	CXMLNode *pNode=NULL;
	int i;
	for(i=0;i<m_Node->GetCount();i++){		   
		   pNode=(CXMLNode *)m_Node->GetData(i);
		   if(pNode)
			      if(pNode->GetName()->Equals(strName))break;
           pNode=NULL;  
	}
    return pNode;
}
CXMLNode *CParseXML::GetChildNode(int index){
   	CXMLNode *pNode=NULL;
	pNode=(CXMLNode *)m_Node->GetData(index);
    return pNode;
}
bool      CParseXML::RemoveChildNode(char *strName){
	bool bRes=false;
   	CXMLNode *pNode=NULL;
	int i;
	for(i=0;i<m_Node->GetCount();i++){		   
		   pNode=(CXMLNode *)m_Node->GetData(i);
		   if(pNode)
			   if(pNode->GetName()->Equals(strName)){
				                  bRes=m_Node->DeleteByIndex(i);
								  delete pNode;
								  break;
			   }			   
           pNode=NULL;  
	}
	return bRes;
}
bool      CParseXML::RemoveChildNode(int index){
   bool bRes;
  CXMLNode *pNode=NULL;
  pNode=(CXMLNode *)m_Node->GetData(index);
  bRes=m_Node->DeleteByIndex(index);
  if(pNode&&bRes)delete pNode;
  return bRes;
 //  	CXMLNode *pNode=NULL;
 // return m_Node->DeleteByIndex(index);
}
void CParseXML::OnBeginTag(char *strName){
   	CXMLNode *pNode=pCurNode;
	if(!pCurNode)
		  pCurNode=this->CreateChildNode(strName);
        else
          pCurNode=pCurNode->CreateChildNode(strName);
}
void CParseXML::OnProperty(char *strPro,char *strValue){
	if(pCurNode)
		   pCurNode->CreateProperty(strPro,strValue);
}
void CParseXML::OnContext(char *strValue){
	if(pCurNode)
		  pCurNode->GetContext()->AddBuffer(strValue);
	  else
		  m_Context.AddBuffer(strValue);
}
void CParseXML::OnEndTag(char *strName){
    if(pCurNode)pCurNode=pCurNode->GetParent();
}
void CParseXML::OnXmlProperty(char *strPro,char *strValue){
   this->CreateProperty(strPro,strValue);
}
CXMLValue*CParseXML::CreateProperty(char *strName,char *strValue){
	CXMLValue *pPro;
	pPro=new CXMLValue();
	pPro->SetName(strName);
	pPro->SetValue(strValue);
	m_Property->Push(pPro);
    return pPro;
}
int       CParseXML::GetPropertyCount(){
	return m_Property->GetCount();
}
CXMLValue*CParseXML::GetProperty(char *strName){
   	CXMLValue *pNode=NULL;
	int i;
	for(i=0;i<m_Property->GetCount();i++){		   
		   pNode=(CXMLValue *)m_Property->GetData(i);
		   if(pNode)
			      if(pNode->GetName()->Equals(strName))break;
           pNode=NULL;  
	}
    return pNode;
}
CXMLValue*CParseXML::GetProperty(int index){
   	CXMLValue *pNode=NULL;
	pNode=(CXMLValue *)m_Property->GetData(index);
    return pNode;
}
bool      CParseXML::RemoveProperty(char *strName){
	bool bRes=false;
   	CXMLValue *pNode=NULL;
	int i;
	for(i=0;i<m_Property->GetCount();i++){		   
		   pNode=(CXMLValue *)m_Property->GetData(i);
		   if(pNode)
			   if(pNode->GetName()->Equals(strName)){
				                  bRes=m_Property->DeleteByIndex(i);
								  delete pNode; 
								  break;             
			   }
           pNode=NULL;  
	}
	return bRes;	
}
bool      CParseXML::RemoveProperty(int index){
  bool bRes;
  CXMLValue *pNode=NULL;
  pNode=(CXMLValue *)m_Property->GetData(index);
  bRes=m_Property->DeleteByIndex(index);
  if(pNode&&bRes)delete pNode;
  return bRes;
}
bool CParseXML::SaveFile(char *strFileName){
 CYGString str;
 bool bRes=false;
 FILE *fp;
 GetXMLContext(str);
 if(str.Length()>0){
	    fp=fopen(strFileName,"w");
		if(fp){
			     fprintf(fp,"%s",str.ToPtr());
				 fclose(fp);
				 bRes=true;
		}
 }
 return bRes;
}
void CParseXML::_XMLContext(CXMLNode *pNode,CYGString &str){
 CXMLValue *pValue;
 CXMLNode *pTmpNode;
 int i,iCount;
 str=str+"<"+pNode->GetName()->ToPtr();
 for(i=0;i<pNode->GetPropertyCount();i++){
	      pValue=(CXMLValue *)pNode->GetProperty(i);
		  str=str+" "+pValue->GetName()->ToPtr()+"=\"";
		  str=str+pValue->GetValue()->ToPtr()+"\" ";
 }
if(pNode->GetChildCount()<=0)
       str=str+">";
   else
//       str=str+">";
       str=str+">\r\n";
if(pNode->GetContext()->Length()>0)str=str+pNode->GetContext()->ToPtr();
iCount=pNode->GetChildCount();
for(i=0;i<iCount;i++){
	      pTmpNode=(CXMLNode *)pNode->GetChildNode(i);
		  _XMLContext(pTmpNode,str);
}
str=str+"</"+pNode->GetName()->ToPtr()+">\r\n";
//str=str+"</"+pNode->GetName()->ToPtr()+">";
}
bool CParseXML::GetXMLContext(CYGString &str){
 bool bRes=false;
 int i;
 str="<?xml ";
 CXMLValue *pValue;
 CXMLNode *pNode;
 if(m_Property->GetCount()<=0){
	   this->CreateProperty("encoding","GB2312");
	   this->CreateProperty("charset","gb2312");
	   this->CreateProperty("version","1.0");
 }
 for(i=0;i<m_Property->GetCount();i++){
	      pValue=(CXMLValue *)m_Property->GetData(i);
		  str=str+" "+pValue->GetName()->ToPtr()+"=\"";
		  str=str+pValue->GetValue()->ToPtr()+"\"";
 }
 str+=" ?>\r\n";
// str+=" ?>";
 for(i=0;i<m_Node->GetCount();i++){
	  pNode=(CXMLNode *)m_Node->GetData(i);
	  if(pNode)
         _XMLContext(pNode,str);
 }
 return bRes;
}
bool CParseXML::LoadText(char *strContext){
	this->EmptyXML();
	return _LoadText(strContext);
}
bool CParseXML::LoadFile(char *strFileName){
	this->EmptyXML();
	return _LoadFile(strFileName);
}

⌨️ 快捷键说明

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