目前位置: VCer资源中心 >>> VCer开源 >>> 可重用类

[本帖已阅读4120次 分值100 回复1次] 张贴资源 发回信箱 控制面板

自动生成一个XML解析类

提供者:rone 张贴时间:2004-06-03 11:40:40.0 出处:www.codeproject.com 作者:不祥

自动生成一个XML解析类(2004-06-03 11:40:40.0)


疏影横斜水清浅,暗香


 
级别: VCer小兵
头衔: VCer会员

经验: 200
作品: 1
分会: 华东分会
注册: 2004-05-13 21:22:07.0
登录: 2004-10-26 22:31:51.0
工程源码[96,272字节] 下载366

实现方法

一篇有关自动代码生成和XML的文章,推荐转载

http://www.codeproject.com/tips/XMLParserGen.asp

Introduction

I think your first thought about this article would be that :"oh, another tool to parse XML like MSXML". In fact, this article is based on MSXML. What I will present for you is not a general XML parser ,but a generator to create specific XML parser . The purpose of my article is not to teach you some knowledge about a grammar parsing technique ,but to provide you some idea of auto code generation through an XML parser generator. XML parser may not be of any use in your programming area ,but that does not matter, if you could get a fresh feeling at the end of my article, it will also help in your future exciting programming life.

Background

Faced with plenty of XML files, I have to write plenty of code to retrieve information from them. Even powered by MSXML SDK and XPATH technique , I have to say the work is hard .In fact it will be quite boring and error-prone to write the following code :

IXMLDOMNodePtr psNode = m_pXmlDoc->selectSingleNode(_T("/rss/channel/description"));

psNode = m_pXmlDoc->selectSingleNode(_T("/rss/channel/language"));

...

The sample XML snippet is given below:

<rss version="0.91">

 <channel>

   <description>XML.com features a rich mix of information and services for the XML community.</description>

   <language>en-us</language>

   <item>

     <title>Normalizing XML, Part 2</title>

   </item>

   <item>

     <title>The .NET Schema Object Model</title>

   </item>

 </channel>

</rss>

The problem here is that if I need the XML node's value I have to write down each XPATH to get it. For each kind of XML file there should be a parser, very simple, but quite boring to implement.

The solution - Auto Code Generation

As a programmer, I am both passionate and lazy. I am too lazy to write a single line of the boring code above but I am very passionate to figure out a way to generate these code automatically. Here is my solution :

Write an algorithm to generate XPATH from XML files

Write a template parser

Fill XPATH into the template parser

Since the XML file's schema is often not at hand, there will be some difficulties about how to figure out whether a XML node(e.g. /rss/channel/item) belongs to a structure. Current solution is that if its occurrence is greater than once it will be treated as structure, otherwise a single node.

Since my language is C++ ,in each parser the structure node's values are put into a STL vector , while the single node's value is retrieved by a defined enum type. The program language for the parser is not important ,and you can modify the generator to generate parsers in VB/Java/C# ,whatever you like :-)

Points of Interest

As I said from the beginning, what I present for you is not only a code but the most important, it is an idea(Auto Code Generation) to save your energy and make our programming life easier and more exciting. So if you have the same experience (not limited to XML area) please contact me freely (Albertsjtu@yahoo.com),and it will be my pleasure to offer my advice .As it goes: "You have an apple, I have an apple; if we exchange, then we still have one each. You have an idea, I have an idea; if we exchange, then we will have two each !" 

History

The algorithm to judge whether a node is a structure was revisited to meet such use case:"/items/item", though it may appear in an XML file only once,I think it more like a structure than a single node.

 

注:转载文章需注明来源:VCer.net 文章地址:http://vcer.net/2395.html

  如果你觉得VCer.net不错,而且你愿意为VCer.net捐赠一元钱,那么点击后面的捐赠按钮吧:) vcer.net捐赠

[回复该贴] [加入个人书签]
[投票结果]

A: 评分 10 100% (2 票)
B: 评分 5 0% (0 票)
C: 评分 0 0% (0 票)
D: 评分 -5 0% (0 票)
E: 评分 -10 0% (0 票)

 


re:自动生成一个XML解析类
最好能够直接贴过来,教育网内部出国需要代理,很郁闷的!:)

jerry 于 2004-06-03 15:16:54.0 编辑 [回复该贴]