未知昵称
2018-11-07 03:17
采纳率: 89.7%
浏览 1.6k

C++使用msxml解析XML文件

 <?xml version="1.0" encoding="utf-8"?>
<AUTOSAR xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://autosar.org/schema/r4.0"  xsi:schemaLocation="http://autosar.org/schema/r4.0 AUTOSAR_00042.xsd">
    <ADMIN-DATA>
        <LANGUAGE>EN</LANGUAGE>
        <USED-LANGUAGES>
            <L-10 L="EN"/>
        </USED-LANGUAGES>
    </ADMIN-DATA>
    <AR-PACKAGES>
        <AR-PACKAGE>
            <SHORT-NAME>DataTypes</SHORT-NAME>
            <AR-PACKAGES>
                <AR-PACKAGE>
                    <SHORT-NAME>BaseTypes</SHORT-NAME>
                    <ELEMENTS>
                        <SW-BASE-TYPE>
                            <SHORT-NAME>boolean</SHORT-NAME>
                            <CATEGORY>FIXED_LENGTH</CATEGORY>
                            <BASE-TYPE-SIZE>8</BASE-TYPE-SIZE>
                            <MAX-BASE-TYPE-SIZE>0</MAX-BASE-TYPE-SIZE>
                            <BASE-TYPE-ENCODING>NONE</BASE-TYPE-ENCODING>
                            <MEM-ALIGNMENT>0</MEM-ALIGNMENT>
                            <NATIVE-DECLARATION>boolean</NATIVE-DECLARATION>
                        </SW-BASE-TYPE>
                        <SW-BASE-TYPE>
                            <SHORT-NAME>uint8</SHORT-NAME>
                            <CATEGORY>FIXED_LENGTH</CATEGORY>
                            <BASE-TYPE-SIZE>8</BASE-TYPE-SIZE>
                            <MAX-BASE-TYPE-SIZE>0</MAX-BASE-TYPE-SIZE>
                            <BASE-TYPE-ENCODING>NONE</BASE-TYPE-ENCODING>
                            <MEM-ALIGNMENT>0</MEM-ALIGNMENT>
                            <NATIVE-DECLARATION>uint8</NATIVE-DECLARATION>
                        </SW-BASE-TYPE>
                        <SW-BASE-TYPE>
                            <SHORT-NAME>uint16</SHORT-NAME>
                            <CATEGORY>FIXED_LENGTH</CATEGORY>
                            <BASE-TYPE-SIZE>16</BASE-TYPE-SIZE>
                            <MAX-BASE-TYPE-SIZE>0</MAX-BASE-TYPE-SIZE>
                            <BASE-TYPE-ENCODING>NONE</BASE-TYPE-ENCODING>
                            <MEM-ALIGNMENT>0</MEM-ALIGNMENT>
                            <NATIVE-DECLARATION>uint16</NATIVE-DECLARATION>
                        </SW-BASE-TYPE>
                        <SW-BASE-TYPE>
                            <SHORT-NAME>uint32</SHORT-NAME>
                            <CATEGORY>FIXED_LENGTH</CATEGORY>
                            <BASE-TYPE-SIZE>32</BASE-TYPE-SIZE>
                            <MAX-BASE-TYPE-SIZE>0</MAX-BASE-TYPE-SIZE>
                            <BASE-TYPE-ENCODING>NONE</BASE-TYPE-ENCODING>
                            <MEM-ALIGNMENT>0</MEM-ALIGNMENT>
                            <NATIVE-DECLARATION>uint32</NATIVE-DECLARATION>
                        </SW-BASE-TYPE>
                    </ELEMENTS>
                </AR-PACKAGE>
            </AR-PACKAGES>
        </AR-PACKAGE>
    </AR-PACKAGES>
</AUTOSAR>

如上所示,一段xml文件,现在想要用msxml解析这段XML文件,

现在想要获取ELEMENTS下,所有SW-BASE-TYPE内所有子节点的内容集合,

msxml库简单介绍

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dabocaiqq 2018-11-07 03:26
    已采纳

    #include "stdafx.h"
    #include

    using namespace std;

    #import
    using namespace MSXML2;

    int ReadXMLFile()
    {
    CoInitialize(NULL);

    char *xmlFile = "student.xml";
    MSXML2::IXMLDOMDocumentPtr pdoc = NULL;
    MSXML2::IXMLDOMNodeListPtr pnodeList = NULL;
    MSXML2::IXMLDOMElementPtr prootElement = NULL;
    MSXML2::IXMLDOMNodePtr pnode = NULL;
    MSXML2::IXMLDOMNamedNodeMapPtr pattrList = NULL;
    MSXML2::IXMLDOMAttributePtr pattrNode = NULL;

    HRESULT hr = pdoc.CreateInstance(__uuidof(DOMDocument60));
    if(FAILED(hr))
    {
    printf("无法创建DOMDocument60对象!");
    return EXIT_FAILURE;
    }

    VARIANT_BOOL xmlLoad = pdoc->load((_variant_t)xmlFile);
    //加载失败
    if(!xmlLoad)
    {
    printf("加载%s失败!\n", xmlFile);
    return EXIT_FAILURE;
    }

    prootElement = pdoc->GetdocumentElement();
    cout<< (char*)prootElement->GetnodeName() <<endl;

    CoUninitialize();

    return 0;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    ReadXMLFile();
    getchar();
    return 0;
    }

    点赞 评论
  • zqbnqsdsmd 2018-11-07 03:35

    你可以先用msxml拿到ELEMENTS。然后再循环一下。

    点赞 评论

相关推荐 更多相似问题