大家好,又见面了,我是你们的朋友全栈君。
#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账号...