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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • dw网页设计怎么加背景音乐_dw网页如何设置背景音乐mp3

    dw网页设计怎么加背景音乐_dw网页如何设置背景音乐mp3本文以mid格式文件和mp3格式文件为例,教你如何在网页中插入背景音乐。1、MID是用来插入背景音乐,但只适用于IE,其参数设定不多。如下:src=”your.mid”:设定midi档案及路径,可以是相对或绝对。autostart=true:是否在音乐档下载完之后就自动播放。true是,false否(内定值)。loop=infinite:是否自动反复播放。LOOP=2表示重复两次…

  • python冒泡排序代码和注释_Python 冒泡排序

    python冒泡排序代码和注释_Python 冒泡排序冒泡排序(BubbleSort)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢”浮”到数列的顶端。见下图:方法一:常规实现冒泡排序#方法1#定义一个列表,用于存放数字list=[]whileTrue:#…

    2022年10月16日
  • SQLyog下载_下载地址

    SQLyog下载_下载地址下载地址:百度网盘请输入提取码链接:https://pan.baidu.com/s/1Xowhx0uuAxykHPmuXztNEQ提取码:e9o2希望对你有所帮助~

  • Linux系统怎么修改用户名_怎么更改微软用户名

    Linux系统怎么修改用户名_怎么更改微软用户名修改用户名(以CentOS-8为例)①进入root用户进行操作,在/etc/sudoers添加admin(新用户名)ALL=(ALL)ALLsurootvim/etc/sudoers②进入/etc/passwd将旧用户名改为admin(新用户名)vim/etc/passwd③进入/etc/group将旧用户名改为admin(新用户名)vim/etc/group④进入/etc/shadow将旧用户名改为admin(新用户名)

  • 更新jsp需要重启tomcat(如何看tomcat的端口)

    今天自己搭建的spring+springmvc+mybatis时,发现修改的Jsp页面静态数据,刷新页面不能及时生效,需要重启tomcat才能生效。把解决方法归纳如下:1、选择tomcat设置:2、进行如下设置:说明:on‘update‘action:当用户主动执行更新的时候更新    快捷键:Ctrl+F9onframedeactication:在编辑窗口失去焦点的时候更新你可以根据…

  • 如何在git中删除指定的文件和目录

    如何在git中删除指定的文件和目录

    2021年10月23日

发表回复

您的电子邮箱地址不会被公开。

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