Reading an XML Document Using XmlLite

C/C++ Source File (XmlLiteReader.cpp)

//-----------------------------------------------------------------------
// This file is part of the Windows SDK Code Samples.
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation.  See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//-----------------------------------------------------------------------

#include "stdafx.h"
#include <atlbase.h>
#include "xmllite.h"
#include <strsafe.h>

HRESULT WriteAttributes(IXmlReader* pReader)
{
    const WCHAR* pwszPrefix;
    const WCHAR* pwszLocalName;
    const WCHAR* pwszValue;
    HRESULT hr = pReader->MoveToFirstAttribute();

    if (S_FALSE == hr)
        return hr;
    if (S_OK != hr)
    {
        wprintf(L"Error moving to first attribute, error is %08.8lx", hr);
        return -1;
    }
    else
    {
        while (TRUE)
        {
            if (!pReader->IsDefault())
            {
                UINT cwchPrefix;
                if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix)))
                {
                    wprintf(L"Error getting prefix, error is %08.8lx", hr);
                    return -1;
                }
                if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
                {
                    wprintf(L"Error getting local name, error is %08.8lx", hr);
                    return -1;
                }
                if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
                {
                    wprintf(L"Error getting value, error is %08.8lx", hr);
                    return -1;
                }
                if (cwchPrefix > 0)
                    wprintf(L"Attr: %s:%s=\"%s\" \n", pwszPrefix, pwszLocalName, pwszValue);
                else
                    wprintf(L"Attr: %s=\"%s\" \n", pwszLocalName, pwszValue);
            }

            if (S_OK != pReader->MoveToNextAttribute())
                break;
        }
    }
    return hr;
}

int _tmain(int argc, WCHAR* argv[])
{
    HRESULT hr;
    CComPtr<IStream> pFileStream;
    CComPtr<IXmlReader> pReader;
    XmlNodeType nodeType;
    const WCHAR* pwszPrefix;
    const WCHAR* pwszLocalName;
    const WCHAR* pwszValue;
    UINT cwchPrefix;

    if (argc != 2)
    {
        wprintf(L"Usage: XmlLiteReader.exe name-of-input-file\n");
        return 0;
    }

    //Open read-only input stream
    if (FAILED(hr = SHCreateStreamOnFile(argv[1], STGM_READ, &pFileStream)))
    {
        wprintf(L"Error creating file reader, error is %08.8lx", hr);
        return -1;
    }

    if (FAILED(hr = CreateXmlReader(__uuidof(IXmlReader), (void**) &pReader, NULL)))
    {
        wprintf(L"Error creating xml reader, error is %08.8lx", hr);
        return -1;
    }

    if (FAILED(hr = pReader->SetProperty(XmlReaderProperty_DtdProcessing, DtdProcessing_Prohibit)))
    {
        wprintf(L"Error setting XmlReaderProperty_DtdProcessing, error is %08.8lx", hr);
        return -1;
    }

    if (FAILED(hr = pReader->SetInput(pFileStream)))
    {
        wprintf(L"Error setting input for reader, error is %08.8lx", hr);
        return -1;
    }

    //read until there are no more nodes
    while (S_OK == (hr = pReader->Read(&nodeType)))
    {
        switch (nodeType)
        {
        case XmlNodeType_XmlDeclaration:
            wprintf(L"XmlDeclaration\n");
            if (FAILED(hr = WriteAttributes(pReader)))
            {
                wprintf(L"Error writing attributes, error is %08.8lx", hr);
                return -1;
            }
            break;
        case XmlNodeType_Element:
            if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix)))
            {
                wprintf(L"Error getting prefix, error is %08.8lx", hr);
                return -1;
            }
            if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
            {
                wprintf(L"Error getting local name, error is %08.8lx", hr);
                return -1;
            }
            if (cwchPrefix > 0)
                wprintf(L"Element: %s:%s\n", pwszPrefix, pwszLocalName);
            else
                wprintf(L"Element: %s\n", pwszLocalName);

            if (FAILED(hr = WriteAttributes(pReader)))
            {
                wprintf(L"Error writing attributes, error is %08.8lx", hr);
                return -1;
            }

            if (pReader->IsEmptyElement() )
                wprintf(L" (empty)");
            break;
        case XmlNodeType_EndElement:
            if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix)))
            {
                wprintf(L"Error getting prefix, error is %08.8lx", hr);
                return -1;
            }
            if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
            {
                wprintf(L"Error getting local name, error is %08.8lx", hr);
                return -1;
            }
            if (cwchPrefix > 0)
                wprintf(L"End Element: %s:%s\n", pwszPrefix, pwszLocalName);
            else
                wprintf(L"End Element: %s\n", pwszLocalName);
            break;
        case XmlNodeType_Text:
        case XmlNodeType_Whitespace:
            if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
            {
                wprintf(L"Error getting value, error is %08.8lx", hr);
                return -1;
            }
            wprintf(L"Text: >%s<\n", pwszValue);
            break;
        case XmlNodeType_CDATA:
            if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
            {
                wprintf(L"Error getting value, error is %08.8lx", hr);
                return -1;
            }
            wprintf(L"CDATA: %s\n", pwszValue);
            break;
        case XmlNodeType_ProcessingInstruction:
            if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
            {
                wprintf(L"Error getting name, error is %08.8lx", hr);
                return -1;
            }
            if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
            {
                wprintf(L"Error getting value, error is %08.8lx", hr);
                return -1;
            }
            wprintf(L"Processing Instruction name:%S value:%S\n", pwszLocalName, pwszValue);
            break;
        case XmlNodeType_Comment:
            if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
            {
                wprintf(L"Error getting value, error is %08.8lx", hr);
                return -1;
            }
            wprintf(L"Comment: %s\n", pwszValue);
            break;
        case XmlNodeType_DocumentType:
            wprintf(L"DOCTYPE is not printed\n");
            break;
        }
    }

    return 0;
}

XML Source File (stocks.xml)

<?xml version="1.0"?>
<portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <stock exchange="nasdaq">
    <name>new</name>
    <symbol>zzzz</symbol>
    <price>20.313</price>
  </stock>
  <stock exchange="nyse">
    <name>zacx corp</name>
    <symbol>ZCXM</symbol>
    <price>28.875</price>
  </stock>
  <stock exchange="nasdaq">
    <name>zaffymat inc</name>
    <symbol>ZFFX</symbol>
    <price>92.250</price>
  </stock>
  <stock exchange="nasdaq">
    <name>zysmergy inc</name>
    <symbol>ZYSZ</symbol>
    <price>20.313</price>
  </stock>
</portfolio>

Output from XmlLiteReaderXmlDeclaration
Attr: version="1.0"
Text: >
<
Element: portfolio
Attr: xmlns:dt="urn:schemas-microsoft-com:datatypes"
Text: >
  <
Element: stock
Attr: exchange="nasdaq"
Text: >
    <
Element: name
Text: >new<
End Element: name
Text: >
    <
Element: symbol
Text: >zzzz<
End Element: symbol
Text: >
    <
Element: price
Text: >20.313<
End Element: price
Text: >
  <
End Element: stock
Text: >
  <
Element: stock
Attr: exchange="nyse"
Text: >
    <
Element: name
Text: >zacx corp<
End Element: name
Text: >
    <
Element: symbol
Text: >ZCXM<
End Element: symbol
Text: >
    <
Element: price
Text: >28.875<
End Element: price
Text: >
  <
End Element: stock
Text: >
  <
Element: stock
Attr: exchange="nasdaq"
Text: >
    <
Element: name
Text: >zaffymat inc<
End Element: name
Text: >
    <
Element: symbol
Text: >ZFFX<
End Element: symbol
Text: >
    <
Element: price
Text: >92.250<
End Element: price
Text: >
  <
End Element: stock
Text: >
  <
Element: stock
Attr: exchange="nasdaq"
Text: >
    <
Element: name
Text: >zysmergy inc<
End Element: name
Text: >
    <
Element: symbol
Text: >ZYSZ<
End Element: symbol
Text: >
    <
Element: price
Text: >20.313<
End Element: price
Text: >
  <
End Element: stock
Text: >
<
End Element: portfolio

To create the example

  1. Create a Visual Studio 2005 project. For details of how to do this, see Building XmlLite Applications.

  2. Copy the content of Source: XmlLiteReader.cpp into your C++ source file.

  3. Build the example. Building the example will also create the debug directory, which is required for the next step.

  4. Copy the sample XML file to the clipboard, and paste it into your favorite editor to create an XML document. Place the document in the debug directory.

  5. Run the sample and verify that the output matches the output provided.



52 comments:

  1. These are actually fantastic ideas in about blogging.
    You have touched some fastidious points here. Any way keep up wrinting.


    Feel free to surf to my homepage ... Nuvocleanse Diet

    ReplyDelete
  2. Good information. Lucky me I discovered your blog by accident (stumbleupon).

    I have saved it for later!

    Also visit my website Androsolve

    ReplyDelete
  3. I am now not certain the place you're getting your information, however great topic. I needs to spend a while learning more or working out more. Thanks for wonderful information I used to be in search of this information for my mission.

    Here is my web-site; Pure Garcinia Cambogia

    ReplyDelete
  4. Hello! This is kind of off topic but I need some guidance from an
    established blog. Is it difficult to set up your own blog?

    I'm not very techincal but I can figure things out pretty fast. I'm thinking
    about setting up my own but I'm not sure where to begin. Do you have any tips or suggestions? Appreciate it

    Here is my webpage - Elevate GF And 1285 Muscle

    ReplyDelete
  5. This website was... how do I say it? Relevant!
    ! Finally I have found something which helped me.
    Kudos!

    Also visit my web-site: Liposom reviews

    ReplyDelete
  6. First of all I want to say terrific blog! I had a quick
    question that I'd like to ask if you do not mind. I was interested to find out how you center yourself and clear your mind prior to writing. I've had a
    difficult time clearing my thoughts in getting my
    ideas out. I do enjoy writing however it just seems like the first 10
    to 15 minutes are lost simply just trying to figure out how to begin.

    Any suggestions or hints? Cheers!

    Here is my web blog :: Nuvocleanse

    ReplyDelete
  7. Write more, thats all I have to say. Literally, it seems as though you relied on
    the video to make your point. You obviously know what
    youre talking about, why throw away your intelligence on just posting
    videos to your weblog when you could be giving us something informative to read?


    My website ... acai ultra lean

    ReplyDelete
  8. It's very trouble-free to find out any matter on net as compared to books, as I found this article at this website.

    Feel free to surf to my webpage ... Rejuvenex review

    ReplyDelete
  9. Hi there this is somewhat of off topic but I was wondering
    if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I'm starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

    Also visit my website ... Green coffee review

    ReplyDelete
  10. Aw, this was an extremely nice post. Spending some time
    and actual effort to generate a great article… but what can
    I say… I procrastinate a lot and never manage to get nearly anything done.


    Feel free to surf to my blog: Cambogia trim review

    ReplyDelete
  11. always i used to read smaller articles or reviews which also clear their motive,
    and that is also happening with this post which I am reading here.


    Look into my weblog; Order Raspberry Ketone Plus

    ReplyDelete
  12. Its not my first time to go to see this site, i am browsing this web page dailly and obtain good information from here all the time.


    My web site: Pay day loans

    ReplyDelete
  13. you're actually a excellent webmaster. The website loading velocity is amazing. It sort of feels that you are doing any unique trick. In addition, The contents are masterpiece. you've performed a
    wonderful task on this matter!

    My webpage; TruVisage Review

    ReplyDelete
  14. Every weekend i used to visit this web site, for the reason that i wish
    for enjoyment, for the reason that this this web site conations really
    nice funny stuff too.

    Also visit my webpage; Vimax Review

    ReplyDelete
  15. I am in fact thankful to the owner of this web site who has shared
    this wonderful paragraph at here.

    Review my weblog; Slim Helper Weight Loss Patch

    ReplyDelete
  16. Very good article. I will be experiencing some of these issues as well.
    .

    Here is my webpage: Skin cream

    ReplyDelete
  17. Valuable information. Lucky me I found your site by accident, and I am stunned why
    this twist of fate didn't happened earlier! I bookmarked it.

    Visit my web page - uy Le derme luxe

    ReplyDelete
  18. Hey very interesting blog!

    Here is my blog; Swiss rose reviews

    ReplyDelete
  19. Excellent web site you've got here.. It's hard to find high quality writing like yours these days.
    I really appreciate individuals like you! Take care!!

    Also visit my blog; Lean muscle x and test force xtreme

    ReplyDelete
  20. Hey! Do you know if they make any plugins to safeguard
    against hackers? I'm kinda paranoid about losing everything I've worked
    hard on. Any tips?

    Here is my homepage - Pro Collagen Cream

    ReplyDelete
  21. Thank you for the auspicious writeup. It in fact used to be a enjoyment
    account it. Glance complicated to more delivered agreeable from you!
    By the way, how could we keep in touch?

    my page; internet money making

    ReplyDelete
  22. Very great post. I simply stumbled upon your weblog and wished to
    mention that I have really enjoyed surfing around your blog posts.

    In any case I will be subscribing for your feed and I'm hoping you write again soon!

    My web page :: Rvtl Eye creams

    ReplyDelete
  23. Hello everybody, here every one is sharing these experience, thus it's pleasant to read this weblog, and I used to go to see this website daily.

    My web blog: Build muscle now

    ReplyDelete
  24. Great blog you have got here.. It's difficult to find high-quality writing like yours these days. I really appreciate individuals like you! Take care!!

    Have a look at my web page :: Best Wrinkle Creams

    ReplyDelete
  25. My brother recommended I might like this website.
    He was totally right. This post truly made my day. You cann't imagine simply how much time I had spent for this info! Thanks!

    my web-site :: garcinia cambogia

    ReplyDelete
  26. Hello, its nice paragraph concerning media print, we all know media is a impressive source of information.


    my web blog: Fase cream

    ReplyDelete
  27. Hey there! Quick question that's entirely off topic. Do you know how to make your site mobile friendly? My web site looks weird when viewing from my iphone. I'm trying to find a template
    or plugin that might be able to correct this issue.
    If you have any recommendations, please share.
    Many thanks!

    My web-site Diet Patch Trial

    ReplyDelete
  28. I blog frequently and I genuinely appreciate your
    information. The article has really peaked my interest.
    I am going to take a note of your site and keep checking for
    new information about once a week. I subscribed to your
    RSS feed as well.

    Also visit my blog: Collagen

    ReplyDelete
  29. Just wish to say your article is as astonishing. The clarity in
    your post is simply excellent and i can assume you are an expert on this subject.
    Well with your permission allow me to grab your feed to keep up
    to date with forthcoming post. Thanks a million and please carry on the
    enjoyable work.

    Have a look at my weblog Androsolve Testosterone Booster

    ReplyDelete
  30. If you wish for to increase your know-how simply keep visiting this web site and be
    updated with the newest information posted here.

    Feel free to visit my web-site: Slim Helper Weight Loss Patch

    ReplyDelete
  31. Good article! We will be linking to this particularly
    great content on our website. Keep up the great writing.

    Feel free to visit my web site: Buy 1285 muscle

    ReplyDelete
  32. Fastidious answer back in return of this question with genuine arguments and describing
    the whole thing about that.

    Take a look at my web page Buy garciniacambogia

    ReplyDelete
  33. Hey there! Do you use Twitter? I'd like to follow you if that would be ok. I'm definitely enjoying
    your blog and look forward to new posts.

    Feel free to surf to my blog post; how to get money fast

    ReplyDelete
  34. Wow that was unusual. I just wrote an extremely long comment but after
    I clicked submit my comment didn't appear. Grrrr... well I'm not writing all that
    over again. Anyhow, just wanted to say fantastic blog!


    Look at my web blog ... Anatomy X5 Reviews

    ReplyDelete
  35. I was curious if you ever thought of changing the structure
    of your website? Its very well written; I love what youve got to say.

    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or 2 images.
    Maybe you could space it out better?

    my web page; Green coffee

    ReplyDelete
  36. If you want to take a great deal from this post then you have to apply
    these techniques to your won weblog.

    Also visit my page: muscle supplements

    ReplyDelete
  37. This is a topic that's close to my heart... Thank you! Where are your contact details though?

    Here is my web page Ripped XL Supplements

    ReplyDelete
  38. Simply desire to say your article is as astounding.
    The clarity in your post is just excellent and i can assume you are
    an expert on this subject. Fine with your permission allow
    me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please carry on the rewarding work.

    Also visit my web-site; Home Staging Minnesota

    ReplyDelete
  39. Thanks for some other informative site. The place else may I get that kind of information written in such an
    ideal method? I have a project that I'm simply now running on, and I have been at the glance out for such info.

    my homepage: puregarciniacambogianow.com

    ReplyDelete
  40. Admiring the persistence you put into your website and detailed information
    you provide. It's good to come across a blog every once in a while that isn't the same old rehashed material.
    Wonderful read! I've saved your site and I'm adding your RSS feeds to
    my Google account.

    my web page: Ripped Muscle X treme

    ReplyDelete
  41. I have read so many content about the blogger lovers but this article is really a good post, keep it up.



    Corporate Movers

    ReplyDelete
  42. hello!,I really like your writing very so much! proportion we keep in touch more about your post on AOL?
    I require an expert in this area to resolve my problem. Maybe
    that's you! Looking forward to see you.

    My web blog - weight loss

    ReplyDelete
  43. This post is actually a pleasant one it assists new the web viewers, who are wishing for blogging.


    Also visit my blog ... Androsolve Muscle Builder

    ReplyDelete
  44. Hi there everyone, it's my first pay a quick visit at this web site, and article is actually fruitful for me, keep up posting these types of articles or reviews.

    buy testostrong

    ReplyDelete
  45. Wow that was odd. I just wrote an incredibly long comment but
    after I clicked submit my comment didn't show up. Grrrr... well I'm not writing
    all that over again. Anyways, just wanted to
    say superb blog!

    my site; Green coffee

    ReplyDelete
  46. Terrific work! This is the kind of information that are supposed to be shared around the internet.
    Disgrace on the seek engines for now not positioning
    this put up upper! Come on over and consult with my web site
    . Thank you =)

    profit master

    ReplyDelete
  47. Hi, after reading this amazing article i am also
    glad to share my knowledge here with mates.

    muscle building

    ReplyDelete
  48. Its like you read my mind! You appear to grasp so much about this, like you wrote the e-book in it or something.
    I think that you simply can do with some p.c. to pressure
    the message house a little bit, however instead of that,
    this is excellent blog. An excellent read.
    I will definitely be back.


    buy Ultra Force

    ReplyDelete
  49. Very good article! We are linking to this particularly great article on our website.
    Keep up the good writing.

    Also visit my page: Cambogia trim review

    ReplyDelete
  50. You could definitely see your skills within the work you write.
    The sector hopes for even more passionate writers such as you who aren't afraid to mention how they believe. At all times follow your heart.

    My blog - green coffee bean

    ReplyDelete
  51. I've been browsing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the web will be a lot more useful than ever before.

    my web-site :: Author's external home page.
    ..

    ReplyDelete
  52. Whats up very cool site!! Guy .. Excellent .. Wonderful ..
    I will bookmark your site and take the feeds additionally?
    I'm glad to find a lot of helpful information right here within the publish, we need work out extra strategies in this regard, thanks for sharing. . . . . .

    Here is my homepage ... lean muscle

    ReplyDelete