-
September 21st, 2019, 06:04 AM
#1
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
pdk
-
September 21st, 2019, 07:54 AM
#2
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 21st, 2019, 11:14 AM
#3
Re: Parse an xml
Why waste time hand parsing xml and debugging the code when there readily available parsing libraries?
-
September 21st, 2019, 12:07 PM
#4
Re: Parse an xml
Originally Posted by Arjay
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 21st, 2019, 12:44 PM
#5
Re: Parse an xml
Originally Posted by 2kaud
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.
-
September 22nd, 2019, 04:14 PM
#6
Re: Parse an xml
Victor Nijegorodov
-
September 24th, 2019, 12:13 AM
#7
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;
}
-
September 24th, 2019, 11:26 AM
#8
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.
-
September 26th, 2019, 10:35 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|