<?xml version="1.0" encoding="utf-8" ?><rss version="2.0">
<channel>
<title><![CDATA[康的blog]]></title>
<link>http://www.kanglog.com/weblog</link>
<description><![CDATA[关注java,python,javascript,flex,c++,sgs]]></description>
<language>zh-cn</language>
<copyright><![CDATA[Copyright 2010, 康的blog]]></copyright>
<webMaster><![CDATA[lx1988cyk@gmail.com ()]]></webMaster>
<generator>kanglog 2.0</generator>
<pubDate>2010</pubDate>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&144]]></link>
	<title><![CDATA[JAXB, Sun and how to marshal a CDATA element]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[收藏]]></category>
	<pubDate>2010-08-16 03:37:51</pubDate>
	<description><![CDATA[<span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 13px; color: rgb(51, 51, 51); line-height: 20px; "><div><span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 13px; color: rgb(51, 51, 51); line-height: 20px; ">[转自]http://odedpeer.blogspot.com/2010/07/jaxb-sun-and-how-to-marshal-cdata.html</span></div>According to&nbsp;<a href="https://jaxb.dev.java.net/faq/index.html#marshalling_cdata" style="color: rgb(85, 136, 170); text-decoration: none; ">https://jaxb.dev.java.net/faq/index.html#marshalling_cdata</a>&nbsp;there is no direct support in marshalling CDATA blocks, this is vendor specific.<br>I‘ll describe how this is done when using the built-in Sun implementation by an example.</span><div><span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 13px; color: rgb(51, 51, 51); line-height: 20px; "><br></span></div><div><span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 13px; color: rgb(51, 51, 51); line-height: 20px; ">Suppose this is my JAXB annotated class:</span></div><div><span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 13px; color: rgb(51, 51, 51); line-height: 20px; "><br></span></div><div><font class="Apple-style-span" color="#333333" face="Georgia, serif" size="3"><span class="Apple-style-span" style="font-size: 13px; line-height: 20px;"><div>package org.oded; &nbsp;</div><div>&nbsp;&nbsp;</div><div>import javax.xml.bind.annotation.*; &nbsp;</div><div>import javax.xml.bind.annotation.adapters.*; &nbsp;</div><div>&nbsp;&nbsp;</div><div>@XmlRootElement &nbsp;</div><div>@XmlAccessorType(XmlAccessType.FIELD) &nbsp;</div><div>public class Item &nbsp;</div><div>{ &nbsp;</div><div>&nbsp;@XmlAttribute public int id; &nbsp;</div><div>&nbsp;&nbsp;&nbsp;</div><div>&nbsp;public String text; &nbsp;</div><div>} &nbsp;</div><div><br></div><div>Next I run the Main class:</div><div><br></div><div><div>package org.oded; &nbsp;&nbsp;&nbsp;</div><div>import java.io.*; &nbsp;</div><div>&nbsp;&nbsp;</div><div>import javax.xml.bind.*; &nbsp;</div><div>&nbsp;&nbsp;</div><div>import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; &nbsp;</div><div>&nbsp;&nbsp;</div><div>public class Main &nbsp;</div><div>{ &nbsp;</div><div>&nbsp;public static void main( String[] args ) throws Exception &nbsp;</div><div>&nbsp;{ &nbsp;</div><div>&nbsp;&nbsp;Item i1 = new Item(); &nbsp;</div><div>&nbsp;&nbsp;i1.text = "hello"; &nbsp;</div><div>&nbsp;&nbsp;i1.id = 1; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp;Item i2 = new Item(); &nbsp;</div><div>&nbsp;&nbsp;i2.text = "&lt;code&gt;&lt;helloworld&gt;&lt;/helloworld&gt;&lt;/code&gt;"; &nbsp;</div><div>&nbsp;&nbsp;i2.id = 2; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp;Marshaller m = JAXBContext.newInstance( Item.class ).createMarshaller(); &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp;m.marshal( i1, new OutputStreamWriter( System.out ) ); &nbsp;</div><div>&nbsp;&nbsp;System.out.println(); &nbsp;</div><div>&nbsp;&nbsp;m.marshal( i2, new OutputStreamWriter( System.out ) ); &nbsp;</div><div>&nbsp;} &nbsp;</div><div>} &nbsp;</div></div><div><br></div><div>The output is:</div><div><div>&lt;item id="1"&gt;&lt;text&gt;hello&lt;/text&gt;&lt;/item&gt; &nbsp;</div><div>&lt;item id="2"&gt;&lt;text&gt;&lt;code&gt;&lt;helloWorld/&gt;&lt;/code&gt;&lt;/text&gt;&lt;/item&gt; &nbsp;</div></div><div><br></div><div><br>Now suppose I want to wrap the XML value of&nbsp;<i>text</i>&nbsp;in a CDATA element and avoid the escaping, I need to add specify an Adapter for<i>text</i>&nbsp;to surround the value in a CDATA element in the following way:</div><div><br></div><div><div>package org.oded; &nbsp;</div><div>&nbsp;&nbsp;</div><div>import javax.xml.bind.annotation.*; &nbsp;</div><div>import javax.xml.bind.annotation.adapters.*; &nbsp;</div><div>&nbsp;&nbsp;</div><div>@XmlRootElement &nbsp;</div><div>@XmlAccessorType(XmlAccessType.FIELD) &nbsp;</div><div>public class Item &nbsp;</div><div>{ &nbsp;</div><div>&nbsp;@XmlAttribute public int id; &nbsp;</div><div>&nbsp;&nbsp;&nbsp;</div><div>&nbsp;@XmlJavaTypeAdapter(value=Adapter.class) &nbsp;&nbsp;</div><div>&nbsp;public String text; &nbsp;</div><div>&nbsp;&nbsp;&nbsp;</div><div>&nbsp;private static class Adapter extends XmlAdapter&lt;string, string=""&gt; &nbsp;</div><div>&nbsp;{ &nbsp;</div><div>&nbsp;&nbsp;</div><div>&nbsp;&nbsp;@Override &nbsp;</div><div>&nbsp;&nbsp;public String marshal( String v ) throws Exception &nbsp;</div><div>&nbsp;&nbsp;{ &nbsp;</div><div>&nbsp;&nbsp; return "&lt;![CDATA["   v   "]]&gt;"; &nbsp;</div><div>&nbsp;&nbsp;} &nbsp;</div><div>&nbsp;&nbsp;</div><div>&nbsp;&nbsp;@Override &nbsp;</div><div>&nbsp;&nbsp;public String unmarshal( String v ) throws Exception &nbsp;</div><div>&nbsp;&nbsp;{ &nbsp;</div><div>&nbsp;&nbsp; return v; &nbsp;</div><div>&nbsp;&nbsp;} &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;} &nbsp;</div><div>} &nbsp;</div></div><div><br></div><div>Now tell the Marshaller, in a Sun-specific way, not to escape the value of&nbsp;<i>text</i>:</div><div><br></div><div><div>package org.oded; &nbsp;</div><div>&nbsp;&nbsp;</div><div>import java.io.*; &nbsp;</div><div>&nbsp;&nbsp;</div><div>import javax.xml.bind.*; &nbsp;</div><div>&nbsp;&nbsp;</div><div>import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; &nbsp;</div><div>&nbsp;&nbsp;</div><div>public class Main &nbsp;</div><div>{ &nbsp;</div><div>&nbsp;public static void main( String[] args ) throws Exception &nbsp;</div><div>&nbsp;{ &nbsp;</div><div>&nbsp;&nbsp;Item i1 = new Item(); &nbsp;</div><div>&nbsp;&nbsp;i1.text = "hello"; &nbsp;</div><div>&nbsp;&nbsp;i1.id = 1; &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp;Item i2 = new Item(); &nbsp;</div><div>&nbsp;&nbsp;i2.text = "&lt;code&gt;&lt;helloworld&gt;&lt;/helloworld&gt;&lt;/code&gt;"; &nbsp;</div><div>&nbsp;&nbsp;i2.id = 2; &nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp;</div><div>&nbsp;&nbsp;Marshaller m = JAXBContext.newInstance( Item.class ).createMarshaller(); &nbsp;</div><div>&nbsp;&nbsp;m.setProperty( "com.sun.xml.internal.bind.characterEscapeHandler", new CharacterEscapeHandler() { &nbsp;</div><div>&nbsp;&nbsp; @Override &nbsp;</div><div>&nbsp;&nbsp; public void escape( char[] ac, int i, int j, boolean flag, Writer writer ) throws IOException &nbsp;</div><div>&nbsp;&nbsp; { &nbsp;</div><div>&nbsp;&nbsp; &nbsp;// do not escape &nbsp;</div><div>&nbsp;&nbsp; &nbsp;writer.write( ac, i, j ); &nbsp;</div><div>&nbsp;&nbsp; } &nbsp;</div><div>&nbsp;&nbsp;}); &nbsp;</div><div>&nbsp;&nbsp; &nbsp;</div><div>&nbsp;&nbsp;m.marshal( i1, new OutputStreamWriter( System.out ) ); &nbsp;</div><div>&nbsp;&nbsp;System.out.println(); &nbsp;</div><div>&nbsp;&nbsp;m.marshal( i2, new OutputStreamWriter( System.out ) ); &nbsp;</div><div>&nbsp;} &nbsp;</div><div>} &nbsp;</div></div><div><br></div><div>Now the output is:</div><div><br></div><div><div>&lt;item id="1"&gt;&lt;text&gt;&lt;![CDATA[hello]]&gt;&lt;/text&gt;&lt;/item&gt; &nbsp;</div><div>&lt;item id="2"&gt;&lt;text&gt;&lt;![CDATA[&lt;code&gt;&lt;helloworld&gt;&lt;/helloworld&gt;&lt;/code&gt;]]&gt;&lt;/text&gt;&lt;/item&gt; &nbsp;</div></div></span></font></div>]]></description>
</item>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&143]]></link>
	<title><![CDATA[python import 中文模块 的方法]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[默认]]></category>
	<pubDate>2010-07-08 04:51:43</pubDate>
	<description><![CDATA[python中，不支持中文变量，所以如下：<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">	</span>import test.中文模块</div><div>会报错。</div><div><br></div><div>解决办法是利用__import__函数：</div><div><br></div><div>mod_name=u‘中文模块‘;</div><div>packa=__import__(‘test.‘ mod_name);</div><div>mod=getattr(&nbsp;packa, dir(packa)[dir(packa).index(mod_name.split(‘.‘).pop())]</div><div><br></div><div>jython中用java调用就没这么多事，代码如下：</div><div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;PyModule mod=(PyModule) __builtin__.__import__("test.中文模块").__getattr__("中文模块");</div></div>]]></description>
</item>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&142]]></link>
	<title><![CDATA[本博客程序提供下载]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[默认]]></category>
	<pubDate>2010-07-08 04:45:15</pubDate>
	<description><![CDATA[下载地址: <a href="http://www.kanglog.com/kanglog.rar" target="_blank">http://www.kanglog.com/kanglog.rar</a><div><br></div><div>说明：</div><div><br></div><div><div>1、在mysql中手动创建好数据库，然后把localhost.sql导入到数据库中</div><div><br></div><div>2、数据库配置：weblog\inc\db.php中，把</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>@数据库账号</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>@数据库密码</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>@数据库名称</div><div>换成你自己的</div><div><br></div><div>3、发邮件配置:weblog\inc\sendmail.php中换成你自己的</div><div><br></div><div>4、为了安全，可以把sendmail.php改名，然后在weblog\ajax.php中，替换sendmail.php</div><div><br></div><div>5、以下功能暂未添加</div><div>blog设置 （需要手动在数据库改:`blog_config`）</div><div>增加皮肤</div><div>编辑皮肤列表</div><div>增加链接 &nbsp;(weblog\inc\skin\clean-simple-white\html.php 中手动改)</div><div>编辑链接 &nbsp;</div><div><br></div><div>6、整个blog程序，重在ajax。后台的php只是负责验证和存取数据。如果学习的话，weblog\js\下的文件是精华。</div></div><div><br></div><div>7、日志编辑器用的“小小菜刀编辑器2.0”，为了安全，最好把weblog\HtmlEditor\editor\upload.php删掉，或者加上安全验证。</div>]]></description>
</item>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&141]]></link>
	<title><![CDATA[java nio]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[默认]]></category>
	<pubDate>2010-06-24 03:18:12</pubDate>
	<description><![CDATA[好久没碰NIO了，都忘了。在此记录一下<div><br></div><div>ServerSocketChannel ssc=ServerSocketChannel.open();</div><div>ssc.socket().bind(new InetSocketAddress(port));</div><div>ssc.configureBlocking(false);</div><div><br></div><div>Selector selector=Selector.open();</div><div><br></div><div><div>while (!stoped) {</div><div>&nbsp;&nbsp; &nbsp;try {</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>selector.select();</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>logger.debug("SELECTED");</div><div>&nbsp;&nbsp; &nbsp;} catch (IOException ex) {</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>logger.error(ex);</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>continue;</div><div>&nbsp;&nbsp; &nbsp;}</div><div>&nbsp;&nbsp; &nbsp;Iterator&lt;SelectionKey&gt; iter = selector.selectedKeys().iterator();</div><div>&nbsp;&nbsp; &nbsp;while (iter.hasNext()) {</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>final SelectionKey key = iter.next();</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>iter.remove();</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>if (key.isAcceptable()) {</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;try {</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>logger.debug("key.isAcceptable");</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>ServerSocketChannel server = (ServerSocketChannel) key.channel();</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>SocketChannel channel = server.accept();</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>channel.configureBlocking(false);</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>channel.register(selector, SelectionKey.OP_READ);</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;} catch (IOException ex) {</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>logger.error(ex);</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;}</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>} else if (key.isReadable()) {</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;logger.debug("key.isReadable");</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;SocketChannel channel = (SocketChannel) key.channel();</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;clientBuffer.clear();</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;try {</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>int count = channel.read(clientBuffer);</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>if (count &gt; 0) {</div><div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp;clientBuffer.flip();</div><div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp;byte[] dst = new byte[clientBuffer.remaining()];</div><div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp;clientBuffer.get(dst);</div><div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp;//处理消息</div><div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp;handlerMessage(channel, dst);</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>}</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;} catch (IOException e) {</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>logger.error(e);</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>try {</div><div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp;channel.close();</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>} catch (IOException ex) {</div><div><span class="Apple-tab-span" style="white-space:pre">		</span> &nbsp; &nbsp;logger.error(ex);</div><div><span class="Apple-tab-span" style="white-space:pre">		</span>}</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;}</div><div><span class="Apple-tab-span" style="white-space:pre">	</span> &nbsp; &nbsp;key.cancel();</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div><div>&nbsp;&nbsp; &nbsp;}</div><div>}</div></div>]]></description>
</item>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&138]]></link>
	<title><![CDATA[flex xml 查找 删除]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[默认]]></category>
	<pubDate>2010-06-21 22:07:32</pubDate>
	<description><![CDATA[一、删除节点的某个子节点<br><br>var list:XMLList=treeData.children();<br>for(var i:int=0;i&lt;list.length();i  ){<br>var child=list[i];<br>if(i==deleteIndex){<br>delete list[i];<br>}<br>}<br><br>二、查找节点<br>treeData.node.(@label==link.cmd).toXMLString()<br>]]></description>
</item>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&137]]></link>
	<title><![CDATA[python 遍历文件夹]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[默认]]></category>
	<pubDate>2010-06-10 03:40:10</pubDate>
	<description><![CDATA[<div><div>python使用os.walk和os.path.join遍历文件夹的两种方法</div><div><br></div><div>for root, dirs, files in os.walk(‘D:svn_project‘):</div><div>&nbsp;&nbsp; &nbsp;for file in files:</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;print os.path.join(root, file)</div><div><br></div><div>import os</div><div>for root, dirs, files in os.walk(‘D:‘):</div><div>&nbsp;&nbsp; &nbsp;for fi in range(0,files.__len__()):</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;print os.path.join(root,files[fi])</div></div>]]></description>
</item>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&136]]></link>
	<title><![CDATA[[转]Flex读写XML的方法总结]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[收藏]]></category>
	<pubDate>2010-06-08 02:16:46</pubDate>
	<description><![CDATA[<P>Flex读写XML文件 <BR>Entry posted Apr 09 by HuShaohua, last edited Apr 15 <BR>1049 Views,&nbsp; 0 Comments <BR>Title:Flex读写XML文件Entry:Flex 读取xml文件的方法: </P>
<P>方法一: </P>
<P>private function readXML1():void{ <BR>&nbsp;&nbsp;&nbsp; var xmlFile:File = new File("file path"); <BR>&nbsp;&nbsp;&nbsp; var stream:FileStream = new FileStream(); <BR>&nbsp;&nbsp;&nbsp; stream.open(xmlFile, FileMode.READ); <BR>&nbsp;&nbsp;&nbsp; var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable)); <BR>&nbsp;&nbsp;&nbsp; stream.close(); <BR>} </P>
<P>方法二: </P>
<P>private function&nbsp; readXML2():void{ <BR>&nbsp;&nbsp;&nbsp; var urlRequest:URLRequest = new URLRequest("xml file path"); <BR>&nbsp;&nbsp;&nbsp; var urlLoader:URLLoader = new URLLoader(urlRequest); <BR>&nbsp;&nbsp;&nbsp; urlLoader.addEventListener(Event.COMPLETE,completeHandler); <BR>} <BR>&nbsp;&nbsp; <BR>private function completeHandler(event:Event):void{ <BR>&nbsp;&nbsp;&nbsp; var xml:XML = new XML(event.target.data); <BR>&nbsp;&nbsp;&nbsp; trace(xml); <BR>} </P>
<P>Flex写XML的方法: </P>
<P>public static function writeXMLFile(xml:XML):void{ <BR>&nbsp;&nbsp;&nbsp; var stream:FileStream = new FileStream(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var xmlFile:File = new File("file path"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var outputString:String = ‘&lt;?xml version="1.0" encoding="utf-8"?&gt; ‘; <BR>&nbsp;&nbsp;&nbsp; outputString = xml.toXMLString(); <BR>&nbsp;&nbsp;&nbsp; outputString = outputString.replace(/ /g, File.lineEnding); <BR>&nbsp;&nbsp;&nbsp; stream = new FileStream(); <BR>&nbsp;&nbsp;&nbsp; stream.open(xmlFile, FileMode.WRITE); <BR>&nbsp;&nbsp;&nbsp; stream.writeUTFBytes(outputString); <BR>&nbsp;&nbsp;&nbsp; stream.close(); <BR>}&nbsp; </P>]]></description>
</item>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&135]]></link>
	<title><![CDATA[[转]flex之xml操作(上)]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[收藏]]></category>
	<pubDate>2010-06-08 02:15:21</pubDate>
	<description><![CDATA[<P>今天我们来看看AS3中新的XML处理方法：E4X，直到现在，ECMA脚本语言规范（ECMA-262）－－AscriptScript 3.0的核心基础，并没有提供任何的XML数据处理类或方法。AcriontScript之前的版本（从Flash 5中的ActionScript开始）有一些处理XML数据的类和方法，但是它们并不是基于ECMAScript标准的。 </P>
<P>新的ECMA脚本语言规范第4版草稿中定义了一系列新的处理XML数据的类和方法。这些类和方法的集合并命名为E4X（"ECMAScript for XML"），ActionScript 3.0包括如下新的E4X类： XML、XMLList、 QName和Namespace。 </P>
<P>E4X类的方法、属性和操作的开法基于以下的目标： </P>
<P>简单－－E4X尽可能的使得处理XML数据的代码容易编写并且易于理解。 <BR>一致性－－E4X的方法于Actionscript的其它部分协调一致。 <BR>友好－－实用非常好理解的操作符处理XML数据，如点号（.）。 <BR>注意：为避免与E4X中的新的XML类冲突，原来ActionScript 2.0中的XML类在ActionScript 3.0被重命名为XMLDocument，为了向前兼容，在ActionScript 3.0中遗留下来的类－－XML、XMLNode、XMLParser和XMLTag--被包含进了flash.xml包中。新的E4X类是核心类－－使用它们不需要import任何包。 </P>
<P>初始化XML对象 </P>
<P>XML对象可以代表一个XML元素、属性、注释、处理指令或文本元素。在ActionScript 3.0中我们可以直接将XML数据赋值给变量： </P>
<P><BR>var myXML:XML = <BR>&nbsp;&nbsp; &lt;order&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item id=‘1‘&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;menuName&gt;burger&lt;/menuName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;price&gt;3.95&lt;/price&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/item&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item id=‘2‘&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;menuName&gt;fries&lt;/menuName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;price&gt;1.45&lt;/price&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/item&gt; <BR>&nbsp;&nbsp; &lt;/order&gt; <BR>你也可以使用new 构造器来从XML数据文本创建一个XML对象实例： </P>
<P><BR>var myXML:XML = new XML("&lt;order&gt;&lt;item id=‘1‘&gt;&lt;menuName&gt;burger&lt;/menuName&gt;&lt;price&gt;3.95&lt;/price&gt;&lt;/item&gt;&lt;/order&gt;") </P>
<P><BR>如果XML数据不是格式完好的（如少了结束标签），那么将会出现运行时错误。 </P>
<P>注意，你也可以将变量实例传入XML数据中： </P>
<P><BR>var tagname:String = "item"; <BR>var attributename:String = "id"; <BR>var attributevalue:String = 5; <BR>var content:String = "Chicken"; <BR>var x:XML = &lt;{tagname} {attributename}={attributevalue}&gt;{content}&lt;/{tagname}&gt;; <BR>trace (x.toXMLString()) <BR>&nbsp;&nbsp; // Output: &lt;item id="5"&gt;Chicken&lt;/item&gt; </P>
<P><BR>通常，我们的应用是从外部源导入XML数据，如web service或RSS feed,以下是一个从远程URL导入XML数据的例子： </P>
<P><BR>var myXML:XML = new XML(); <BR>var XML_URL:String = "<A href="http://www.example.com/Sample3.xml">http://www.example.com/Sample3.xml</A>"; <BR>//创建URLRequest。 <BR>var myXMLURL:URLRequest = new URLRequest(XML_URL); <BR>//使用URLLoader导入数据。 <BR>var myLoader:URLLoader = new URLLoader(myXMLURL); <BR>//添加事件监听器，以在XML数据导入完成后处理XML数据。 <BR>myLoader.addEventListener("complete", xmlLoaded); <BR>//导入完成后，创建使用导入的数据创建XML对象 <BR>function xmlLoaded(evtObj:Event) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var myXML:XML = XML(myLoader.data); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trace("Data loaded."); </P>
<P>} </P>
<P><BR>为了演示代码的清晰性，本文中的大部份示例都第1种直接使用文本的方法创建XML对象。 </P>
<P>E4X包含一些直观的方法XML数据的操作符（如.和@:用于访问属性）： </P>
<P><BR>//获取第1个item的menuName值 <BR>trace(myXML.item[0].menuName); // Output: burger <BR>//获取第1个item的id属性值 <BR>trace(myXML.item[0].@id);//Output:1 <BR>//获取id属性为2的item的menuName值 <BR>trace(myXML.item.(@id==2).menuName); // Output: fries <BR>//获取menuName为burger的item的price值 <BR>trace(myXML.item.(menuName=="burger").price); // Output: 3.95 </P>
<P>你也可以使用appendChild()方法给XML添加新的子节点： </P>
<P><BR>var newItem:XML = <BR>&nbsp;&nbsp; &lt;item id="3"&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;menuName&gt;medium cola&lt;/menuName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;price&gt;1.25&lt;/price&gt; <BR>&nbsp;&nbsp; &lt;/item&gt; </P>
<P>myXML.appendChild(newItem); </P>
<P><BR>当然你也可以使用@和.操作符来更新数据: </P>
<P><BR>myXML.item[0].menuName="regular burger"; <BR>myXML.item[1].menuName="small fries"; <BR>myXML.item[2].menuName="medium cola"; </P>
<P>myXML.item.(menuName=="regular burger").@quantity = "2"; <BR>myXML.item.(menuName=="small fries").@quantity = "2"; <BR>myXML.item.(menuName=="medium cola").@quantity = "2"; </P>
<P><BR>访问XML数据 </P>
<P>你可以使用.（点号）和..操作符访问XML对象的子节点，使用@操作符访问某一节点的属性。考虑以下XML对象： </P>
<P>&nbsp;</P>
<P>var x:XML = <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;book ISBN="0942407296"&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;title&gt;Baking Extravagant Pastries with Kumquats&lt;/title&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;author&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;lastName&gt;Contino&lt;/lastName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;firstName&gt;Chuck&lt;/firstName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/author&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;pageCount&gt;238&lt;/pageCount&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/book&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;book ISBN="0865436401"&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;title&gt;Emu Care and Breeding&lt;/title&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;editor&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;lastName&gt;Case&lt;/lastName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;firstName&gt;Justin&lt;/firstName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/editor&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;pageCount&gt;115&lt;/pageCount&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/book&gt; <BR>&nbsp;&nbsp; &lt;/order&gt; </P>
<P><BR>对象x.book表示一个包含所有名称为book的子节点的XMLList对象，该XMLList包含两个XML对象（两个book节点）。 </P>
<P>对象x..lastName表示一个包含XML树结构下部所有的lastName属性的XMLList对象，该XMList包含两个XML对象（两个LastName属性）。 </P>
<P>对象x.book.editor.lastName表示一个包含所有x对象的所有名称为book的子节点的所有名称为editor的子节点的所有lastName节点的XMLList对象，该XMLList只包含一个XML对象(值为"Case"的lastName属性)。 </P>
<P>访问父节点和子节点 </P>
<P>parent()方法返回XML对象的父节点。 </P>
<P>你可以使用子节点列表的顺序索引值来访问特定的子节点，例如，假定某一XML对象x有两个名称为book的子节点，你可以如下访问它们： </P>
<P><BR>//第1个book节点 <BR>x.book[0] <BR>//第2个book节点 <BR>x.book[1] </P>
<P><BR>要访问孙子节点，我们可以如下直接使用儿子和孙子节点的索引值来访问： </P>
<P><BR>x.book[0].title[0] </P>
<P>不过如果x.book[0]只有一个名称为title的子节点的话，那么可以省略索引： </P>
<P><BR>x.book[0].title </P>
<P>类似的，如果x对象只有一个book子节点，并且该book节点的子节点对象只有一个title对象，那么两个索引值都可以省略： </P>
<P>x.book.title </P>
<P>注意，你也可以使用child()方法，直接使用名称访问特定的子节点： </P>
<P><BR>var x.XML = <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;order&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;book&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;title&gt;Dictionary&lt;/title&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/book&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/order&gt; </P>
<P>var childName:String = "book"; <BR>trace (x.child(childName).title) // Output: Dictionary </P>
<P><BR>访问属性 </P>
<P>我们使用使用@操作符访问XMLNode属性： </P>
<P><BR>var myXML:XML = <BR>&nbsp;&nbsp; &lt;order&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item id=‘1‘&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;menuName&gt;burger&lt;/menuName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;price&gt;3.95&lt;/price&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/item&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item id=‘2‘&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;menuName&gt;fries&lt;/menuName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;price&gt;1.45&lt;/price&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/item&gt; <BR>&nbsp;&nbsp; &lt;/order&gt; </P>
<P>//获取第1个item的id属性值 <BR>trace(myXML.item[0].@id);//Output:1 </P>
<P><BR>使用属性或元素值过滤XML数据 </P>
<P>我们可以使用特定的元素名称和属性值来定位到特定的元素考虑以下XML对象： </P>
<P><BR>var x:XML = <BR>&nbsp;&nbsp; &lt;employeeList&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;employee id="347"&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;lastName&gt;Zmed&lt;/lastName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;firstName&gt;Sue&lt;/firstName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;position&gt;Data analyst&lt;/position&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/employee&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;employee id="348"&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;lastName&gt;McGee&lt;/lastName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;firstName&gt;Chuck&lt;/firstName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;position&gt;Jr. data analyst&lt;/position&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/employee&gt; <BR>&nbsp;&nbsp; &lt;/employeeList&gt; </P>
<P>&nbsp;</P>
<P>以下是正确的访问方法： </P>
<P><BR>//lastName为“McGee”的employee对象，第1个employee节点 <BR>x.employee.(lastName == "McGee") // The first employee node <BR>//lastName为“McGee”的employee对象的firstName节点，第1个employee节点的节点 <BR>x.employee.(lastName == "McGee").firstName // The firstName property of that node <BR>//lastName为“McGee”的id属性 <BR>x.employee.(lastName == "McGee").@id // The value of the id attribute <BR>//所有id属性值为347的employee对象列表 <BR>x.employee.(@id == 347) <BR>//id属性值为347的employee对象的lastName子节点 <BR>x.employee.(@id == 347).lastName <BR>//所有id属性值大于347的employee对象列表 <BR>x.employee.(@id &gt; 300) // An XML list with both employee properties <BR>//所有position子节点值包含“analyst”的employee对象列表 <BR>x.employee.(position.toString().search("analyst") &gt; -1) </P>
<P><BR>使用for ... in和for each ... in 语句 </P>
<P>ActionScript 3.0 包括了一个新用来遍历XMLList对象的的for ... in语句和for each ... in语句。例如，考虑以下XML对象，myXML和myXML..item XMLList对象（包含两个item XML对象节点的XML列表）： </P>
<P><BR>var myXML:XML = <BR>&nbsp;&nbsp; &lt;order&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item id=‘1‘ quantity=‘2‘&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;menuName&gt;burger&lt;/menuName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;price&gt;3.95&lt;/price&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/item&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item id=‘2‘ quantity=‘2‘&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;menuName&gt;fries&lt;/menuName&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;price&gt;1.45&lt;/price&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/item&gt; <BR>&nbsp;&nbsp; &lt;/order&gt; </P>
<P><BR>for ... in语句可以让我们遍历XMLList的所有属性名称,实际上就是个节点的索引值： </P>
<P><BR>var total:Number = 0; <BR>for (var pname:String in myXML..item) <BR>{ <BR>&nbsp;&nbsp;&nbsp; total = Number(<A href="mailto:myXML.item.@quantity[pname">myXML.item.@quantity[pname</A>]) * Number(myXML.item.price[pname]); <BR>} </P>
<P><BR>for each ... in语句遍历XMLList的所有节点： </P>
<P><BR>var total2:Number = 0; <BR>for each (var item:XML in myXML..item) <BR>{ <BR>total2 = Number(<A href="mailto:item@quantity">item@quantity</A>) * Number(item.price); <BR>} </P>
<P><BR>使用with语句 </P>
<P>我们可以使用with语句，来指明后续的属性和节点值都是基于某一XML对象，前面的for each ... in示例代码，使用with语句的代码如下： </P>
<P><BR>var total:Number = 0; <BR>for each (var item in myXML..item) <BR>{ <BR>&nbsp;&nbsp; with (item) <BR>&nbsp;&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //{内的属性和节点对象都是基于item XML对象的，所有不需要使用item.来访问。 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total = Number(@quantity) * Number(price); <BR>&nbsp;&nbsp;&nbsp;&nbsp; } </P>
<P>} <BR>trace(total); </P>
<P><BR>修改XML对象 </P>
<P>我们可以使用prependChild()方法或者appendChild()方法在XML对象的子节点列表的前面或者最后面添加节点： </P>
<P><BR>var x1:XML = &lt;p&gt;Paragraph 1&lt;/p&gt; <BR>var x2:XML = &lt;p&gt;Paragraph 2&lt;/p&gt; <BR>var x:XML = &lt;body&gt;&lt;/body&gt; <BR>x = x.appendChild(x1); <BR>x = x.appendChild(x2); <BR>x = x.prependChild(&lt;p&gt;Paragraph 0&lt;/p&gt;); </P>
<P>// x == &lt;body&gt;&lt;p&gt;Paragraph 0&lt;/p&gt;&lt;p&gt;Paragraph 1&lt;/p&gt;&lt;p&gt;Paragraph 2&lt;/p&gt;&lt;/body&gt; </P>
<P><BR>使用insertChildBefore()方法或者insertChildAfter()方法在特定的节点前面活着回后面添加节点： </P>
<P><BR>var x:XML = <BR>&nbsp;&nbsp; &lt;body&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;p&gt;Paragraph 1&lt;/p&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;p&gt;Paragraph 2&lt;/p&gt; <BR>&nbsp;&nbsp; &lt;/body&gt; </P>
<P>var newNode:XML = &lt;p&gt;Paragraph 1.5&lt;/p&gt; <BR>x = x.insertChildAfter(x.p[0], newNode) <BR>x = x.insertChildBefore(x.p[2], &lt;p&gt;Paragraph 1.75&lt;/p&gt;) </P>
<P><BR>注意，我们也可以在构造XML对象的时候使用大括号（{和}）来引用变量： </P>
<P><BR>var ids:Array = [121, 122, 123]; <BR>var names:Array = [["Murphy","Pat"],["Thibaut","Jean"], ["Smith","Vijay"]] <BR>var x:XML = new XML("&lt;employeeList&gt;&lt;/employeeList&gt;"); </P>
<P>for (var i:int = 0; i &lt; 3; i ) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var newnode:XML = new XML(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newnode = <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;employee id={ids[i]}&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;last&gt;{names[i][0]}&lt;/last&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;first&gt;{names[i][1]}&lt;/first&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/employee&gt; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = x.appendChild(newnode) </P>
<P>} </P>
<P><BR>我们也可以使用=操作符来给XML对象节点赋值： </P>
<P><BR>var x:XML = <BR>&nbsp;&nbsp; &lt;employee&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;lastname&gt;Smith&lt;/lastname&gt; <BR>&nbsp;&nbsp; &lt;/employee&gt; </P>
<P>x.firstname = "Jean"; <BR><A href="mailto:x.@id">x.@id</A> = "239"; </P>
<P><BR>以上代码将把XML对象X设置成如下： </P>
<P><BR>&lt;employee id="239"&gt; <BR>&nbsp;&nbsp; &lt;lastname&gt;Smith&lt;/lastname&gt; <BR>&nbsp;&nbsp; &lt;firstname&gt;Jean&lt;/firstname&gt; <BR>&nbsp;&nbsp; &lt;/employee&gt; </P>
<P><BR>我们也可以使用 和 =操作符来连结XMLList： </P>
<P><BR>var x1:XML = &lt;a&gt;test1&lt;/a&gt; <BR>var x2:XML = &lt;b&gt;test2&lt;/b&gt; <BR>var xList:XMLList = x1 x2; <BR>xList = &lt;c&gt;test3&lt;/c&gt; <BR>&nbsp;</P>]]></description>
</item>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&134]]></link>
	<title><![CDATA[[转]python中读写文件及中文编码处理方法]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[收藏]]></category>
	<pubDate>2010-05-26 06:14:55</pubDate>
	<description><![CDATA[<span class="Apple-style-span" style="font-family: Arial; line-height: 20px; font-size: 12px; color: rgb(102, 102, 102); -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><strong style="line-height: normal; ">一、打开文件</strong></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">代码如下：</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">&gt;&gt;&gt; f = open("d:	est.txt", "w")</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">说明：</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">第一个参数是文件名称，包括路径；第二个参数是打开的模式mode</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">‘r‘：只读（缺省。如果文件不存在，则抛出错误）</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">‘w‘：只写（如果文件不存在，则自动创建文件）</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">‘a‘：附加到文件末尾</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">‘r ‘：读写</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">如果需要以二进制方式打开文件，需要在mode后面加上字符"b<wbr style="line-height: normal; ">"，比如"rb""wb"等</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><strong style="line-height: normal; ">二、读取内容</strong></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">f.read(size)</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">参数size表示读取的数量，可以省略。如果省略size参数<wbr style="line-height: normal; ">，则表示读取文件所有内容。</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">f.readline()</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">读取文件一行的内容</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">f.readlines()</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">读取所有的行到数组里面[line1,line2,..<wbr style="line-height: normal; ">.lineN]。在避免将所有文件内容加载到内存中<wbr style="line-height: normal; ">，这种方法常常使用，便于提高效率。</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><strong style="line-height: normal; ">三、写入文件</strong></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">f.write(string) 将一个字符串写入文件，如果写入结束，必须在字符串后面加上"<wbr style="line-height: normal; ">
"，然后f.close()关闭文件</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><strong style="line-height: normal; ">四、文件中的内容定位</strong></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">f.read()读取之后，文件指针到达文件的末尾，如果再来一次f<wbr style="line-height: normal; ">.read()将会发现读取的是空内容，如果想再次读取全部内容<wbr style="line-height: normal; ">，必须将定位指针移动到文件开始：<br style="line-height: normal; ">f.seek(0)</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">这个函数的格式如下（单位是bytes）：</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">f.seek(offset, from_what)</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">from_what表示开始读取的位置，offset表示从fro<wbr style="line-height: normal; ">m_what再移动一定量的距离，比如f.seek(10, 3)表示定位到第三个字符并再后移10个字符。from<wbr style="line-height: normal; ">_what值为0时表示文件的开始，它也可以省略<wbr style="line-height: normal; ">，缺省是0即文件开头。下面给出一个完整的例子：</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; ">&gt;&gt;&gt; f = open(‘/tmp/workfile‘, ‘r ‘)<br style="line-height: normal; ">&gt;&gt;&gt; f.write(‘0123456789abcdef‘)<br style="line-height: normal; ">&gt;&gt;&gt; f.seek(5)&nbsp;&nbsp;&nbsp;&nbsp; # Go to the 6th byte in the file<br style="line-height: normal; ">&gt;&gt;&gt; f.read(1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; ">‘5‘<br style="line-height: normal; ">&gt;&gt;&gt; f.seek (-3, 2) # Go to the 3rd byte before the end<br style="line-height: normal; ">&gt;&gt;&gt; f.read(1)<br style="line-height: normal; ">‘d‘</div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "></div><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><strong style="line-height: normal; ">五、关闭文件释放资源</strong></div>文件操作完毕，一定要记得关闭文件f.close()<div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><wbr style="line-height: normal; ">，可以释放资源供其他程序使<br style="line-height: normal; "><br style="line-height: normal; "><font color="#0000ff" style="line-height: normal; ">*******************************************************</font><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "><strong style="line-height: normal; ">文件读写</strong><br style="line-height: normal; "></span><hr width="100%" size="2" style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 0); ">只是ASCII</span><span style="line-height: normal; color: rgb(0, 0, 0); ">或者gbk</span><span style="line-height: normal; color: rgb(0, 0, 0); ">编码格式的的文件读写，比较简单，读写如下：<br style="line-height: normal; "><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 13px; line-height: normal; border-right-color: rgb(204, 204, 204); border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: rgb(204, 204, 204); border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid; width: 927px; padding-top: 4px; border-bottom-color: rgb(204, 204, 204); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(238, 238, 238); "><span style="line-height: normal; color: rgb(0, 128, 128); ">1</span>&nbsp;<span style="line-height: normal; color: rgb(0, 128, 0); ">#</span><span style="line-height: normal; color: rgb(0, 128, 0); ">&nbsp;coding=gbk</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">2</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">3</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;open(</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">c:/intimate.txt</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">,</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">r</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">)&nbsp;</span><span style="line-height: normal; color: rgb(0, 128, 0); ">#</span><span style="line-height: normal; color: rgb(0, 128, 0); ">&nbsp;r 指示文件打开模式，即只读</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">4</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">s1&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;f.read()<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">5</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">s2&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;f.readline()<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">6</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">s3&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;f.readlines()&nbsp;</span><span style="line-height: normal; color: rgb(0, 128, 0); ">#</span><span style="line-height: normal; color: rgb(0, 128, 0); ">读出所有内容</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">7</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">8</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f.close()<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">9</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">10</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;open(</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">c:/intimate.txt</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">,</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">w</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">)&nbsp;</span><span style="line-height: normal; color: rgb(0, 128, 0); ">#</span><span style="line-height: normal; color: rgb(0, 128, 0); ">&nbsp;w 写文件</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">11</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f.write(s1)<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">12</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f.writelines(s2)&nbsp;</span><span style="line-height: normal; color: rgb(0, 128, 0); ">#</span><span style="line-height: normal; color: rgb(0, 128, 0); ">&nbsp;没有writeline</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">13</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f.close()</span></div><br style="line-height: normal; ">f.writelines不会输出换行符。<br style="line-height: normal; ">unicode文件读写：<br style="line-height: normal; "><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 13px; line-height: normal; border-right-color: rgb(204, 204, 204); border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: rgb(204, 204, 204); border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid; width: 927px; padding-top: 4px; border-bottom-color: rgb(204, 204, 204); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(238, 238, 238); "><span style="line-height: normal; color: rgb(0, 128, 128); ">1</span>&nbsp;<span style="line-height: normal; color: rgb(0, 128, 0); ">#</span><span style="line-height: normal; color: rgb(0, 128, 0); ">&nbsp;coding=gbk</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">2</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 255); ">import</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;codecs<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">3</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">4</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;codecs.open(</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">c:/intimate.txt</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">,</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">a</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">,</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">utf-8</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">)<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">5</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f.write(u</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">中文</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">)<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">6</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">s&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">中文</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">7</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f.write(s.decode(</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">gbk</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">))<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">8</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f.close()<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">9</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">10</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;codecs.open(</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">c:/intimate.txt</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">,</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">r</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">,</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">utf-8</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">)<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">11</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">s&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;f.readlines()<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">12</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">f.close()<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">13</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 255); ">for</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;line&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 255); ">in</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;s:<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">14</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 255); ">print</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;line.encode(</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">gbk</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">)</span></div></span><span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "><font color="#0000ff" style="line-height: normal; ">1 python代码文件的编码</font><br style="line-height: normal; "><br style="line-height: normal; ">py文件默认是ASCII编码，中文在显示时会做一个ASCII到系统默认编码的转换，这时就会出错：SyntaxError: Non-ASCII character。需要在代码文件的第一行或第二行添加编码指示：<br style="line-height: normal; "><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 13px; line-height: normal; border-right-color: rgb(204, 204, 204); border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: rgb(204, 204, 204); border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid; width: 927px; padding-top: 4px; border-bottom-color: rgb(204, 204, 204); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(238, 238, 238); "><span style="line-height: normal; color: rgb(0, 128, 128); ">1</span>&nbsp;<span style="line-height: normal; color: rgb(0, 128, 0); ">#</span><span style="line-height: normal; color: rgb(0, 128, 0); ">&nbsp;coding=utf-8</span><span style="line-height: normal; color: rgb(0, 128, 0); ">&nbsp;##以utf-8编码储存中文字符<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">2</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 255); ">print</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">中文</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span></div></span><span style="line-height: normal; color: rgb(0, 0, 0); ">像上面那样直接输入的字符串是按照代码文件的编码来处理的，如果用unicode编码，有以下三种方式：<br style="line-height: normal; "><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 13px; line-height: normal; border-right-color: rgb(204, 204, 204); border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: rgb(204, 204, 204); border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid; width: 927px; padding-top: 4px; border-bottom-color: rgb(204, 204, 204); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(238, 238, 238); "><span style="line-height: normal; color: rgb(0, 128, 128); ">1</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">s1&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;u</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">中文</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;#u表示用unicode编码方式储存信息<br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">2</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">s2&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;unicode(</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">中文</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">,</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">gbk</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">)<br style="line-height: normal; "></span></div>unicode是一个内置函数，第二个参数指示源字符串的编码格式。<br style="line-height: normal; ">decode是任何字符串具有的方法，将字符串转换成unicode格式，参数</span><span style="line-height: normal; color: rgb(0, 0, 0); ">指示源字符串的编码格式。<br style="line-height: normal; ">encode也</span><span style="line-height: normal; color: rgb(0, 0, 0); ">是任何字符串具有的方法，将字符串转换成参数指定的格式</span><span style="line-height: normal; color: rgb(0, 0, 0); ">。</span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; ">2 字符串的编码<br style="line-height: normal; "></span>用 u‘汉字‘ 构造出来的是unicode类型，不用的话构造出来是str类型&nbsp;<br style="line-height: normal; ">str的编码是与系统环境相关的，一般就是sys.getfilesystemencoding()得到的值&nbsp;<br style="line-height: normal; ">所以从unicode转str，要用encode方法&nbsp;<br style="line-height: normal; ">从str转unicode，所以要用decode<br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 0); ">例如：<br style="line-height: normal; "><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 13px; line-height: normal; border-right-color: rgb(204, 204, 204); border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: rgb(204, 204, 204); border-top-width: 1px; border-top-style: solid; padding-left: 4px; padding-bottom: 4px; border-left-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid; width: 927px; padding-top: 4px; border-bottom-color: rgb(204, 204, 204); border-bottom-width: 1px; border-bottom-style: solid; background-color: rgb(238, 238, 238); "><span style="line-height: normal; color: rgb(0, 128, 128); ">1</span>&nbsp;<span style="line-height: normal; color: rgb(0, 128, 0); ">#</span><span style="line-height: normal; color: rgb(0, 128, 0); ">&nbsp;coding=utf-8&nbsp;&nbsp; #默认编码格式为utf-8</span><span style="line-height: normal; color: rgb(0, 128, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">2</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">3</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 0); ">s&nbsp;</span><span style="line-height: normal; color: rgb(0, 0, 0); ">=</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;u</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">中文</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘ #unicode编码的文字</span><span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "></span><span style="line-height: normal; color: rgb(0, 128, 128); ">4</span>&nbsp;<span style="line-height: normal; color: rgb(0, 0, 255); ">print</span><span style="line-height: normal; color: rgb(0, 0, 0); ">&nbsp;s.encode(</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(128, 0, 0); ">utf-8</span><span style="line-height: normal; color: rgb(128, 0, 0); ">‘</span><span style="line-height: normal; color: rgb(0, 0, 0); ">)&nbsp;&nbsp; #转换成utf-8格式输出&nbsp;<br style="line-height: normal; ">5 print s #效果与上面相同</span><span style="line-height: normal; color: rgb(0, 0, 0); "><span style="line-height: normal; color: rgb(0, 0, 0); ">，似乎默认直接转换为指定编码</span></span><br style="line-height: normal; "><span style="line-height: normal; color: rgb(0, 0, 0); "><br style="line-height: normal; "></span><font color="#ff0000" style="line-height: normal; ">我的总结：</font><br style="line-height: normal; ">u=u‘unicode编码文字‘<br style="line-height: normal; ">g=u.encode(‘gbk‘) #转换为gbk格式<br style="line-height: normal; ">print g #此时为乱码，因为当前环境为utf-8,gbk编码文字为乱码<br style="line-height: normal; ">str=g.decode(‘gbk‘).encode(‘utf-8‘)&nbsp;&nbsp; #以gbk编码格式读取g（因为他就是gbk编码的）并转换为utf-8格式输出<br style="line-height: normal; ">print str #正常显示中文<br style="line-height: normal; "><br style="line-height: normal; "><font color="#0000ff" style="line-height: normal; ">安全的方法：</font><br style="line-height: normal; ">s.decode(‘gbk‘,‘ignore‘).encode(‘utf-8′) #以gbk编码读取（当然是读取gbk编码格式的文字了）并忽略错误的编码，转换成utf-8编码输出&nbsp;<br style="line-height: normal; "><br style="line-height: normal; ">因为decode的函数原型是decode([encoding], [errors=‘strict‘])，可以用第二个参数控制错误处理的策略，默认的参数就是strict，代表遇到非法字符时抛出异常；&nbsp;<br style="line-height: normal; ">如果设置为ignore，则会忽略非法字符；&nbsp;<br style="line-height: normal; ">如果设置为replace，则会用?取代非法字符；&nbsp;<br style="line-height: normal; ">如果设置为xmlcharrefreplace，则使用XML的字符引用。</div></span><br style="line-height: normal; ">******************<br style="line-height: normal; ">另外的方法：<br style="line-height: normal; ">f=file("/home/abc.txt")<br style="line-height: normal; ">for i in f:<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp; print i</div></span>
]]></description>
</item>
<item>
    <link><![CDATA[http://www.kanglog.com/weblog/index.php?#article&133]]></link>
	<title><![CDATA[[转]Python没有switch语句的解决方法]]></title>
	<author><![CDATA[小康]]></author>
	<category><![CDATA[收藏]]></category>
	<pubDate>2010-05-26 06:10:11</pubDate>
	<description><![CDATA[<span class="Apple-style-span" style="font-family: Arial; line-height: 18px; font-size: 12px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><p style="line-height: normal; ">from&nbsp;<a href="http://i.azpala.com/2008/01/20/python-switch/" style="color: rgb(68, 100, 0); font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; ">http://i.azpala.com/2008/01/20/python-switch/</a></p><p style="line-height: normal; ">其他语言中，switch语句大概是这样的</p><pre style="line-height: normal; "><font color="#339966" style="line-height: normal; ">switch (var)
{
    case value1: do_some_stuff1();
    case value2: do_some_stuff2();
    ...
    case valueN: do_some_stuffN();
    default: do_default_stuff();
}</font></pre><p style="line-height: normal; ">而python本身没有switch语句，解决方法有以下3种:</p><p style="line-height: normal; "><strong style="line-height: normal; ">A.使用dictionary</strong></p><pre style="line-height: normal; "><font color="#3366ff" style="line-height: normal; ">values = {
           value1: do_some_stuff1,
           value2: do_some_stuff2,
           ...
           valueN: do_some_stuffN,
         }
values.get(var, do_default_stuff)()</font></pre><pre style="line-height: normal; ">具体请参考: <a title="Read entry: Python switch statement" href="http://www.mustap.com/pythonzone_post_224_python-switch-statement" style="color: rgb(68, 100, 0); font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; ">Python switch statement</a></pre><pre style="line-height: normal; ">这个方法的缺点是:我不知道do_some_stuff是不是允许多个语句，如何实现，</pre><pre style="line-height: normal; ">也许是可以的，我不知道-.-</pre><pre style="line-height: normal; "><strong style="line-height: normal; ">B.使用lambda</strong></pre><pre style="line-height: normal; ">在PHP中，</pre><pre style="line-height: normal; "><font color="#339966" style="line-height: normal; "><code class="php" style="line-height: normal; ">switch ($value) {
    case ‘a‘:
        $result = $x * 5;
        break;
    case ‘b‘:
        $result = $x   7;
        break;
    case ‘c‘:
        $result = $x - 2;
        break;
}</code> </font></pre><pre style="line-height: normal; ">在python中:</pre><pre style="line-height: normal; "><font color="#3366ff" style="line-height: normal; "><code class="python" style="line-height: normal; ">result = {
  ‘a‘: lambda x: x * 5,
  ‘b‘: lambda x: x   7,
  ‘c‘: lambda x: x - 2
}[value](x)</code> </font></pre><pre style="line-height: normal; ">具体参考: <a href="http://simonwillison.net/2004/May/7/switch/" target="_blank" style="color: rgb(68, 100, 0); font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; ">Switch statements in Python</a></pre><pre style="line-height: normal; "><strong style="line-height: normal; ">C.<a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410692" target="_blank" style="color: rgb(68, 100, 0); font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; ">Brian Beck</a>提供了一个类 switch 来实现其他语言中switch的功能</strong></pre><pre style="line-height: normal; "><font color="#3366ff" style="line-height: normal; "><font color="#ff00ff" style="line-height: normal; "><span class="syntaxcomment" style="line-height: normal; "># This class provides the functionality we want. You only need to look at</span>
<span class="syntaxcomment" style="line-height: normal; "># this if you want to know how this works. It only needs to be defined</span>
<span class="syntaxcomment" style="line-height: normal; "># once, no need to muck around with its internals.</span></font></font></pre><pre style="line-height: normal; "><font color="#3366ff" style="line-height: normal; "><span class="syntaxkeyword" style="line-height: normal; ">class</span> <span class="syntaxname" style="line-height: normal; ">switch</span>(object):
    <span class="syntaxkeyword" style="line-height: normal; ">def</span> __init__(self, value):
        self.value = value
        self.fall = False

    <span class="syntaxkeyword" style="line-height: normal; ">def</span> __iter__(self):
        <span class="syntaxstring" style="line-height: normal; ">"""Return the match method once, then stop"""</span>
        yield self.match
        <span class="syntaxkeyword" style="line-height: normal; ">raise</span> StopIteration

    <span class="syntaxkeyword" style="line-height: normal; ">def</span> match(self, *args):
        <span class="syntaxstring" style="line-height: normal; ">"""Indicate whether or not to enter a case suite"""</span>
        <span class="syntaxkeyword" style="line-height: normal; ">if</span> self.fall <span class="syntaxkeyword" style="line-height: normal; ">or</span> <span class="syntaxkeyword" style="line-height: normal; ">not</span> args:
            <span class="syntaxkeyword" style="line-height: normal; ">return</span> True
        <span class="syntaxkeyword" style="line-height: normal; ">elif</span> self.value <span class="syntaxkeyword" style="line-height: normal; ">in</span> args: <span class="syntaxcomment" style="line-height: normal; "># changed for v1.5, see below</span>
            self.fall = True
            <span class="syntaxkeyword" style="line-height: normal; ">return</span> True
        <span class="syntaxkeyword" style="line-height: normal; ">else</span>:
            <span class="syntaxkeyword" style="line-height: normal; ">return</span> False

</font><font color="#ff00ff" style="line-height: normal; "><span class="syntaxcomment" style="line-height: normal; "># The following example is pretty much the exact use-case of a dictionary,</span>
<span class="syntaxcomment" style="line-height: normal; "># but is included for its simplicity. Note that you can include statements</span>
<span class="syntaxcomment" style="line-height: normal; "># in each suite.</span>
</font><font color="#3366ff" style="line-height: normal; ">v = <span class="syntaxstring" style="line-height: normal; ">‘ten‘</span>
<span class="syntaxkeyword" style="line-height: normal; ">for</span> case <span class="syntaxkeyword" style="line-height: normal; ">in</span> switch(v):
    <span class="syntaxkeyword" style="line-height: normal; ">if</span> case(<span class="syntaxstring" style="line-height: normal; ">‘one‘</span>):
        <span class="syntaxkeyword" style="line-height: normal; ">print</span> 1
        <span class="syntaxkeyword" style="line-height: normal; ">break</span>
    <span class="syntaxkeyword" style="line-height: normal; ">if</span> case(<span class="syntaxstring" style="line-height: normal; ">‘two‘</span>):
        <span class="syntaxkeyword" style="line-height: normal; ">print</span> 2
        <span class="syntaxkeyword" style="line-height: normal; ">break</span>
    <span class="syntaxkeyword" style="line-height: normal; ">if</span> case(<span class="syntaxstring" style="line-height: normal; ">‘ten‘</span>):
        <span class="syntaxkeyword" style="line-height: normal; ">print</span> 10
        <span class="syntaxkeyword" style="line-height: normal; ">break</span>
    <span class="syntaxkeyword" style="line-height: normal; ">if</span> case(<span class="syntaxstring" style="line-height: normal; ">‘eleven‘</span>):
        <span class="syntaxkeyword" style="line-height: normal; ">print</span> 11
        <span class="syntaxkeyword" style="line-height: normal; ">break</span>
    <span class="syntaxkeyword" style="line-height: normal; ">if</span> case(): <span class="syntaxcomment" style="line-height: normal; "># default, could also just omit condition or ‘if True‘</span>
        <span class="syntaxkeyword" style="line-height: normal; ">print</span> <span class="syntaxstring" style="line-height: normal; ">"something else!"</span>
        <span class="syntaxcomment" style="line-height: normal; "># No need to break here, it‘ll stop anyway</span></font></pre></span>
]]></description>
</item>
</channel>
</rss>