c语言解析xml文件

#include "stdafx.h"
#include <map>

#include "Mytext.h"
#include <exception>
#include <string>
#include <atlbase.h>
#include <windows.h>
#include <atlstr.h>
#include <MsXml.h>
#include <UIAnimation.h>
#include <UIAutomationClient.h>
#include <iostream>
#include <stdio.h>
using namespace  std;

#pragma comment(lib,"Oleacc.lib")
#pragma comment(lib,"msxml6.lib")
typedef int(WINAPI *po)(int, int);
wstring ReadValueOfNode(const CComPtr<IXMLDOMNode> &pNode)
{
	//读取节点的文本
	CComBSTR bstr;
	wstring wvalue;
	if (pNode->get_text(&bstr) == S_OK && bstr.m_str != NULL)
	{
		wvalue = bstr;
		return wvalue;
		//strValue = bstr.m_str;
	}

	else
	{
		wvalue.clear();
		return L"错误";
	}

}

void ReadValueOfAttr(const CComPtr<IXMLDOMNamedNodeMap> &pMapAttr, const wstring &strAttrName, wstring &strAttrValue)
{
	//读取节点的某个属性
	//输入 pMapAttr 属性集
	//输入 bstrAttrName 属性名
	//输出 strAttrValue 属性值

	CComPtr<IXMLDOMNode> pNodeAttr;
	CComVariant var;
	if (pMapAttr->getNamedItem(CComBSTR(strAttrName.c_str()), &pNodeAttr) != S_OK)
		throw L"pMapAttr->getNamedItem name error!";
	if (pNodeAttr->get_nodeValue(&var) != S_OK)
		throw L"pNodeAttr->get_nodeValue name error!";
	if (var.vt != VT_BSTR)
		throw L"pNodeAttr->get_nodeValue name error!";
	strAttrValue = var.bstrVal;
}

void GetDomRoot(const WCHAR *szXml, CComPtr<IXMLDOMElement> &pRoot)
{
	CString strTmp = szXml;
	strTmp.Replace(L"^", L"^^");
	strTmp.Replace(L"*", L"**");
	strTmp.Replace(L"
", L" ^ ");//\n
	strTmp.Replace(L"
", L" * ");//\r
	wstring strXml = strTmp;
	CComPtr<IXMLDOMDocument> pDocFromString;
	//if (::CoCreateInstance(CLSID_DOMDocument30, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void **)&pDocFromString) != S_OK)
	throw L"CoCreateInstance for XML error!";
	if (pDocFromString->put_async(VARIANT_FALSE) != S_OK)
		throw L"pDocFromString->put_async error!";
	if (pDocFromString->put_preserveWhiteSpace(VARIANT_TRUE) != S_OK)
		throw L"pDocFromString->put_preserveWhiteSpace error!";

	VARIANT_BOOL b = VARIANT_FALSE;
	/*if (pDocFromString->loadXML(CComBSTR(szXml), &b) != S_OK || b == VARIANT_FALSE)*/
	/*if (pDocFromString->loadXML(CComBSTR(szXml), &b) != S_OK || b == VARIANT_FALSE)
		throw L"pDocFromString->loadXML error!";*/
	if (pDocFromString->load(CComVariant(strXml.c_str()), &b) != S_OK || b == VARIANT_FALSE)
		throw L"pDocFromString->loadXML error!";

	if (pDocFromString->get_documentElement(&pRoot) != S_OK)
		throw L"pDocFromString->get_documentElement error!";
}

void GetDomRootFromTXT(const TCHAR *szXml, CComPtr<IXMLDOMElement> &pRoot)
{
	//CString strTmp = szXml;
	//strTmp.Replace(L"^", L"^^");
	//strTmp.Replace(L"*", L"**");
	//strTmp.Replace(L"
", L" ^ ");//\n
	//strTmp.Replace(L"
", L" * ");//\r
	//wstring strXml = strTmp;
	CComPtr<IXMLDOMDocument> pDocFromString;
	pDocFromString.CoCreateInstance(__uuidof(::DOMDocument));    //创建文档对象 
	if (pDocFromString == nullptr)
		throw L"CoCreateInstance for XML error!";
	VARIANT_BOOL bFlag = VARIANT_FALSE;
	pDocFromString->load(CComVariant(szXml), &bFlag);

	pDocFromString->get_documentElement(&pRoot);
	if (pRoot == NULL)
		throw L"pDocFromString->get_documentElement error!";
	//	::CoUninitialize();
}


void GetXmlText()
{
	wstring strXml = L"AR_ElementCommand.xml";
	::CoInitialize(NULL);
	{//故意加了括号

		CComPtr<IXMLDOMElement> pRoot;
		GetDomRootFromTXT(strXml.c_str(), pRoot);

		CComPtr<IXMLDOMNodeList> pList;
		if (pRoot->get_childNodes(&pList) != S_OK)
			throw L"pRoot->get_childNodes error!";

		long iLen = 0;
		if (pList->get_length(&iLen) != S_OK)
			throw L"pList->get_length error!";

		CComPtr<IXMLDOMNode> pNode;
		CComBSTR bstr;
		for (long i = 0; i < iLen; ++i)
		{
			pNode = NULL;
			if (pList->get_item(i, &pNode) != S_OK)
				throw L"pList->get_item error!";

			bstr.Empty();
			if (pNode->get_nodeName(&bstr) != S_OK)
				throw L"pNode->get_nodeName error!";

			if (bstr == L"Command")
			{
				CComPtr<IXMLDOMNamedNodeMap> pMapAttr;
				if (pNode->get_attributes(&pMapAttr) != S_OK)
					throw L"pNode->get_attributes error!";
				wstring strName, strType, strValue;

				ReadValueOfAttr(pMapAttr, L"name", strName);
				ReadValueOfAttr(pMapAttr, L"type", strType);
				CComPtr<IXMLDOMNodeList> pList;
				if (pNode->get_childNodes(&pList) != S_OK)
					throw L"pNode->get_childNodes error!";

				long iLen = 0;
				if (pList->get_length(&iLen) != S_OK)
					throw L"pList->get_length error!";

				CComPtr<IXMLDOMNode> pParameter;
				CString strTmp;
				for (long i = 0; i < iLen; ++i)
				{
					pParameter = NULL;
					if (pList->get_item(i, &pParameter) != S_OK)
						throw L"pList->get_item error!";

					pMapAttr = NULL;
					if (pParameter->get_attributes(&pMapAttr) != S_OK)
						throw L"pParameter->get_attributes error!";

					ReadValueOfAttr(pMapAttr, L"type", strType);
					strValue =
						ReadValueOfNode(pParameter);
					//std::map<pair<std::wstring, std::wstring> myMapCommnd;
					std::map<wstring, wstring> myMapCommnd;
					myMapCommnd.insert(pair<wstring, wstring>(strType, strValue));
				}
			}
			else if (bstr == "Element")
			{
				CComPtr<IXMLDOMNamedNodeMap> pMapAttr;

				if (pNode->get_attributes(&pMapAttr) != S_OK)
					throw L"pNode->get_attributes error!";
				wstring strName, strType, strValue;
				ReadValueOfAttr(pMapAttr, L"fullNameFormat", strName);
				if (wcscmp(L"winClass,name", strName.c_str()) == 0)
				{
					CComPtr<IXMLDOMNodeList> pListProperty;
					if (pNode->get_childNodes(&pListProperty) != S_OK)
						throw L"pNode->get_childNodes error!";

					long iLenProperty = 0;
					if (pListProperty->get_length(&iLenProperty) != S_OK)
						throw L"pListProperty->get_length error!";

					CComPtr<IXMLDOMNode> pProperty;
					CComPtr<IXMLDOMNamedNodeMap> pMapAttr;
					wstring strKey, strValue, strWeight, strAction;
					int iWeight = 0;
					for (long j = 0; j < iLenProperty; ++j)
					{
						pProperty = NULL;
						if (pListProperty->get_item(j, &pProperty) != S_OK)
							throw L"pList->get_item error!";

						bstr.Empty();
						if (pProperty->get_nodeName(&bstr) != S_OK)
							throw L"pProperty->get_nodeName error!";
						if (bstr != L"Property")
							throw L"bstr != Property error!";

						pMapAttr = NULL;
						if (pProperty->get_attributes(&pMapAttr) != S_OK)
							throw L"pProperty->get_attributes error!";

						ReadValueOfAttr(pMapAttr, L"key", strKey);
						ReadValueOfAttr(pMapAttr, L"weight", strWeight);
						strValue =
							ReadValueOfNode(pProperty);
						std::map <std::wstring, std::wstring> myMapWinClass;
						myMapWinClass.insert(pair<wstring, wstring>(strKey, strWeight));

					}
					//ReadValueOfAttr(pMapAttr, L"type", strType);
				}
				else if (wcscmp(L"role,name", strName.c_str()) == 0)
				{
					CComPtr<IXMLDOMNodeList> pListProperty;
					if (pNode->get_childNodes(&pListProperty) != S_OK)
						throw L"pNode->get_childNodes error!";

					long iLenProperty = 0;
					if (pListProperty->get_length(&iLenProperty) != S_OK)
						throw L"pListProperty->get_length error!";

					CComPtr<IXMLDOMNode> pProperty;
					CComPtr<IXMLDOMNamedNodeMap> pMapAttr;
					wstring strKey, strValue, strWeight, strAction;
					int iWeight = 0;
					for (long j = 0; j < iLenProperty; ++j)
					{
						pProperty = NULL;
						if (pListProperty->get_item(j, &pProperty) != S_OK)
							throw L"pList->get_item error!";

						bstr.Empty();
						if (pProperty->get_nodeName(&bstr) != S_OK)
							throw L"pProperty->get_nodeName error!";
						if (bstr != L"Property")
							throw L"bstr != Property error!";

						pMapAttr = NULL;
						if (pProperty->get_attributes(&pMapAttr) != S_OK)
							throw L"pProperty->get_attributes error!";

						ReadValueOfAttr(pMapAttr, L"key", strKey);
						ReadValueOfAttr(pMapAttr, L"weight", strWeight);
						strValue =
							ReadValueOfNode(pProperty);
						std::map<std::wstring, std::wstring> myMapRole;
						myMapRole.insert(pair<wstring, wstring>(strKey, strWeight));

					}
					//	m_command.FromXmlDom(pNode);

				}

			}

		}

	}
	::CoUninitialize();
}

int main()
{
	GetXmlText();

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值