发布网友 发布时间:2022-04-26 21:30
共1个回答
热心网友 时间:2023-11-05 02:23
#include <windows.h>
#include <tchar.h>
#include <objbase.h>
#include <msxml2.h>
#include <stdio.h>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "uuid.lib")
#pragma comment(lib, "msxml2.lib")
LPWSTR szXml =
L"<?xml version=\"1.0\"?>\r\n"
L"<Configuration><Modbus modbus=\"G1A\">"
L"<Enable>Enable</Enable>"
L"<Type>Master</Type>"
L"<Protocol>RTU</Protocol>"
L"<RingNumber>1</RingNumber>"
L"<BaudRate>19200</BaudRate>"
L"<Parity>N</Parity>"
L"<DataBits>8</DataBits>"
L"<StopBit>1</StopBit>"
L"<ResponseDelay>1</ResponseDelay>"
L"<ResponseTimeout>800</ResponseTimeout>"
L"<RetryCount>2</RetryCount>"
L"<ErrorDelayCounter>200</ErrorDelayCounter>"
L"</Modbus>"
L"<Modbus modbus=\"H2B\">"
L"<Enable>Enable</Enable>"
L"<Type>Master</Type>"
L"<Protocol>RTU</Protocol>"
L"<RingNumber>1</RingNumber>"
L"<BaudRate>9600</BaudRate>"
L"<Parity>E</Parity>"
L"<DataBits>8</DataBits>"
L"<StopBit>1</StopBit>"
L"<ResponseDelay>1</ResponseDelay>"
L"<ResponseTimeout>500</ResponseTimeout>"
L"<RetryCount>3</RetryCount>"
L"<ErrorDelayCounter>200</ErrorDelayCounter>"
L"</Modbus>"
L"</Configuration>";
int main()
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
HRESULT hr;
IXMLDOMDocument* pXml;
hr = CoCreateInstance(CLSID_DOMDocument30, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pXml);
if (hr != S_OK) {
puts("无法创建对象");
return 1;
}
VARIANT_BOOL bRetLoadXml;
BSTR bstrXml = SysAllocString(szXml);
pXml->loadXML(bstrXml, &bRetLoadXml);
if (bRetLoadXml == VARIANT_FALSE) {
puts("加载XML失败");
pXml->Release();
SysFreeString(bstrXml);
return 1;
}
IXMLDOMNode* pConf;
BSTR bstrXPath;
bstrXPath = SysAllocString(L"/Configuration");
hr = pXml->selectSingleNode(bstrXPath, &pConf);
SysFreeString(bstrXPath);
if (hr != S_OK) {
puts("查找根节点失败");
pXml->Release();
SysFreeString(bstrXml);
return 1;
}
IXMLDOMNode* pSubNode = 0;
IXMLDOMNode* pNextSubNode;
hr = pConf->get_firstChild(&pNextSubNode);
if (hr != S_OK) {
puts("获取配置失败");
pConf->Release();
SysFreeString(bstrXml);
pXml->Release();
return 1;
}
do {
if (pSubNode != 0) pSubNode->Release();
pSubNode = pNextSubNode;
//get section caption
BSTR bstrText;
pSubNode->get_nodeName(&bstrText);
wprintf(L"[%s", bstrText);
SysFreeString(bstrText);
IXMLDOMNamedNodeMap* pAttrs;
hr = pSubNode->get_attributes(&pAttrs);
{
IXMLDOMNode* pAttr;
pAttrs->get_item(0, &pAttr);
if (pAttr != NULL) {
BSTR bstrAttrVal;
pAttr->get_text(&bstrAttrVal);
wprintf(L" %s", bstrAttrVal);
SysFreeString(bstrAttrVal);
pAttr->Release();
}
}
pAttrs->Release();
wprintf(L"%s\n", L"]");
//get key-value pairs
IXMLDOMNode* pKVPair = 0;
IXMLDOMNode* pNextKVPair = 0;
pSubNode->get_firstChild(&pNextKVPair);
do {
if(pKVPair != 0) pKVPair->Release();
pKVPair = pNextKVPair;
BSTR bstrKorV;
pKVPair->get_nodeName(&bstrKorV);
wprintf(L"%-32s", bstrKorV);
SysFreeString(bstrKorV);
wprintf(L"%s", L" : ");
pKVPair->get_text(&bstrKorV);
wprintf(L"%s", bstrKorV);
SysFreeString(bstrKorV);
wprintf(L"\n");
} while(pKVPair->get_nextSibling(&pNextKVPair) == S_OK);
wprintf(L"%s", L"\n");
}
while(pSubNode->get_nextSibling(&pNextSubNode) == S_OK);
pSubNode->Release();
pConf->Release();
SysFreeString(bstrXml);
pXml->Release();
CoUninitialize();
}
用MSXML解析。
都写到这程度了,剩下的根据需要自己改吧。比如wprintf改成你要输出成字符串或者什么的。
MSXML这com组件全程走的Unicode,所以字符串常量都是带L的,如果不知道什么是Unicode不知道如何和普通字符串(char xxx[])互相转换,请自己去查。com组件的初始化和资源清理请在整个程序的开头和结尾做。
热心网友 时间:2023-11-05 02:23
#include <windows.h>
#include <tchar.h>
#include <objbase.h>
#include <msxml2.h>
#include <stdio.h>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "uuid.lib")
#pragma comment(lib, "msxml2.lib")
LPWSTR szXml =
L"<?xml version=\"1.0\"?>\r\n"
L"<Configuration><Modbus modbus=\"G1A\">"
L"<Enable>Enable</Enable>"
L"<Type>Master</Type>"
L"<Protocol>RTU</Protocol>"
L"<RingNumber>1</RingNumber>"
L"<BaudRate>19200</BaudRate>"
L"<Parity>N</Parity>"
L"<DataBits>8</DataBits>"
L"<StopBit>1</StopBit>"
L"<ResponseDelay>1</ResponseDelay>"
L"<ResponseTimeout>800</ResponseTimeout>"
L"<RetryCount>2</RetryCount>"
L"<ErrorDelayCounter>200</ErrorDelayCounter>"
L"</Modbus>"
L"<Modbus modbus=\"H2B\">"
L"<Enable>Enable</Enable>"
L"<Type>Master</Type>"
L"<Protocol>RTU</Protocol>"
L"<RingNumber>1</RingNumber>"
L"<BaudRate>9600</BaudRate>"
L"<Parity>E</Parity>"
L"<DataBits>8</DataBits>"
L"<StopBit>1</StopBit>"
L"<ResponseDelay>1</ResponseDelay>"
L"<ResponseTimeout>500</ResponseTimeout>"
L"<RetryCount>3</RetryCount>"
L"<ErrorDelayCounter>200</ErrorDelayCounter>"
L"</Modbus>"
L"</Configuration>";
int main()
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
HRESULT hr;
IXMLDOMDocument* pXml;
hr = CoCreateInstance(CLSID_DOMDocument30, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pXml);
if (hr != S_OK) {
puts("无法创建对象");
return 1;
}
VARIANT_BOOL bRetLoadXml;
BSTR bstrXml = SysAllocString(szXml);
pXml->loadXML(bstrXml, &bRetLoadXml);
if (bRetLoadXml == VARIANT_FALSE) {
puts("加载XML失败");
pXml->Release();
SysFreeString(bstrXml);
return 1;
}
IXMLDOMNode* pConf;
BSTR bstrXPath;
bstrXPath = SysAllocString(L"/Configuration");
hr = pXml->selectSingleNode(bstrXPath, &pConf);
SysFreeString(bstrXPath);
if (hr != S_OK) {
puts("查找根节点失败");
pXml->Release();
SysFreeString(bstrXml);
return 1;
}
IXMLDOMNode* pSubNode = 0;
IXMLDOMNode* pNextSubNode;
hr = pConf->get_firstChild(&pNextSubNode);
if (hr != S_OK) {
puts("获取配置失败");
pConf->Release();
SysFreeString(bstrXml);
pXml->Release();
return 1;
}
do {
if (pSubNode != 0) pSubNode->Release();
pSubNode = pNextSubNode;
//get section caption
BSTR bstrText;
pSubNode->get_nodeName(&bstrText);
wprintf(L"[%s", bstrText);
SysFreeString(bstrText);
IXMLDOMNamedNodeMap* pAttrs;
hr = pSubNode->get_attributes(&pAttrs);
{
IXMLDOMNode* pAttr;
pAttrs->get_item(0, &pAttr);
if (pAttr != NULL) {
BSTR bstrAttrVal;
pAttr->get_text(&bstrAttrVal);
wprintf(L" %s", bstrAttrVal);
SysFreeString(bstrAttrVal);
pAttr->Release();
}
}
pAttrs->Release();
wprintf(L"%s\n", L"]");
//get key-value pairs
IXMLDOMNode* pKVPair = 0;
IXMLDOMNode* pNextKVPair = 0;
pSubNode->get_firstChild(&pNextKVPair);
do {
if(pKVPair != 0) pKVPair->Release();
pKVPair = pNextKVPair;
BSTR bstrKorV;
pKVPair->get_nodeName(&bstrKorV);
wprintf(L"%-32s", bstrKorV);
SysFreeString(bstrKorV);
wprintf(L"%s", L" : ");
pKVPair->get_text(&bstrKorV);
wprintf(L"%s", bstrKorV);
SysFreeString(bstrKorV);
wprintf(L"\n");
} while(pKVPair->get_nextSibling(&pNextKVPair) == S_OK);
wprintf(L"%s", L"\n");
}
while(pSubNode->get_nextSibling(&pNextSubNode) == S_OK);
pSubNode->Release();
pConf->Release();
SysFreeString(bstrXml);
pXml->Release();
CoUninitialize();
}
用MSXML解析。
都写到这程度了,剩下的根据需要自己改吧。比如wprintf改成你要输出成字符串或者什么的。
MSXML这com组件全程走的Unicode,所以字符串常量都是带L的,如果不知道什么是Unicode不知道如何和普通字符串(char xxx[])互相转换,请自己去查。com组件的初始化和资源清理请在整个程序的开头和结尾做。