/* xml操作类 */
class operxml
{
var $parser;
public function __construct()
{
$this->parser = xml_parser_create();
xml_parser_set_option($this->parser, xml_option_case_folding, 0);
xml_parser_set_option($this->parser, xml_option_skip_white, 1);
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, tag_open, tag_close);
xml_set_character_data_handler($this->parser, cdata);
}
public function parse($xmlstr=,$endtag=true)
{
$this->endtag = $endtag;
$this->xmlstr = $xmlstr;
$this->tree = new stdclass;
$this->tree->tag = root;
$this->tree->props = new stdclass;
$this->tree->children = array();
$this->tree->p = null;
$this->tree->level = -1;
$this->deep = 0;
$this->plist = array($this->tree);
xml_parse($this->parser, $this->xmlstr);
if(count($this->tree->children)>0)
$this->root = $this->tree->children[0];
else
$this->root = null;
return $this;
}
public function tag_open($parser, $tag, $attributes)
{
$o = new stdclass;
$o->p = $this->plist[$this->deep];
$o->index = count($o->p->children);
$o->level = $o->p->level 1;
$o->tag = $tag;
$o->props = new stdclass;
while(list($key,$value)=each($attributes))
$o->props->{$key} = $value;
$o->value = ;
$o->children = array();
array_push($o->p->children,$o);
$this->deep ;
$this->plist[$this->deep] = $o;
}
public function cdata($parser, $cdata)
{
$this->plist[$this->deep]->value = $cdata;
}
public function tag_close($parser, $tag)
{
$this->deep--;
}
public function getnodebyprop() // 根据属性名称和值取得节点,
{ // 参数:属性名称,属性值1,属性值2,属性值3,...
$args = func_get_args();
$node = $this->tree;
for($i=1;$i {
$node = $this->_getnodebyprop($node,$args[0],$args[$i]);
if($node==null) break;
}
return $node;
}
public function getchildbytag($node,$tag) // 取得$node节点下标签为$tag的节点
{
for($i=0;$ichildren);$i )
{
if($node->children[$i]->tag==$tag)
return $node->children[$i];
}
return null;
}
public function getchildsbytag($node,$tag) // 取得$node节点下标签为$tag的节点,返回节点列表数组
{
$rs = array();
for($i=0;$ichildren);$i )
if($node->children[$i]->tag==$tag)
array_push($rs,$node->children[$i]);
return $rs;
}
public function addroot($tag) // 添加根节点
{
$this->tree->children = array();
$this->root = $this->addchild($this->tree,$tag);
return $this->root;
}
public function addchild($node,$tag) // 在$node节点下添加标签为$tag的节点,并返回添加的节点
{
$o = new stdclass;
$o->p = $node;
$o->level = $node->level 1;
$o->index = count($node->children);
$o->tag = $tag;
$o->props = new stdclass;
$o->value = ;
$o->children = array();
array_push($node->children,$o);
return $o;
}
public function delete($node) // 删除$node节点
{
$p = $node->p;
array_splice($p->children,$node->index,1);
for($i=0;$ichildren);$i )
$p->children[$i]->index = $i;
}
public function move($dstnode,$srcnode) // 将srcnode移动到$dstnode下面
{
$this->delete($srcnode);
$srcnode->p = $dstnode;
$srcnode->level = $dstnode->level 1;
$srcnode->index = count($dstnode->children);
array_push($dstnode->children,$srcnode);
}
public function __tostring() // 返回xml格式串
{
$s = ;
for($i=0;$itree->children);$i )
$s .= $this->traversalnodetoxml($this->tree->children[$i],$this->endtag);
return $s;
}
public function save($xmlfile) // 保存成xml格式文件
{
$content = $this->__tostring();
$fp = @fopen($xmlfile,w) or die(创建文件失败:.$xmlfile);
@fwrite($fp,$content);
@fclose($fp);
@chmod($xmlfile,0777);
}
private function traversalnodetoxml($treenode,$endtag)
{
$space = ;
$space = str_pad($s,$treenode->level*2, ,str_pad_left);
$s = $space.tag;
while(list($key,$value)=each($treenode->props))
$s .= $key=$value;
$childcount = count($treenode->children);
if($childcount==0)
{
if($treenode->value!= || $endtag)
$s .= >.$treenode->value..$treenode->tag.>;
else
$s .= />;
return $s;
}
$s .= >;
for($i=0;$i $s .= $this->traversalnodetoxml($treenode->children[$i],$endtag);
$s .= $space..$treenode->tag.>;
return $s;
}
private function _getnodebyprop($node,$propname,$propvalue)
{
for($i=0;$ichildren);$i )
{
$anode = $node->children[$i];
if(isset($anode->props) && isset($anode->
http://www.bkjia.com/phpjc/486173.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/486173.htmltecharticle编程之家提供一个功能齐全的xml操作类 ? /* xml操作类 */ class operxml { var $parser; public function __construct() { $this-parser = xml_parser_create(); xml_parser...
