使用XERCES库的C ++代码读取XML文件 [英] C++ code to read an XML file, using the XERCES library

查看:147
本文介绍了使用XERCES库的C ++代码读取XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个C ++代码来解析XML文件并从中提取数据.

但是,我绝对是个初学者,所以我只搜索了Google.
大多数人似乎都引用"Xerces"库来完成这项工作,所以我继续进行下去.

但是,Xerces库的在线文档有点依赖示例和示例程序.
我在Code项目中找到了一篇有关使用Xerces库编写XML文件"的文章,但没有一篇文章可以读取XML文件并从中提取数据.

如果有人可以给我一个小的示例代码来读取和提取XML文件中的数据(使用Xerces库),或者如果他们可以将我定向到一个站点/文章中提供了相同的小教程,那么我将不胜感激.

I want to write a C++ code to parse an XML file and extract data from it.

However, I''m an absolute beginner so I just searched Google.
Most people seem to refer an "Xerces" library to do this job, so i went ahead with it.

However, the online documentation of the Xerces library is a little lean on examples and sample programs.
I found an article in Code project on "Writing an XML file using the Xerces library", but none to read from an XML file and extract data from it.

If someone can give me a small sample code to read and extract data from an XML file (using the Xerces library), or if they can direct me to a site/article that gives a small tutorial on the same, I would be really grateful.

推荐答案

我还试图使用XERCES来解析XML文件.从代码调试中我了解的是,必须使用DefaultHandler接口来解析XML文件.必须实现其以下功能才能访问各个元素和属性:

I am also trying to use XERCES to parse XML files. What I understand from my code debugging is , one has to use the DefaultHandler interface for parsing the XML files. Its following functions must be implemented to have access to individual elements and attributes:

startDocument(); //indates the document start
void startElement() //indiacates the element start, here you can have all attributes to a particular element and iterate to access them
void endDocument();
void endElement() //indiacates that particular element ends

////sample Code
void SVGHandler::startElement(const   XMLCh* const    uri,
									 const   XMLCh* const    localname,
									 const   XMLCh* const    qname,
									 const   Attributes&		attributes)
{
	// The name has to be representable without any escapes
	/*fFormatter  << XMLFormatter::NoEscapes << chOpenAngle ;
	if ( fExpandNS )
	{
		if (XMLString::compareIString(uri,XMLUni::fgZeroLenString) != 0)
			fFormatter  << uri << chColon;
		fFormatter << localname ;
	}
	else
		fFormatter << qname ;*/

	XMLSize_t len = attributes.getLength();
	for (XMLSize_t index = 0; index < len; index++)
	{
		//
		//  Again the name has to be completely representable. But the
		//  attribute can have refs and requires the attribute style
		//  escaping.
		//
		
		const XMLCh * attName , * nodeValue;
		const XMLCh * nodeName;

		nodeName = qname;
		if(wcscmp(nodeName , L"svg")==0)
		{
			attName = attributes.getQName(index);
			if(wcscmp(attName , L"height")==0)
				svgProp.height = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"width")==0)
					svgProp.width = _wtol(attributes.getValue(index));
		}
		else if(wcscmp(nodeName , L"g")==0)
		{
			attName = attributes.getQName(index);
			if(wcscmp(attName , L"style")==0)
				groupProp.gStyle.stroke = _wtol(attributes.getValue(index));
		}
		else if(wcscmp(nodeName , L"rect")==0)
		{
			attName = attributes.getQName(index);
			if(wcscmp(attName , L"height")==0)
				rectObj.height = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"style")==0)
				rectObj.recStyle.stroke = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"width")==0)
				rectObj.width = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"x")==0)
				rectObj.xPos = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"y")==0)
				rectObj.yPos = _wtol(attributes.getValue(index));
		}
		else if(wcscmp(nodeName , L"text")==0)
		{
			attName = attributes.getQName(index);
			 if(wcscmp(attName , L"x")==0)
				rectObj.xPos = _wtol(attributes.getValue(index));
			else if(wcscmp(attName , L"y")==0)
				rectObj.yPos = _wtol(attributes.getValue(index)); 
		}



	}
	
}


您可能已经有了合适的示例,请仔细阅读此页面 [^ ].特别是 DOMPrint示例 [
You probably already have the appropriate example, please read carefully this page[^]. Specifically the DOMPrint sample[^] looks promising.


这篇关于使用XERCES库的C ++代码读取XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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