dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Parse an xml

  1. #1
    Join Date
    May 2015
    Posts
    213

    Parse an xml

    Hi,

    I want to parse and get the list of following parameter.

    Can i just use c++ or do i need to use free tools like DOM, tinyXML etc. Please kindly suggest.

    thanks
    pdkName:  Capture.jpg
Views: 161
Size:  15.0 KBName:  Capture.jpg
Views: 161
Size:  15.0 KBName:  Capture.jpg
Views: 161
Size:  15.0 KBName:  Capture.jpg
Views: 161
Size:  15.0 KB

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,900

    Re: Parse an xml

    You can parse it direct using standard c++ - as XML is just text. However, it would probably be easier to use something like tinyXML-2 or other simple XML parser.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: Parse an xml

    Why waste time hand parsing xml and debugging the code when there readily available parsing libraries?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,900

    Re: Parse an xml

    Quote Originally Posted by Arjay View Post
    Why waste time hand parsing xml and debugging the code when there readily available parsing libraries?
    Only as an exercise!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: Parse an xml

    Quote Originally Posted by 2kaud View Post
    Only as an exercise!
    IMO, a better exercise would be to learn how to leverage what's available. In programming, there is so much to learn there is little time to reinvent the wheel.

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

    Re: Parse an xml

    Quote Originally Posted by Arjay View Post
    IMO, a better exercise would be to learn how to leverage what's available. In programming, there is so much to learn there is little time to reinvent the wheel.
    Victor Nijegorodov

  7. #7
    Join Date
    May 2015
    Posts
    213

    Re: Parse an xml

    Thanks a lot Kaud, Arjay and Victor for comments
    I am told to use the COM utility to do this task
    So i was googling and trying to learn and wrote the following code. But it fails at transform.

    I am using the visual studio 15. Any help is very much appreciated and it is very urgent (I already wasted yesterday trying debug myself but no progress)

    It is failing at transformNodeToObject step
    Code:
    #include "stdafx.h"
    #include <windows.h>
    #include "iostream"
    
    #include <WTypes.h>
    #include <comdef.h>
    
    #include <wchar.h>
    
    #include <vector>
    
    #import <msxml6.dll>
    
    using namespace MSXML2;
    using namespace std;
    int main()
    {
    
    	HRESULT hResult = S_OK;
    	hResult = CoInitialize(NULL);
    
    	if (FAILED(hResult))
    	{
    		cerr << "Failed to initialize COM environment" << endl;
    		return 0;
    	}
    
    	// MSXML COM smart pointers
        // Use the Document2 class to enable schema validation
        IXMLDOMDocument2Ptr spDocSource;
    	IXMLDOMDocument2Ptr spDocResult;
    	IXMLDOMDocument2Ptr spDocStylesheet;
    	struct IDispatch * pDispatch;
    
    	// Create the COM DOM Document objects
    	hResult = spDocSource.CreateInstance(__uuidof(DOMDocument60));
    	if FAILED(hResult)
    	{
    		cerr << "Failed to create Source Document instance" << endl;
    		return 1;
    	}
    
    	hResult = spDocResult.CreateInstance(__uuidof(DOMDocument60));
    	if FAILED(hResult)
    	{
    		cerr << "Failed to create Result Document instance" << endl;
    		return 1;
    	}
    
    	hResult = spDocStylesheet.CreateInstance(__uuidof(DOMDocument60));
    	if FAILED(hResult)
        {
    		cerr << "Failed to create Stylesheet Document instance" << endl;
    		return 1;
    	}
    
    	// Load the source document
        spDocSource->async = VARIANT_FALSE;
    	hResult = spDocSource->load("xmlinputfile.xml");
    	if (hResult != VARIANT_TRUE)
    	{
    		cout << "Error parsing xmlinputfile.xml" << endl;
    		return 1;
    	}
    
    	spDocSource->async = VARIANT_FALSE;
    	hResult = spDocSource->load("XSLTFile1.xslt");
    	if (hResult != VARIANT_TRUE)
    	{
    		cout << "Error parsing XSLTFile1.xml" << endl;
    		return 1;
    	}
    
    	spDocResult->QueryInterface(IID_IDispatch, (void **)&pDispatch);
    	VARIANT vResultDoc;
    	vResultDoc.vt = VT_DISPATCH;
    	vResultDoc.pdispVal = pDispatch;
    
    	hResult = spDocSource->transformNodeToObject(spDocStylesheet, vResultDoc);
    
    
    	if FAILED(hResult)
    	{
    		cout << "Error in performing transformation" << endl;
    		return 1;
    	}

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: Parse an xml

    You don't need to transform anything if you are only trying to read xml. Have your requirements changed from your original post? If not, google for some COM code that shows you how to read the elements.

  9. #9
    Join Date
    May 2015
    Posts
    213

    Re: Parse an xml

    This issue is resolved now. My updated code is as below. Thanks a lot for the discussions..
    Code:
    	HRESULT hr = CoInitialize(NULL);
    
    	MSXML2::IXMLDOMDocumentPtr pXMLDom;
    	MSXML2::IXMLDOMDocumentPtr pXSLDoc;
    	MSXML2::IXMLDOMDocumentPtr pXMLOut;
    
    	// Load the XML file. 
    	pXMLDom.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER);
    
    
    	pXMLDom->async = VARIANT_FALSE; // The default is true. 
    	pXMLDom->validateOnParse = VARIANT_FALSE;
    	pXMLDom->resolveExternals = VARIANT_FALSE;
    
    	pXMLDom->load(L"xmlinputfile.xml");
    
    
    	// Load the XSLT style sheet. 
    	FAILED(pXSLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER));
    
    	pXSLDoc->async = VARIANT_FALSE; // The default is true. 
    	pXSLDoc->validateOnParse = VARIANT_FALSE;
    	pXSLDoc->resolveExternals = VARIANT_FALSE;
    
    	pXSLDoc->load(L"XSLTFile1.xslt");
    
    	// Transform the XSLT to an XML string. 
    	_bstr_t xmlStr = pXMLDom->transformNode(pXSLDoc);

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)