Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to retrive value from XML which data in CDATA form.But I read XML using ReadFile function(this function is mandatory to read file)I cant use any other option to read XML(no DOM and SAX).Its requirement to read xml using READFile only.
once its read and get full xml in form of string and get value of tag which values are in CDATA.

Below is the XML:

HTML
<namespace>
	<ns1>
		<ns1name><![CDATA[MyData1</ns1name>
		<class>
			<cs1>
				<cs1name><![CDATA[ComputerSystem</cs1name>
				<property><![CDATA[DomainName@@SystemInfo@@IPAddress@MacAddress
				</property>
			</cs1>
		</class>
	</ns1>
	<ns2>
	<ns2name><![CDATA[MyData2</ns2name>
		<class>
			<cs1>
				<cs1name><![CDATA[OtherDetails</cs1name>
				<property><![CDATA[Caption@#@Description@#@ElementName@#@InstallDate</property>
			</cs1>
		</class>
	</ns2>
</namespace>


output:
Count no of child in XML??
CDATA value inside tags.

What I have tried:

C++
// XMLSample.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include<string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	wchar_t AppPath[MAX_PATH] = { 0 };
	wchar_t XMLfilePath[MAX_PATH] = { 0 };
	wchar_t *pwszFindChar = NULL;
	//stDBStart	structDBStart = { 0 };

	GetModuleFileName(NULL,AppPath, MAX_PATH);

	pwszFindChar = wcsrchr(AppPath, L'\\');
	if (NULL == pwszFindChar)
	{
		return 0;
	}
	*pwszFindChar = NULL;

	lstrcpyW(XMLfilePath, AppPath);
	lstrcatW(XMLfilePath,L"\\HyperV.xml");

	DWORD dwIniLen = 0;

	dwIniLen = ::GetCompressedFileSizeW(XMLfilePath, 0);
	dwIniLen *= 2;

	
	HANDLE		hFile = NULL;
	
	DWORD  dwFileSize=0;
	DWORD	dwReadBuffer = 0;
	wchar_t* pszBuffer = { 0 };
	
	hFile = CreateFileW(XMLfilePath,
		GENERIC_READ ,
		FILE_SHARE_READ,
		NULL,
		OPEN_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL);

	dwIniLen = ::GetCompressedFileSize(XMLfilePath, 0);
	dwIniLen *= 2;

	//wchar_t* buffer = new wchar_t[dwIniLen];

	pszBuffer = new wchar_t[dwIniLen];
	//wchar_t pszBuffer[dwFileSize] = { '\0' };

	wmemset(pszBuffer, NULL, (dwIniLen));
	::ReadFile(hFile, pszBuffer, dwIniLen, &dwReadBuffer, 0);

	wstring ws(pszBuffer);
	// your new String
	string xmlContent(ws.begin(), ws.end());
	
	/*if (xmlContent.find())*/

	if (0 == dwReadBuffer)
	{
		CloseHandle(hFile);
	}
	else
	{
		
	}



			CloseHandle(hFile);

			return 0;
		

	}
Posted
Updated 8-Aug-16 22:07pm
v2

If using just ReadFile is you requirement then you have to parse yourself the retrieved XML strings (using, for instance, the string::find[^] method).
 
Share this answer
 
Comments
nishac189 9-Aug-16 2:31am    
okk Thanx
nishac189 9-Aug-16 2:32am    
How can I Count number of nodes inside XML
Stop wasting time with ReadFile and use a simple parser such as XmlLite[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900