msxml6 C++「建议收藏」

msxml6 C++「建议收藏」#includestdlib.h#includestdio.h//引入MSXML解析器#importmsxml6.dllusingnamespaceMSXML2;intmain(){constchar*sz

msxml6

 一 xml生成

#include <stdlib.h>
#include <stdio.h>
 
// 引入MSXML解析器
#import <msxml6.dll>

using namespace MSXML2;

int main()
{
    const char *szXmlFile = "D:\\china.xml"; // xml文件

    MSXML2::IXMLDOMDocumentPtr pDoc = NULL; // xml文档

    MSXML2::IXMLDOMProcessingInstructionPtr pProInstruction = NULL; // xml声明

    MSXML2::IXMLDOMCommentPtr pComment = NULL; // 注释

    MSXML2::IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // 根节点(元素)

    MSXML2::IXMLDOMNodePtr pNode = NULL, pNode1 = NULL, pNode2 = NULL; // 节点

    MSXML2::IXMLDOMAttributePtr pAttrNode = NULL; // 属性

    HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument60)); //

    if (FAILED(hr))
    {
        printf("无法创建DOMDocument40对象,请检查是否安装并初始化了MsXml Parser库!");
        return EXIT_FAILURE;
    }   

    // (1)创建xml文档声明(或insertBefore根节点)
    pProInstruction = pDoc->createProcessingInstruction((_bstr_t)(char*)"xml", (_bstr_t)(char*)"version=\"1.0\" encoding=\"utf-8\"");
    pDoc->appendChild((MSXML2::IXMLDOMNode*)pProInstruction);
 
    // (2)创建根节点<China>
    pRootElement =  pDoc->createElement((_bstr_t)(char*)"China");   
    pDoc->PutRefdocumentElement(pRootElement); // pXMLDomDoc->documentElement = pRootElement;
 
    // (3)创建节点<China><Continent>   
    pComment = pDoc->createComment((_bstr_t)(char*)"所在的洲");
    pRootElement->appendChild((MSXML2::IXMLDOMNode*)pComment); // 注释

    pNode = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"Continent", (_bstr_t)(char*)"");
    pNode->Puttext((_bstr_t)(char*)"Asia"); // pNode->text = "Asia";
    pRootElement->appendChild(pNode); // 节点

    // (4)创建节点<China><Population>
    pComment = pDoc->createComment((_bstr_t)(char*)"人口数量");
    pRootElement->appendChild((MSXML2::IXMLDOMNode*)pComment); // 注释

    pElement = pDoc->createElement((_bstr_t)(char*)"Population");
    pAttrNode = pDoc->createAttribute((_bstr_t)(char*)"Units");
    pAttrNode->Puttext((_bstr_t)(char*)"Million Person");
    pElement->setAttributeNode(pAttrNode); // 统计单位
    pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 统计年份
    pElement->Puttext((_bstr_t)(char*)"1,296");
    pRootElement->appendChild(pElement); // 节点

    // (5)创建节点<China><Municipality>
    pComment = pDoc->createComment((_bstr_t)(char*)"四个直辖市");
    pRootElement->appendChild((MSXML2::IXMLDOMNode*)pComment); // 注释

    pNode = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"Municipality", (_bstr_t)(char*)"");
    pRootElement->appendChild(pNode); // 节点
 
    // (6)创建节点<China><Municipality><TianJin>
    pNode1 = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"TianJin", (_bstr_t)(char*)"");

    // 创建节点<China><Municipality><TianJin><Area>
    pElement = pDoc->createElement((_bstr_t)(char*)"Area");
    pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Thousand Square kilometers"); // 统计单位
    pElement->Puttext((_bstr_t)(char*)"12");
    pNode1->appendChild((MSXML2::IXMLDOMNode*)pElement); // 节点

    // 创建节点<China><Municipality><TianJin><Population>
    pElement = pDoc->createElement((_bstr_t)(char*)"Population");
    pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Million Person"); // 统计单位
    pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 统计年份
    pElement->Puttext((_bstr_t)(char*)"10.01");
    pNode1->appendChild((MSXML2::IXMLDOMNode*)pElement); // 节点

    pNode->appendChild(pNode1);

    // (7)创建节点<China><Municipality><BeiJing>并插入<TianJin>前
    pNode2 = pDoc->createNode((_variant_t)(long)MSXML2::NODE_ELEMENT, (_bstr_t)(char*)"BeiJing", (_bstr_t)(char*)"");

    // 创建节点<China><Municipality><BeiJing><Area>
    pElement = pDoc->createElement((_bstr_t)(char*)"Area");
    pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Thousand Square kilometers"); // 统计单位
    pElement->Puttext((_bstr_t)(char*)"17");
    pNode2->appendChild((MSXML2::IXMLDOMNode*)pElement); // 节点

    // 创建节点<China><Municipality><BeiJing><Population>
    pElement = pDoc->createElement((_bstr_t)(char*)"Population");
    pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Million Person"); // 统计单位
    pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 统计年份
    pElement->Puttext((_bstr_t)(char*)"13.82");
    pNode2->appendChild((MSXML2::IXMLDOMNode*)pElement); // 节点
    pNode->insertBefore(pNode2, (_variant_t)(IDispatch*)pNode1);

    // (8)创建节点<China><Municipality><ShangHai>
    // (9)创建节点<China><Municipality><ChongQing>
    pDoc->save((_variant_t)szXmlFile);

    return EXIT_SUCCESS;
}

二 xml解析

// XmlParsingDemo.cpp
#include <stdlib.h>
#include <stdio.h>

// 引入MSXML解析器
#import <msxml4.dll>

using namespace MSXML2;


class InitializeCom
{
public:
    InitializeCom()  {  CoInitialize(NULL); // Initializes the COM library  }
    ~InitializeCom() {  CoUninitialize(); // Closes the COM library  }
} InitCom;

int main()
{
    char *szXmlFile = "D:\\china.xml"; //上篇创建的xml文档

    IXMLDOMDocumentPtr pDoc = NULL; // xml文档

    IXMLDOMNodeListPtr pNodeList = NULL; // 节点链表

    IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // 根节点(元素)

    IXMLDOMNodePtr pNode = NULL, pNode1 = NULL; // 节点

    IXMLDOMNamedNodeMapPtr pAttrList = NULL; // 属性链表

    IXMLDOMAttributePtr pAttrNode = NULL; // 属性

    long lChilds, lAttr, i;

    HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument40));

    if (FAILED(hr))
    {
        printf("无法创建DOMDocument40对象,请检查是否安装并初始化了MsXml Parser库!");
        return EXIT_FAILURE;
    }

    VARIANT_BOOL bXmlLoad = pDoc->load((_variant_t)szXmlFile);

    if (!bXmlLoad) // 加载失败
    {
        printf("加载%s失败!\n", szXmlFile);
        return EXIT_FAILURE;
    }

    // (1)根节点
    pRootElement = pDoc->GetdocumentElement();
    printf("root = %s\n", (char*)pRootElement->GetnodeName()); // pRootElement->nodeName

    // (2)根节点的一级子节点
    pNodeList = pRootElement->GetchildNodes(); // pRootElement->childNodes
    lChilds = pNodeList->Getlength(); // pNodeList->length

    for (i = 0; i < lChilds; i++)
    {
        pNode = pNodeList->Getitem(i); // pNodeList->item[i]

        if (pNode->GetnodeType() != NODE_COMMENT) // 过滤注释节点
        {
            printf("child[%d] of [%s]: [%s]\n", i ,(char*)pRootElement->GetnodeName(), (char*)pNode->GetnodeName());
        }
    }

    // (3)统计文档中所有的<Population>节点
    pNodeList = pDoc->getElementsByTagName((_bstr_t)(char*)"Population");
    lChilds = pNodeList->Getlength();
    printf("文档中[Population]共有%d个\n", lChilds);

    // (4)根节点下的<Population>节点
    pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"Population");

    // 已知根节点为<China>时:pNode = pDoc->selectSingleNode((_bstr_t)(char*)"China//Population");
    printf("根节点下的[Population]子节点值为%s\n", (char*)pNode->Gettext());
    pAttrList = pNode->Getattributes();
    lAttr     = pAttrList->Getlength();

    for (i = 0; i < lAttr; i++)
    {
        pAttrNode = pAttrList->Getitem(i);
        printf("Attr[%d] of [%s]: %s = %s\n", i, (char*)pNode->GetnodeName(), (char*)pAttrNode->GetnodeName(), (char*)pAttrNode->Gettext());
    }

    // (5)查找节点<Municipality>下的所有子节点
    // "//"表示在任意一层寻找Municipality;"//*"查找<Municipality></Municipality>中的所有子节点
    pNodeList = pDoc->selectNodes((_bstr_t)(char*)"//Municipality//*"); // 这里可将pDoc换成pRootElement

    while (pNode = pNodeList->nextNode())
    {
        printf("childs of [Municipality]: %s\n", (char*)pNode->GetnodeName());
    }

    // (6)查找节点<Municipality>下的一级子节点
    pNode     = pRootElement->selectSingleNode((_bstr_t)(char*)"Municipality");
    pNodeList = pNode->GetchildNodes();
    lChilds   = pNodeList->Getlength();

    for (i = 0; i < lChilds; i++)
    {
        pNode1 = pNodeList->Getitem(i); // pNodeList->item[i]
        printf("child[%d] of [Municipality]: %s\n", i, (char*)pNode1->GetnodeName());
    }
 
    // (7)查询父、子、兄、弟节点
    pNode  = pRootElement->selectSingleNode((_bstr_t)(char*)"//TianJin");
    pNode1 = pNode->GetparentNode(); // 父节点
    printf("[TianJin]的父节点为[%s]\n", (char*)pNode1->GetnodeName());
 
    pNodeList = pNode->GetchildNodes(); // 子节点
    lChilds   = pNodeList->Getlength();

    for (i = 0; i < lChilds; i++)
    {
        pNode1 = pNodeList->nextNode();
        printf("child[%d] of [TianJin]: %s\n", i, (char*)pNode1->GetnodeName());
    }

    pNode1 = pNode->GetpreviousSibling(); // 兄节点
    printf("[TianJin]的兄节点为[%s]\n", (char*)pNode1->GetnodeName());

    pNode1 = pNode->GetnextSibling(); // 弟节点
    printf("[TianJin]的弟节点为[%s]\n", (char*)pNode1->GetnodeName());
 
    return EXIT_SUCCESS;
}

https://www.cnblogs.com/areliang/archive/2011/11/18/2254334.html

https://www.cnblogs.com/areliang/archive/2011/11/23/2260566.html

今天的文章msxml6 C++「建议收藏」分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/70524.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注