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


相关推荐

  • Navicat连接sqlserver 2012踩坑

    Navicat连接sqlserver 2012踩坑Navicat连接sqlserver2012踩坑解决问题的办法来自于两个博客(搬运、记录)(1)[配置远账号和登录方式](https://blog.csdn.net/weixin_42241984/article/details/105432253)这里主要是账户的状态要注意2.[配置TCP的动态端口为1433]3.要注意的是在服务器里查看以下三个进程是否已经启动(除了sqlserveragent),以及修改后重启服务。…

    2022年8月30日
    5
  • 到底是谁?BCH近期出现大量未知算力

    到底是谁?BCH近期出现大量未知算力到底是谁?BCH近期出现大量未知算力

    2022年4月22日
    44
  • tidb4.0.6离线安装

    tidb4.0.6离线安装

    2021年5月15日
    138
  • linux中getchar函数用法,linux getchar函数使用

    linux中getchar函数用法,linux getchar函数使用1函数介绍1)函数原型intgetchar(void);2)函数功能从stdin中读取一个字符。3)返回值返回读取字符的ASCII值或者EOF字符或者出错值。4)头文件#include2函数使用2.1getchar函数的特点Linux下编写的一个例子:#includeintmain(void){charch;intnum;num=0;printf…

    2022年10月18日
    2
  • seekg()与tellg()用法详解

    seekg()与tellg()用法详解对输入流操作:seekg()与tellg()对输出流操作:seekp()与tellp()下面以输入流函数为例介绍用法:seekg()是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基地址。对于第一个参数,可以是正负数值,正的表示向后偏移,负的表示向前偏移。而第二个参数可以是:ios::beg:表示输入流的开始位置ios::cur:表示输入流的当前位置ios::end:表示输入流的结束位置tellg()函数不需要带参数,它返回当前定位指针的位置,也代表着输入流的大小。假设

    2022年6月5日
    53
  • RestTemplate设置MediaType[通俗易懂]

    RestTemplate设置MediaType[通俗易懂]Stringurl=”http://testurl”;Map<String,Object>requestMap=newHashMap<>();resultMap.put(“param1″,”value1”);resultMap.put(“param2″,”value2”);HttpHeadersheaders=newHttpH…

    2022年5月9日
    33

发表回复

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

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