c语言解析xml文件「建议收藏」

c语言解析xml文件「建议收藏」#include”stdafx.h”#include#include”Mytext.h”#include#include#include#include#include#include#include#include#include#includeusingnamespacestd;#pragmacomment(lib,”Oleac

大家好,又见面了,我是你们的朋友全栈君。

#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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159307.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • cBridge 2.0 – 200万美元漏洞赏金计划

    cBridge 2.0 – 200万美元漏洞赏金计划我们发布了cBridge2.0-200万美元漏洞赏金计划,快来看看吧!

    2022年6月3日
    30
  • 树莓派python编程自学-树莓派Python编程指南 中文PDF扫描版

    《树莓派Python编程指南》共12章:第1~3章深入介绍如何在树莓派上使用Python,为学习其他知识打下基础;第4章介绍使用Qt工具包开发图形用户界面;第5章介绍在Python中创建游戏;第6章介绍如何利用GPU来使用OpenGL创建3D场景;第7章讲解如何从网页上抓取信息或使用树莓派为全世界提供内容和服务;第8章介绍使用Python在树莓派中构建自己的游戏世界;第9章使用Python在树莓派…

    2022年4月8日
    131
  • ES6中set和map「建议收藏」

    ES6中set和map「建议收藏」一。set数据容器能够存储无重复值数据的有序列表1.通过newset()方法创建容器通过add()方法添加2.set.size获取存储的数据的数量例: varset=newSet() set.add(1); set.add(‘1’); console.log(set)console.log(set.size)3.Set内部使用Obj…

    2022年9月7日
    0
  • npm使用淘宝镜像(npm切换淘宝镜像)

    1.通过cnpm使用淘宝镜像:npminstall-gcnpm–registry=https://registry.npm.taobao.org2.将npm设置为淘宝镜像:npmconfigsetregistryhttps://registry.npm.taobao.org3.查看cnpm镜像设置:cnpmconfiggetregistry

    2022年4月10日
    357
  • C语言程序设计50例(经典收藏)[通俗易懂]

    C语言程序设计50例(经典收藏)本篇文章是对C语言程序设计的50个小案例进行了详细的分析介绍,需要的朋友参考下【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去      掉不满足条件的排列。2.程序源代码:代码如下:#include&quot;stdio.h&quot;#i…

    2022年4月18日
    64
  • java_简易画图板

    java_简易画图板下面我将分享用Java制作简易画图板的过程。version1Draw.javaJava代码importjavax.swing.JFrame;/****@authoryangzhenlin*

    2022年7月2日
    19

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号