CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: XML Parsing

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    XML Parsing

    In VC++ is there any inbuilt class for storying values inside an XML File, without using CLI?

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: XML Parsing

    One option is to use XmlLite which is quite easy. (http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx).
    See also XmlLite Samples.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: XML Parsing

    Quote Originally Posted by maverick786us View Post
    In VC++ is there any inbuilt class for storying values inside an XML File, without using CLI?
    No, any inbuilt one does not exist. You have to use some library to deal with XML. (In fact this is the way C/C++ deal with anything else.)

    Full-fledged lib from MS: MSXML
    Best regards,
    Igor

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: XML Parsing

    Probably, the word "inbuilt" used here is not very clear for me.
    So let me try do detail a little bit by quoting from MSDN:
    • The XmlLite runtime file, Xmllite.dll, is integrated into the following operating systems and products:
      • Windows 8
      • Windows Server 2008 R2
      • Windows 7.
      • Windows Server 2008
      • Windows Vista
      • Windows XP with Service Pack 3 or later.
      • Windows Server 2003 with Service Pack 2 or later.
      • Microsoft Internet Explorer 7 and later.
    • To build applications with XmlLite, you must install the XmlLite development files, Xmllite.lib and Xmllite.h.
      [...]they are only provided in the Windows SDK for Vista.
      [...]You do not have to install the entire Windows SDK to get Xmllite.lib and Xmllite.h. To get Xmllite.lib and Xmllite.h...

    All details can be found here: http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    Indeed MSXML parser may be a better option, but can scare the C++ programmers which don't know too much about using COM components.
    Dealing with Xmllite is easier and good enough in many cases.
    Last edited by ovidiucucu; October 9th, 2014 at 08:15 AM. Reason: typo
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: XML Parsing

    there are other libraries as well.

    There's a basic xml lib in boost

    if your expected xml is simple enough, you can even write your own parser in a couple hundred lines of code (been there, done that), but things can quite quickly get difficult if you need to deal with namespaces, different character encoding, DTD's, Entities, if you need to worry about formatting, or need to handle special features like CData, and/or if you need to care about valid, yet awkward/unusual xml.

    the thing you'd need to worry about first is if you want a SAX/Streaming approach, or a DOM/load everything type approach.
    with SAX, the xml is read and your app is called into via a callback mechanism each time a tag is found, the xml is never loaded entirely, and you only see each tag once.
    with DOM, the xml is loaded into memory, and you can then do queries (usually XPath, but some libs have their own built in method/script) to extract the data you need.

    Both approaches have benefits and disadvantages.

  6. #6
    Join Date
    May 2002
    Posts
    1,798

    Re: XML Parsing

    I have experimented with a number of XML parsers and have become sold on XmlLite.

    Pretty simple to use.

    Code:
    #include <xmllite.h>
    #pragma comment (lib, "xmllite.lib")
    
    class MyXmlParser
    {
    //
        IStream *pFileStream;
        IXmlReader *pReader;
        XmlNodeType nodeType;
         IStream *pOutFileStream;
        IXmlWriter *pWriter;
    //
    
    };
    The examples cited above are very good.
    mpliam

  7. #7
    Join Date
    Apr 2005
    Posts
    1,828

    Re: XML Parsing

    Quote Originally Posted by Mike Pliam View Post
    I have experimented with a number of XML parsers and have become sold on XmlLite.

    Pretty simple to use.

    Code:
    #include <xmllite.h>
    #pragma comment (lib, "xmllite.lib")
    
    class MyXmlParser
    {
    //
        IStream *pFileStream;
        IXmlReader *pReader;
        XmlNodeType nodeType;
         IStream *pOutFileStream;
        IXmlWriter *pWriter;
    //
    
    };
    The examples cited above are very good.
    Is this the best? What I am looking for is a light weight class that can be easily exported in a Win 32 DLL

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: XML Parsing

    Quote Originally Posted by maverick786us View Post
    Is this the best? What I am looking for is a light weight class that can be easily exported in a Win 32 DLL
    Who knows what could be "the best" for you?
    However, it is pretty easy to use. Just try the XmlLite Samples:
    Read an XML Document using XmlLite
    Write an XML Document using XmlLite
    Victor Nijegorodov

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: XML Parsing

    Quote Originally Posted by maverick786us View Post
    Is this the best? What I am looking for is a light weight class that can be easily exported in a Win 32 DLL
    I wander Are You Really Serious? Best? Lightweight? Easy? YES! MSXML is really the best, ligtweight and easy to use. For me. And I don't care if it does the same for you. Does this fact matter anyhow for YOU? Please stop bogging us with irrelevant questions of the kind. Ans let's get technical, for the sake of all of us.
    Best regards,
    Igor

  10. #10
    Join Date
    Apr 2005
    Posts
    1,828

    Re: XML Parsing

    Quote Originally Posted by Igor Vartanov View Post
    I wander Are You Really Serious? Best? Lightweight? Easy? YES! MSXML is really the best, ligtweight and easy to use. For me. And I don't care if it does the same for you. Does this fact matter anyhow for YOU? Please stop bogging us with irrelevant questions of the kind. Ans let's get technical, for the sake of all of us.
    I mean there are so many 3rd party SDKs and classes for XML parsing. By light weight I mean that it should have minimal dependency DLLs and classes, when I deploy it on my client's PC. I agree that the solutions that you have provided are all excellent examples

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: XML Parsing

    Quote Originally Posted by maverick786us View Post
    I mean there are so many 3rd party SDKs and classes for XML parsing. By light weight I mean that it should have minimal dependency DLLs and classes, when I deploy it on my client's PC.
    Then I'd use one of the MSFT product.
    Victor Nijegorodov

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: XML Parsing

    Did you try the Microsoft sample code for writing?
    https://code.msdn.microsoft.com/XmlL...hId=1720047283
    There is a code and there is a XML-file example.
    Victor Nijegorodov

  13. #13
    Join Date
    Apr 2005
    Posts
    1,828

    Re: XML Parsing

    Quote Originally Posted by VictorN View Post
    Did you try the Microsoft sample code for writing?
    https://code.msdn.microsoft.com/XmlL...hId=1720047283
    There is a code and there is a XML-file example.
    I used this sample code. But It didn't write anything into this sample XML File that I created.

    Code:
    #include <ole2.h>
    #include <xmllite.h>
    #include <stdio.h>
    #include <shlwapi.h>
    
    #pragma warning(disable : 4127)  // conditional expression is constant
    #define CHKHR(stmt)             do { hr = (stmt); if (FAILED(hr)) goto CleanUp; } while(0)
    #define HR(stmt)                do { hr = (stmt); goto CleanUp; } while(0)
    #define SAFE_RELEASE(I)         do { if (I){ I->Release(); } I = NULL; } while(0)
    
    int __cdecl wmain(int argc, _In_reads_(argc) WCHAR* argv[])
    {
        HRESULT hr = S_OK;
        IStream *pOutFileStream = NULL;
        IXmlWriter *pWriter = NULL;
    
        if (argc != 2)
        {
            wprintf(L"Usage: XmlLiteWriter.exe name-of-output-file\n");
            return 0;
        }
    
        //Open writeable output stream
        if (FAILED(hr = SHCreateStreamOnFile(argv[1], STGM_CREATE | STGM_WRITE, &pOutFileStream)))
        {
            wprintf(L"Error creating file writer, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = CreateXmlWriter(__uuidof(IXmlWriter), (void**) &pWriter, NULL)))
        {
            wprintf(L"Error creating xml writer, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->SetOutput(pOutFileStream)))
        {
            wprintf(L"Error, Method: SetOutput, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->SetProperty(XmlWriterProperty_Indent, TRUE)))
        {
            wprintf(L"Error, Method: SetProperty XmlWriterProperty_Indent, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteStartDocument(XmlStandalone_Omit)))
        {
            wprintf(L"Error, Method: WriteStartDocument, error is %08.8lx", hr);
            HR(hr);
        }
    
        // if you want to use a DTD using either the SYSTEM or PUBLIC identifiers,
        // or if you want to use an internal DTD subset, you can modify the following
        // call to WriteDocType.
        if (FAILED(hr = pWriter->WriteDocType(L"root", NULL, NULL, NULL)))
        {
            wprintf(L"Error, Method: WriteDocType, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteProcessingInstruction(L"xml-stylesheet",
            L"href=\"mystyle.css\" title=\"Compact\" type=\"text/css\"")))
        {
            wprintf(L"Error, Method: WriteProcessingInstruction, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteStartElement(NULL, L"root", NULL)))
        {
            wprintf(L"Error, Method: WriteStartElement, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteStartElement(NULL, L"sub", NULL)))
        {
            wprintf(L"Error, Method: WriteStartElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteAttributeString(NULL, L"myAttr", NULL,
                                                      L"1234")))
        {
            wprintf(L"Error, Method: WriteAttributeString, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteString(
                   L"Markup is <escaped> for this string")))
        {
            wprintf(L"Error, Method: WriteString, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteFullEndElement()))
        {
            wprintf(L"Error, Method: WriteFullEndElement, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteStartElement(NULL, L"anotherChild", NULL)))
        {
            wprintf(L"Error, Method: WriteStartElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteString(L"some text")))
        {
            wprintf(L"Error, Method: WriteString, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteFullEndElement()))
        {
            wprintf(L"Error, Method: WriteFullEndElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteCData(L"This is CDATA text.")))
        {
            wprintf(L"Error, Method: WriteCData, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteStartElement(NULL, L"containsCharacterEntity", NULL)))
        {
            wprintf(L"Error, Method: WriteStartElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteCharEntity(L'a')))
        {
            wprintf(L"Error, Method: WriteCharEntity, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteFullEndElement()))
        {
            wprintf(L"Error, Method: WriteFullEndElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteStartElement(NULL, L"containsChars", NULL)))
        {
            wprintf(L"Error, Method: WriteStartElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteChars(L"abcdefghijklm", 5)))
        {
            wprintf(L"Error, Method: WriteChars, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteFullEndElement()))
        {
            wprintf(L"Error, Method: WriteFullEndElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteStartElement(NULL, L"containsEntity", NULL)))
        {
            wprintf(L"Error, Method: WriteStartElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteEntityRef(L"myEntity")))
        {
            wprintf(L"Error, Method: WriteEntityRef, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteEndElement()))
        {
            wprintf(L"Error, Method: WriteEndElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteStartElement(NULL, L"containsName", NULL)))
        {
            wprintf(L"Error, Method: WriteStartElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteName(L"myName")))
        {
            wprintf(L"Error, Method: WriteName, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteEndElement()))
        {
            wprintf(L"Error, Method: WriteEndElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteStartElement(NULL, L"containsNmToken", NULL)))
        {
            wprintf(L"Error, Method: WriteStartElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteNmToken(L"myNmToken")))
        {
            wprintf(L"Error, Method: WriteNmToken, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteEndElement()))
        {
            wprintf(L"Error, Method: WriteEndElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteComment(L"This is a comment")))
        {
            wprintf(L"Error, Method: WriteComment, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteRaw(L"<elementWrittenRaw/>")))
        {
            wprintf(L"Error, Method: WriteRaw, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteRawChars(L"<rawCharacters/>", 16)))
        {
            wprintf(L"Error, Method: WriteRawChars, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        if (FAILED(hr = pWriter->WriteElementString(NULL, L"myElement", NULL, L"myValue")))
        {
            wprintf(L"Error, Method: WriteElementString, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteFullEndElement()))
        {
            wprintf(L"Error, Method: WriteFullEndElement, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->WriteWhitespace(L"\n")))
        {
            wprintf(L"Error, Method: WriteWhitespace, error is %08.8lx", hr);
            HR(hr);
        }
    
        // WriteEndDocument closes any open elements or attributes
        if (FAILED(hr = pWriter->WriteEndDocument()))
        {
            wprintf(L"Error, Method: WriteEndDocument, error is %08.8lx", hr);
            HR(hr);
        }
        if (FAILED(hr = pWriter->Flush()))
        {
            wprintf(L"Error, Method: Flush, error is %08.8lx", hr);
            HR(hr);
        }
    
    CleanUp:
        SAFE_RELEASE(pOutFileStream);
        SAFE_RELEASE(pWriter);
        return hr;
    }
    These are the contents of the XML File that I created.

    Code:
    <!-- Edited by XMLSpy -->
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Encryption Information</heading>
    <body>CryptoColor enable (Y / N)</body>
    <body>CryptoColor (HEX color code)</body>
    <body>Display Frames = Yes</body>
    <body>Highlighted spam sites = No</body>
    <body>Context Sensitive warning = No</body>
    <body>HW ID = XXXXX</body>
    <body>Product Serial Number = EEFSE-WEXXE-WVRX-FRDXD</body>
    <body>Operating System Version = Windows 8.1</body>
    </note>
    Last edited by maverick786us; November 17th, 2014 at 05:50 AM.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,725

    Re: XML Parsing

    Quote Originally Posted by maverick786us View Post
    I used this sample code. But It didn't write anything into this sample XML File that I created.
    Did you debug this code to be sure there is no any error?
    Victor Nijegorodov

  15. #15
    Join Date
    Apr 2005
    Posts
    1,828

    Re: XML Parsing

    Quote Originally Posted by VictorN View Post
    Did you debug this code to be sure there is no any error?
    There was no error. It went through each and every section. But I didn't see any change in the XML file

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)