如何使用readfile从CDATA XML中检索值? [英] How to retrive value from CDATA XML using readfile?

查看:104
本文介绍了如何使用readfile从CDATA XML中检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从XML中检索CDATA格式的数据。但是我使用ReadFile函数读取XML(这个函数是读取文件必需的)我不能使用任何其他选项来读取XML(没有DOM和SAX)。它要求仅使用READFile读取xml。

一旦读取并以字符串形式获取完整的xml并获取标签的值,其值为CDATA。



以下是XML:



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:

<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>





输出:

用XML计算孩子的数量?

标签内的CDATA值。



< b>我尝试了什么:





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

What I have tried:

// 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;
		

	}

推荐答案

如果仅使用 ReadFile 是你的要求然后你必须自己解析检索到的 XML 字符串(例如,使用 string :: find [ ^ ]方法)。
If using just ReadFile is you requirement then you have to parse yourself the retrieved XML strings (using, for instance, the string::find[^] method).


停止使用ReadFile浪费时间并使用简单的解析器,例如XmlLite [ ^ ]。
Stop wasting time with ReadFile and use a simple parser such as XmlLite[^].


这篇关于如何使用readfile从CDATA XML中检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆