<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhoneGeek 爱疯极客 &#187; XML解析器</title>
	<atom:link href="http://www.iphone-geek.cn/tag/xml%e8%a7%a3%e6%9e%90%e5%99%a8/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iphone-geek.cn</link>
	<description>iPhone 新闻，编程，技巧与提示，代码，教程</description>
	<lastBuildDate>Sun, 25 Jul 2010 13:49:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>跨平台代码分享之三 – XML解析器</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%b8%89-%e2%80%93-xml%e8%a7%a3%e6%9e%90%e5%99%a8</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%b8%89-%e2%80%93-xml%e8%a7%a3%e6%9e%90%e5%99%a8#comments</comments>
		<pubDate>Wed, 25 Nov 2009 03:21:57 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[跨平台]]></category>
		<category><![CDATA[XML解析器]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=318</guid>
		<description><![CDATA[XML格式使用越来越广泛，不论是游戏还是普通应用软件都有使用。而我喜欢用xml作为配置文件。XML解析的方法实在很多，最根本的两种是SAX和DOM。SAX需要一边读取文件一边解析，速度较快；而DOM采用树状结构保存解析文件，使用方便。iPone自带的XML解析器一则无法跨平台，一则使用不便，还有一些bug（我是指SDK 2.0，新版本SDK 3.0的XML解析器没用过，所以没有发言权），所以我决定使用其他开源XML解析器。开源的XML解析器也有很多种，比如libxml2，TouchXML（iPhone版），tinyxml等。其中tinyxml简单易用，如果你不要求诸如DTD等功能，那么tinyxml绝对应该是你的首选。我采用的是ticpp（tinyxml的cpp版本，采用DOM方法）。
&#160;

项目下载
&#160;
下面是ticpp的Xcode项目下载。
&#160;
（注：虽然我讨论的是跨平台代码，但我这里只是提供了Xcode项目，要在Windows下使用也是十分简单的事情，你可以自创Visual Studio项目文件或使用GNU make）
&#160;
使用方法
&#160;
这里我给出一个xml文件示例：
123456&#60;options version=&#34;1.0&#34;&#62;
&#60;levels&#62;
&#160; &#160; &#60;level name=&#34;InTheBeginning&#34; enemy=&#34;10&#34; /&#62;
&#160; &#160; &#60;level name=&#34;Fighting&#34; enemy=&#34;100&#34; /&#62; &#160; &#160;
&#60;/levels&#62;
&#60;/options&#62;
&#160;
首先，我定义了一个XML解析器类，它定义了XML解析的接口，我使用ticpp，但是如果你更改XML解析器为比如libxml2，你只需更改类的实现部分，你运用XML解析器的程序代码仍然可以保持不变。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174//////////////////////// 头文件 ///////////////////////////////////
/*
&#160;* &#160;xmlparser.h
&#160;* &#160;utils
&#160;*
&#160;* &#160;Created by Bagusflyer on 09-6-27.
&#160;* &#160;Copyright 2009 www.iphone-geek.cn. All rights reserved.
&#160;*
&#160;*/

#ifndef _UTILS_XMLPARSER_H_
#define _UTILS_XMLPARSER_H_

#include &#60;fstream&#62;
#include &#34;ticpp/ticpp.h&#34;
&#160; &#160; 
typedef ticpp::Element XmlNode;
typedef ticpp::Document XmlDocument; 

using namspace std；

namespace utils
&#123;

&#160; &#160; class CXmlParser
&#160; &#160; &#123;
&#160; &#160; public:
&#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>XML格式使用越来越广泛，不论是游戏还是普通应用软件都有使用。而我喜欢用xml作为配置文件。XML解析的方法实在很多，最根本的两种是SAX和DOM。SAX需要一边读取文件一边解析，速度较快；而DOM采用树状结构保存解析文件，使用方便。iPone自带的XML解析器一则无法跨平台，一则使用不便，还有一些bug（我是指SDK 2.0，新版本SDK 3.0的XML解析器没用过，所以没有发言权），所以我决定使用其他开源XML解析器。开源的XML解析器也有很多种，比如libxml2，TouchXML（iPhone版），tinyxml等。其中tinyxml简单易用，如果你不要求诸如DTD等功能，那么tinyxml绝对应该是你的首选。我采用的是ticpp（tinyxml的cpp版本，采用DOM方法）。</p>
<p>&nbsp;</p>
<p><span id="more-318"></span></p>
<h2>项目下载</h2>
<p>&nbsp;</p>
<p>下面是ticpp的Xcode项目<a href='http://www.iphone-geek.cn/wp-content/uploads/2009/11/ticpp.zip'>下载</a>。</p>
<p>&nbsp;</p>
<p>（注：虽然我讨论的是跨平台代码，但我这里只是提供了Xcode项目，要在Windows下使用也是十分简单的事情，你可以自创Visual Studio项目文件或使用GNU make）</p>
<p>&nbsp;</p>
<h2>使用方法</h2>
<p>&nbsp;</p>
<p>这里我给出一个xml文件示例：</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;options</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;levels<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;InTheBeginning&quot;</span> <span style="color: #000066;">enemy</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Fighting&quot;</span> <span style="color: #000066;">enemy</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> &nbsp; &nbsp;<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/levels<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/options<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>首先，我定义了一个XML解析器类，它定义了XML解析的接口，我使用ticpp，但是如果你更改XML解析器为比如libxml2，你只需更改类的实现部分，你运用XML解析器的程序代码仍然可以保持不变。</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">//////////////////////// 头文件 ///////////////////////////////////</span><br />
<span style="color: #ff0000; font-style: italic;">/*<br />
&nbsp;* &nbsp;xmlparser.h<br />
&nbsp;* &nbsp;utils<br />
&nbsp;*<br />
&nbsp;* &nbsp;Created by Bagusflyer on 09-6-27.<br />
&nbsp;* &nbsp;Copyright 2009 www.iphone-geek.cn. All rights reserved.<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339900;">#ifndef _UTILS_XMLPARSER_H_</span><br />
<span style="color: #339900;">#define _UTILS_XMLPARSER_H_</span><br />
<br />
<span style="color: #339900;">#include &lt;fstream&gt;</span><br />
<span style="color: #339900;">#include &quot;ticpp/ticpp.h&quot;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #0000ff;">typedef</span> ticpp<span style="color: #008080;">::</span><span style="color: #007788;">Element</span> XmlNode<span style="color: #008080;">;</span><br />
<span style="color: #0000ff;">typedef</span> ticpp<span style="color: #008080;">::</span><span style="color: #007788;">Document</span> XmlDocument<span style="color: #008080;">;</span> <br />
<br />
<span style="color: #0000ff;">using</span> namspace std；<br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">class</span> CXmlParser<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CXmlParser<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span>m_pRootNode<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> parse<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> file,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> parse<span style="color: #008000;">&#40;</span>ifstream<span style="color: #000040;">&amp;</span> stream,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span> parse<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> buf, <span style="color: #0000ff;">size_t</span> len, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootNode<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">int</span> getAttribute<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib, <span style="color: #0000ff;">int</span> defval<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">float</span> getAttributeFloat<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib, <span style="color: #0000ff;">float</span> defval<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; string getAttribute<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; string getText<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; string getName<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> getFirstChild<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> nodeName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> getFirstChild<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> getNextChild<span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,string nodeName<span style="color: #000080;">=</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> rootNode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> m_pRootNode<span style="color: #008080;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlDocument m_doc<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span>&nbsp; &nbsp; m_pRootNode<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #339900;">#endif // _UTILS_XMLPARSER_H_</span><br />
<br />
<span style="color: #666666;">//////////////////////////// 实现文件 ///////////////////////////////</span><br />
<span style="color: #ff0000; font-style: italic;">/*<br />
&nbsp;* &nbsp;xmlparser.cpp<br />
&nbsp;* &nbsp;utils<br />
&nbsp;*<br />
&nbsp;* &nbsp;Created by Bagusflyer on 09-6-27.<br />
&nbsp;* &nbsp;Copyright 2009 www.iphone-geek.cn. All rights reserved.<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339900;">#include &quot;utils/xmlparser.h&quot;</span><br />
<br />
<span style="color: #0000ff;">namespace</span> utils<br />
<span style="color: #008000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib,<span style="color: #0000ff;">int</span> defval<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">long</span> value<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; elem.<span style="color: #007788;">GetAttributeOrDefault</span><span style="color: #008000;">&#40;</span>attrib,<span style="color: #000040;">&amp;</span>value,defval<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> value<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">float</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getAttributeFloat</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib, <span style="color: #0000ff;">float</span> defval<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">float</span> value<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; elem.<span style="color: #007788;">GetAttributeOrDefault</span><span style="color: #008000;">&#40;</span>attrib,<span style="color: #000040;">&amp;</span>value,defval<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> value<span style="color: #008080;">;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; string CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> attrib<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; string value<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; elem.<span style="color: #007788;">GetAttributeOrDefault</span><span style="color: #008000;">&#40;</span>attrib,<span style="color: #000040;">&amp;</span>value,_T<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> value<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">parse</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> file,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">ifstream</span> in<span style="color: #008000;">&#40;</span>file.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>in.<span style="color: #007788;">good</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 注意：错误检查，略去</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">//throw XmlParserError(_T(&quot;Can't open &quot;+file));</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parse<span style="color: #008000;">&#40;</span>in,rootName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">parse</span><span style="color: #008000;">&#40;</span>ifstream<span style="color: #000040;">&amp;</span> stream,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; stream.<span style="color: #007788;">seekg</span><span style="color: #008000;">&#40;</span>0,std<span style="color: #008080;">::</span><span style="color: #007788;">ios</span><span style="color: #008080;">::</span><span style="color: #007788;">end</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">size_t</span> len <span style="color: #000080;">=</span> stream.<span style="color: #007788;">tellg</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; stream.<span style="color: #007788;">seekg</span><span style="color: #008000;">&#40;</span>0,std<span style="color: #008080;">::</span><span style="color: #007788;">ios</span><span style="color: #008080;">::</span><span style="color: #007788;">beg</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> buf <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#91;</span>len<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; stream.<span style="color: #007788;">read</span><span style="color: #008000;">&#40;</span>buf,len<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">try</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>buf,len,rootName<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">delete</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buf<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">catch</span> <span style="color: #008000;">&#40;</span>ticpp<span style="color: #008080;">::</span><span style="color: #007788;">Exception</span><span style="color: #000040;">&amp;</span> ex<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">delete</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buf<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">throw</span> ex<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">parse</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> buf, <span style="color: #0000ff;">size_t</span> len, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> rootNode<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// Load a document</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> data<span style="color: #008000;">&#40;</span>buf,len<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_doc.<span style="color: #007788;">Parse</span><span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_pRootNode <span style="color: #000080;">=</span> m_doc.<span style="color: #007788;">FirstChildElement</span><span style="color: #008000;">&#40;</span>rootNode<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">catch</span><span style="color: #008000;">&#40;</span> ticpp<span style="color: #008080;">::</span><span style="color: #007788;">Exception</span><span style="color: #000040;">&amp;</span> ex <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// If any function has an error, execution will enter here.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// Report the error</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 注意：请使用自己的XmlParser exception类</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// throw XmlParserError(&quot;Failed to parse xml&quot;); </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; string CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getText</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">GetTextOrDefault</span><span style="color: #008000;">&#40;</span>_T<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; string CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getName</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">Value</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getFirstChild</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,<span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> nodeName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">FirstChildElement</span><span style="color: #008000;">&#40;</span>nodeName,<span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getFirstChild</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">FirstChildElement</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> CXmlParser<span style="color: #008080;">::</span><span style="color: #007788;">getNextChild</span><span style="color: #008000;">&#40;</span>XmlNode<span style="color: #000040;">&amp;</span> elem,string nodeName<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> nodeName.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">NextSiblingElement</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> elem.<span style="color: #007788;">NextSiblingElement</span><span style="color: #008000;">&#40;</span>nodeName,<span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// namespace utils</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>然后，在你的代码（比如我有一个COptionParser，它包含了一个utils::CXmlParser对象m_xmlPaser）中使用XmlParser进行解析：</p>
<div class="codecolorer-container cpp mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">/////////////////// 以下是进行解析的代码 //////////////////////////////</span><br />
<span style="color: #0000ff;">void</span> parser<span style="color: #008000;">&#40;</span>string<span style="color: #000040;">&amp;</span> xmlFile<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; m_xmlParser.<span style="color: #007788;">parse</span><span style="color: #008000;">&#40;</span>xmlFile,<span style="color: #FF0000;">&quot;options&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// 解析version属性</span><br />
&nbsp; &nbsp; string version <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>m_xmlParser.<span style="color: #007788;">rootNode</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,<span style="color: #FF0000;">&quot;version&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> pLevelsNode <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getFirstChild</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>m_xmlParser.<span style="color: #007788;">rootNode</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,<span style="color: #FF0000;">&quot;levels&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>pLevelsNode <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 定义自己的exception</span><br />
&nbsp; &nbsp; <span style="color: #666666;">//throw utils::XmlParserError(&quot;No levels found&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>pLevelsNode <span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 解析每个Level</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XmlNode<span style="color: #000040;">*</span> chd <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getFirstChild</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>pLevelsNode ,<span style="color: #FF0000;">&quot;level&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>chd<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 解析level名称</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string name <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>chd,<span style="color: #FF0000;">&quot;name&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 解析敌人数目，缺省值为15</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">int</span> enemies <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getAttribute</span><span style="color: #008000;">&#40;</span>elem,<span style="color: #FF0000;">&quot;enemy&quot;</span>,15<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// 下一个level</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chd <span style="color: #000080;">=</span> m_xmlParser.<span style="color: #007788;">getNextChild</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>chd,<span style="color: #FF0000;">&quot;level&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>&nbsp;</p>
<p>代码虽然不少，可是思路还是很简单的。下一次，我会介绍跨平台压缩库。有什么问题，欢迎留言探讨！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%b7%a8%e5%b9%b3%e5%8f%b0%e4%bb%a3%e7%a0%81%e5%88%86%e4%ba%ab%e4%b9%8b%e4%b8%89-%e2%80%93-xml%e8%a7%a3%e6%9e%90%e5%99%a8/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
