2009
05-31
05-31
突然发现写程序不像原来思路那么清晰了,一个递归居然想了半天,不过还是凑合着写了一个简单的xml与array互转的函数,来实现跟其他系统的对接。
原来一直用的是拼凑的方式生成xml,这次使用了DOMDocument对象。不知道这个类是否有非法字符的问题,待测试一下便知。
- class AXML
- {
- var $dom = '';
- # array to xml ###########################################
- #array to xml
- function array2xml( $array )
- {
- $this->dom = new DOMDocument('1.0', 'utf-8');
- $this->dom->formatOutput = true;
- $this->_array( array( 'root' => $array ) , $this->dom );
- echo $this->dom->saveXML();
- }
- #array to dom
- function _array( $array = array() , $dom )
- {
- $i = 0;
- foreach( $array as $key => $value )
- {
- if( is_array( $value ) )
- {
- //遍历
- $node[$i] = $this->dom->createElement( $key );
- $dom->appendChild( $node[$i] );
- $this->_array( $value , $node[$i] );
- }
- else
- {
- $node[$i] = $this->dom->createElement( $key , $value );
- $dom->appendChild( $node[$i] );
- }
- $i++;
- }
- }
- # xml to array ###########################################
- #xml to array
- function xml2array( $xmls )
- {
- $array = array();
- $this->dom = new SimpleXMLElement( $xmls );
- print_r( $this->_object( $this->dom ) );
- }
- #object to array
- function _object( $object )
- {
- $array = array();
- $t = get_object_vars( $object );
- $i = 0;
- foreach( $t as $key => $value )
- {
- if(is_object($value))
- {
- $array[$key] = $this->_object( $value );
- }
- else
- {
- $array[$key] = $value;
- }
- $i++;
- }
- return $array;
- }
- }
- $xml = new AXML();
- $xml->array2xml( $array );
- $xml->xml2array( $xml_string );
六月 1st, 2009 at 22:56:36
xmlrpc….
六月 2nd, 2009 at 09:02:11
我做的是个php与python之间的xmlrpc,只不过自己实现比较有意思。