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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Python15行代码实现免费发送手机短信,推送消息「建议收藏」

    Python15行代码实现免费发送手机短信,推送消息

    2022年2月13日
    42
  • Eclipse常见错误overlaps the location of another project: ‘xxx

    Eclipse常见错误overlaps the location of another project: ‘xxxEclipse常见错误overlaps the location of another project: ‘xxx

    2022年4月23日
    69
  • 跨境电商erp源码java大卖先生_erp 开源

    跨境电商erp源码java大卖先生_erp 开源1订单管理本模块支持多平台订单自动下载同步以及多帐号多店铺订单管理,方便用户对销售进行科学、直观的分类管理。包括订单处理,包装验货,称重出库,智能交运,交运日志,快速拣货,快速发货等子模块。2商品管理(SKU)商品管理模块,提供对亚马逊店逊商品进行线下管理的功能,包括但不限于中文名称、英文名称,售价等相应管理3.采购管理采购管理主要对于商品采购、入库、及供应商的设置,并于商品细分,包括采购管理、入库管理和供应商管理模块。4.物流管理此模块主要提供用户设置速…

    2022年9月20日
    1
  • OpenProcessToken令牌函数用法「建议收藏」

    OpenProcessToken令牌函数用法「建议收藏」>GetCurrentProcessID得到当前进程的IDOpenProcessToken得到进程的令牌句柄LookupPrivilegeValue查询进程的权限AdjustTokenPrivileges调整令牌权限要对一个任意进程(包括系统安全进程和服务进程)进行指定了写相关的访问权的OpenProcess操作,只要当前进程具有SeDeDebug权限就可以了。要是一个用户是Admin

    2022年6月25日
    22
  • 字符串分割方法代码

    字符串分割方法代码

    2021年8月24日
    63
  • C++ 中的getline()函数用法详解

    C++ 中的getline()函数用法详解    遇到了要输入一行字符串的操作,我想除了fgets()的方法(fgets()用法链接),getline()也是可以的,但是我对getline的操作不熟悉,便查阅了很多资料,发现都说的很模糊,借这个机会我想彻底理清楚getline的用法;  网上有说getline有两种用法的,我在这总结一下,一、getline()用的比较多的用法 1) istrea…

    2025年6月1日
    0

发表回复

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

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