zoukankan      html  css  js  c++  java
  • vc 解析XML笔记

    // ConsoleReadXml.cpp : Defines the entry point for the console application.
    //

    #include 
    "stdafx.h"
    #import 
    "msxml4.dll"
    #include 
    "stdlib.h"
    #include 
    <iostream>
    #include 
    "string"
    #include  
    "atlbase.h" 
    #include 
    "atlstr.h"
    #include 
    "comutil.h"
    using namespace std;
    using namespace MSXML2;

    int _tmain(int argc, _TCHAR* argv[])
    {
        CoInitialize(NULL);
        
    char temp[500];
        MSXML2::IXMLDOMDocumentPtr pDoc;
        HRESULT hr;
        hr
    =pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
        
    if(FAILED(hr))
        {  
            cout
    <<"msxml4.dll can't find "<<endl;
            
    return 1;
        }
        
    string strXml="<?xml version=\"1.0\" encoding=\"utf-8\"?> <Users><User><UserName>Test1</UserName><PassWord>MyPassWord</PassWord></User><User><UserName>Test1</UserName><PassWord>MyPassWord</PassWord></User></Users>";

        
        VARIANT_BOOL b
    =pDoc->loadXML((_bstr_t)strXml.c_str());

        
    //根节点
        MSXML2::IXMLDOMElementPtr spElement;

        pDoc
    ->get_documentElement(&spElement);

        
        CComBSTR varNodeText;
        CComBSTR varNodeTypeName;
        
    string temp2;

        MSXML2::IXMLDOMNodeListPtr spUserNodeList;

        
    //获取第一层节点值。Users标签下值
        spElement->get_childNodes(&spUserNodeList);
        spElement
    ->get_nodeName(&varNodeText);
        CW2A str(varNodeText);
        cout
    <<str<<endl;
        
    //temp=varNodeText;
        
    //cout>>temp.c_str()<<endl;

        
    //获取节点数
        long luserCount;
        spUserNodeList
    ->get_length(&luserCount);

        
    //遍历所有节点
        for (int i=0;i<luserCount;i++)
        {
            MSXML2::IXMLDOMNodePtr spUserChld;
            MSXML2::IXMLDOMNodeListPtr spUserChldList;
            
            
    //获取子节点
            spUserNodeList->get_item(i,&spUserChld);
            spUserChld
    ->get_nodeName(&varNodeTypeName);
            CW2A strtemp(varNodeTypeName);
            cout
    <<strtemp<<endl;
            
    //temp=varNodeTypeName;
            
    //cout<<temp<<endl;

            
    //根据子节点再获取子节点信息
            spUserChld->get_childNodes(&spUserChldList);
            
    long luserChldcnt;
            spUserChldList
    ->get_length(&luserChldcnt);
            
    for (int j=0;j<luserChldcnt;j++)
            {
                MSXML2::IXMLDOMNodePtr endNode;
                spUserChldList
    ->get_item(j,&endNode);
                endNode
    ->get_nodeName(&varNodeTypeName);
                
    //temp=varNodeTypeName;
                
    //cout<<temp<<endl;
                
                CW2A printstr(varNodeTypeName);
                cout
    <<printstr<<"--";
                
                endNode
    ->get_text(&varNodeText);

                CW2A printstr2(varNodeText);
                cout
    <<printstr2<<endl;
                endNode.Release();

            }
            spUserChldList.Release();
            spUserChld.Release();
            
        }
        spUserNodeList.Release();
        spElement.Release();
        pDoc.Release();

        
        CoUninitialize();
        
    return 0;
    }


    程序输出图
  • 相关阅读:
    注册机实现算法
    nginx优化配置服务,快速提升服务器性能,突破100000万高并发不是梦!
    语雀、有道云笔记、滴答清单一键保存文章,剪藏文章chrom、360极速浏览器火狐插件
    超简单!查询自己在CSDN、博客园、简书、知乎、51CTO等平台的百度收录情况
    最全的中文Vscode扩展插件&配置调试&快捷键&格式化方法
    中文汉化atom最美前端开发编辑器插件安装和常用配置&快捷键,5年的使用心得
    加速Github访问,最新地址仅需1步!修改host快速达到2M!
    使用标签link rel="alternate"属性,帮助搜索引擎对不同设备的用户提供不同类型的页面
    JavaScript高级程序设计学习笔记(1)
    idea中多模块项目引入
  • 原文地址:https://www.cnblogs.com/likwo/p/1576802.html
Copyright © 2011-2022 走看看