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)


相关推荐

  • Mysql学习总结(10)——MySql触发器使用讲解

    Mysql学习总结(10)——MySql触发器使用讲解

  • mt4下载正版官网下载(如何分辨真假MT4软件)

    mt4下载正版官网下载(如何分辨真假MT4软件)在全球零售外汇行业,外汇经纪商使用最多的还是俄罗斯迈达克公司的MT4交易平台,一些不合规的外汇经纪商也对MT4十分热衷,这使市场上几千块一个的盗版MT4日益猖獗,致使一部分交易者因此遭受一些不必要的利益侵害。那么MT4。fOrex6。cc的特点是什么?如何判别一个MT4软件是否是盗版?今天就带你们辨别真假MT4.MT4的优势1.强大的工作表现MT4强大的工作表现,这一点是毋庸置疑的。MT4自2005年7月1日推出以来,就不断的获得市场的认可。下单灵活、界面友好、交易直观等这些都是MT4平台成为外汇市场

  • Android中实现二级菜单

    Android中实现二级菜单公司项目里的需求,商城的左下角,要求有分类的Menu。看图:1.当点击一级分类,修改二级菜单里面的内容。2.同时一级菜单里的背景变为右边有白色三角的背景。3.当点击二级菜单的条目时候,Popupwindow消失,并且界面更新对应条目的商品数据。我的实现思路是:左下角弹出Popupwindow,里面使用两个ListView实现。我的Demo的效果:

  • js用户管理中心tab切换界面模板

    效果体验:http://hovertree.com/texiao/js/27/效果图如下:代码如下:转自:http://hovertree.com/h/bjaf/hovertreepanel.htm

    2021年12月24日
  • word在试图打开文件时遇到错误,一种解决办法[通俗易懂]

    word在试图打开文件时遇到错误,一种解决办法[通俗易懂]1.双击word打开时弹出错误窗口弹出错误,无法打开word;2.解决办法此时右键打开word的属性窗口:选择下面的解除锁定,点击;锁定解除后,就可以正常打开word进行查看了。

  • Vue学习之按键修饰符

    Vue学习之按键修饰符Vue学习之按键修饰符

发表回复

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

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