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

📄 xmlfile.cpp

📁 VC+XML+UG OPEN API做的自动装配
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			}
			pNode=NULL;
			nodelst= NULL;
			foundNode = NULL;
			rootElem = NULL;
		}
		delete [] pCStrKeys;
	}
	return 0;
/*
std::map<std::string, std::string> mp;
	std::map<std::string, std::string>::const_iterator iter;
	std::string strmsg;
	xmlfile.GetKeysValue("xmlRoot/西安/空军工程大学/学生/入学新生", mp);
	for(iter=mp.begin(); iter!=mp.end(); ++iter)
	{
		strmsg+=" ("+iter->first+", "+ iter->second+") \n";
	}
	AfxMessageBox(strmsg.c_str());
*/
	
}

// get a basekey's all children's value
long CXMLFile::GetKeys(const char* cstrBaseKeyName, std::vector<std::string>& keys)
{
	int iNumKeys = 0;
	std::string* pCStrKeys = NULL;
	std::string strValue;

	pCStrKeys = ParseKeys(cstrBaseKeyName, iNumKeys);

	if (pCStrKeys)
	{
		if (XmlDocPtr == NULL)  // load the xml document
			return -1;
		MSXML2::IXMLDOMElementPtr rootElem = NULL;
		MSXML2::IXMLDOMNodePtr foundNode = NULL;
		MSXML2::IXMLDOMNodePtr pNode= NULL;
		XmlDocPtr->get_documentElement(&rootElem);  // root node

		if (rootElem)
		{
			foundNode = FindNode(rootElem, pCStrKeys, iNumKeys);
			pNode=foundNode->GetfirstChild();
			while(pNode!=NULL)
			{
				//pNode =pNode-> nodelst->item[i];
				keys.push_back ((const char*)pNode->nodeName);//(const char*)pNode->xml;	
				//ATLTRACE((const char*)pNode->text);ATLTRACE("\n==============");
				pNode=pNode->GetnextSibling();
			}
		}
		delete [] pCStrKeys;
	}
	return 0;
}

long CXMLFile::GetRootElem(MSXML2::IXMLDOMElementPtr rootElem)
{
	return XmlDocPtr->get_documentElement(&rootElem);
}

long CXMLFile::GetNode(const char* cstrKeyName,
					   MSXML2::IXMLDOMNodePtr& foundNode)
{
	int iNumKeys = 0;
	std::string* pCStrKeys = NULL;

	std::string strBaseKeyName( "//");
	strBaseKeyName +=cstrKeyName;

	MSXML2::IXMLDOMElementPtr rootElem = NULL;
	
	foundNode=XmlDocPtr->selectSingleNode( _com_util::ConvertStringToBSTR(strBaseKeyName.c_str()) );
	if (foundNode)
	{
		return 0;
	}
	else
		return -1;

}

// Parse all keys from the base key name.
std::string* CXMLFile::ParseKeys(const char* cstrFullKeyPath, int &iNumKeys)
{
	std::string cstrTemp;
	std::string* pCStrKeys = NULL;
	std::string strFullKeyPath(cstrFullKeyPath);
	// replace spaces with _ since xml doesn't like them
	std::replace(strFullKeyPath.begin(), strFullKeyPath.end(), ' ', '_');
	
	if (*(strFullKeyPath.end() - 1) == '/' )
		strFullKeyPath.erase(strFullKeyPath.end() -1 );// remove slashes on the end

	iNumKeys=std::count(strFullKeyPath.begin(), strFullKeyPath.end(), '/') +1;

	pCStrKeys = new std::string[iNumKeys];  // create storage for the keys

	if (pCStrKeys)
	{
		int iFind = 0, iLastFind = 0, iCount = -1;
		
		// get all of the keys in the chain
		while (iFind != -1)
		{
			iFind = strFullKeyPath.find('/', iLastFind);
			if (iFind > -1)
			{
				iCount++;
				pCStrKeys[iCount].assign(strFullKeyPath, iLastFind, iFind - iLastFind);
				iLastFind = iFind + 1;
			}
			else
			{
				// make sure we don't just discard the last key in the chain
				if (iLastFind < strFullKeyPath.length()) 
				{
					iCount++;
					pCStrKeys[iCount].assign(strFullKeyPath, iLastFind, strFullKeyPath.length() - iLastFind);
				}
			}
		}
	}

	return pCStrKeys;
}

//if the specific file exist, then return true, else return false
static bool FileExist(const char* pszFileName)
{
	bool bExist = false;
	HANDLE hFile;
	
	if (NULL != pszFileName)
	{
		// Use the preferred Win32 API call and not the older OpenFile.
		hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
			NULL, OPEN_EXISTING, 0, 0);
		
		if (hFile != INVALID_HANDLE_VALUE)
		{
			// If the handle is valid then the file exists.
			CloseHandle(hFile);
			bExist = true;
		}
	}
	
	return (bExist);
}

// load the XML file into the parser
bool CXMLFile::load(const char* filename, const char* root_name)
{
	if(XmlDocPtr != NULL)
		return false;
	
	VARIANT_BOOL vbSuccessful;
	xml_file_name=filename;
	// initialize the Xml parser
	HRESULT hr = XmlDocPtr.CreateInstance(__uuidof(MSXML2::DOMDocument30));
		//__uuidof(MSXML2::DOMDocument40));
	//(MSXML2::CLSID_DOMDocument);
		
	if (XmlDocPtr == NULL) return false;

	m_root_name=root_name;
	// see if the file exists
	if ( !FileExist(filename ) )  // if not
	{
		// create it
		std::string strtmp("<?xml version=\"1.0\" ?><");//encoding=\"UTF-16\"
		if(root_name!=NULL)
		{
			strtmp+=root_name;
			strtmp+="></";
			strtmp+=root_name;
			strtmp+=">";
		}
		else
			strtmp+="xmlRoot></xmlRoot>";

		vbSuccessful=XmlDocPtr->loadXML(_bstr_t(strtmp.c_str()));
	}
	else  // if so
	{
		// load it
		vbSuccessful=XmlDocPtr->load(CComVariant::CComVariant((const char*)filename ));
	}

	if (vbSuccessful == VARIANT_TRUE)
	{
		return true;  // loaded		
	}
	else
	{
		// an XML load error occurred so display the reason
		MSXML2::IXMLDOMParseErrorPtr pIParseError = NULL;
		XmlDocPtr->get_parseError(&pIParseError);

		if (pIParseError)
		{
			long value;
			BSTR bstr = NULL;

			HRESULT hr = pIParseError->get_errorCode(&value);
			pIParseError->get_reason(&bstr);
			//std::string cstrMessage=(char *)_bstr_t(bstr, true);

			MessageBox( NULL, (char *)_bstr_t(bstr, true), "错误提示",
				MB_OK|MB_ICONERROR);

			if (bstr) { SysFreeString(bstr); bstr = NULL; }	
			pIParseError = NULL;		
		}
		return false;
	}	
}

// save the XML file
bool CXMLFile::save(const char* filename)
{ 
	if(XmlDocPtr==NULL)
		return false;
	HRESULT hr;
	if(filename==NULL||filename=="")
		hr = XmlDocPtr->save(CComVariant::CComVariant(xml_file_name.c_str()));
	else
		hr = XmlDocPtr->save(CComVariant::CComVariant(filename));
	xml_file_name=filename;
	//XmlDocPtr=NULL;
	return SUCCEEDED(hr);
}

// discard any changes
void CXMLFile::DiscardChanges()
{
	XmlDocPtr=NULL;	
}

// find a node given a chain of key names
MSXML2::IXMLDOMNodePtr CXMLFile::FindNode(MSXML2::IXMLDOMNodePtr parentNode, 
											  std::string* pCStrKeys, int iNumKeys, 
												  bool bAddNodes /*= false*/)
{
	MSXML2::IXMLDOMNodePtr foundNode = NULL;
	MSXML2::IXMLDOMElementPtr tempElem = NULL;

	for (int i=0; i<iNumKeys; i++)
	{
		// find the node named X directly under the parent
		foundNode = parentNode->selectSingleNode(_bstr_t(pCStrKeys[i].c_str()));
		
		if (foundNode == NULL) 
		{
			// if its not found...
			if (bAddNodes)  // create the node and append to parent (Set only)
			{
				tempElem=XmlDocPtr->createElement(_bstr_t(pCStrKeys[i].c_str()));
				if (tempElem) 
				{
					foundNode=parentNode->appendChild(tempElem);
					// since we are traversing the nodes, we need to set the parentNode to our foundNode
					parentNode = NULL;
					parentNode = foundNode;
					foundNode = NULL;
				}
			}
			else
			{
				foundNode = NULL;
				parentNode = NULL;
				break;
			}
		}
		else
		{
			parentNode = NULL;
			parentNode = foundNode;
			foundNode = NULL;
		}
	}

	return parentNode;
}

MSXML2::IXMLDOMNodePtr CXMLFile::GetSingleNode(MSXML2::IXMLDOMDocument2Ptr doc,const char* str)
{
	MSXML2::IXMLDOMNodePtr node;
	node = doc->selectSingleNode(str);
	return node;
}

⌨️ 快捷键说明

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