访问 XML 结构中的属性值 - 使用 MSXML6 的 C++

Accessing attribute values in an XML structure- C++ using MSXML6

提问人:tharun thomas 提问时间:10/20/2021 最后编辑:Remy Lebeautharun thomas 更新时间:10/20/2021 访问量:327

问:

我在下面有这个XML文件:

<catalog>   
  <book id="bk101">
    <author> Gambardella, Matthew </author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>An in-depth look at creating applications with XML.</description>
  </book>
</catalog> 

我想在节点内打印属性(即 的值,即 ),但是我在使用 MSXML6 库时遇到麻烦,我想坚持使用它而不是更改为 Rapid 等。bookidbk101

以下是我到目前为止的代码:

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#import <msxml6.dll> rename_namespace(_T("MSXML"))
#include<iostream>
#include<string>

int main(int argc, char* argv[]) {
    HRESULT hr = CoInitialize(NULL);
    if (SUCCEEDED(hr)) {
        try {
            MSXML::IXMLDOMDocument2Ptr xmlDoc;
            MSXML::IXMLDOMNodePtr bookholder;
            MSXML::IXMLDOMNodePtr author;
            MSXML::IXMLDOMNodePtr title;
            
            hr = xmlDoc.CreateInstance(__uuidof(MSXML::DOMDocument60),
                NULL, CLSCTX_INPROC_SERVER);
            // TODO: if (FAILED(hr))...
            
            if (xmlDoc->load(_T("books.xml")) != VARIANT_TRUE) {
                printf("Unable to load input.xml\n");
            }
            else {
                printf("XML was successfully loaded\n");

                xmlDoc->setProperty("SelectionLanguage", "XPath");
                MSXML::IXMLDOMNodeListPtr booklist = xmlDoc->selectNodes("/catalog/*");
                bookholder = booklist->Getitem(0);
                //printf(bookholder->Getxml()+"\n"); //works till here
                //printf(bookholder->; //what to do here to print attribute?
            }
        }
        catch (_com_error& e) {
            printf("ERROR: %ws\n", e.ErrorMessage());
        }
        CoUninitialize();
    }
    return 0;
}
C++ XML MSXML6

评论

0赞 Remy Lebeau 10/20/2021
您是否阅读了 IXMLDOMNode 文档? 具有属性集合。IXMLDOMNode
0赞 tharun thomas 10/20/2021
谢谢 Remy,我无法找到有关 MSXML 库的好文档,但这看起来很有希望!

答: 暂无答案