Easily parse XML documents with PHP 5.0 (2)

Source: Internet
Author: User
Tags abstract foreach array key return version trim
XML file: simpledocumentparser.php

<?php/** *========================================================= * * @author Hahawen (older Youth) * @since 2004-12- @copyright Copyright (c), Nxcoder Group * *========================================================= *//** * CL Ass Simpledocumentparser * Use SAX parse XML file, and builds Simpledocumentobject * All of this pachage ' s is work for XML fil E, and is action as DOM. * @package SmartWeb.common.xml * @version 1.0 */class simpledocumentparser{Private $dom Rootobject = null; Private $currentNO = null; Private $currentName = null; Private $currentValue = null; Private $currentAttribute = null; Public Function getsimpledocument () {return $this->domrootobject.} Public Function Parse ($file) {$xmlParser     = Xml_parser_create ();     Xml_parser_set_option ($xmlParser, xml_option_case_folding, 0);     Xml_parser_set_option ($xmlParser, Xml_option_skip_white, 1);     Xml_parser_set_option ($xmlParser, xml_option_target_encoding, ' UTF-8 '); Xml_set_object ($XMLparser, $this);     Xml_set_element_handler ($xmlParser, "startelement", "endelement");        Xml_set_character_data_handler ($xmlParser, "characterdata"); if (!xml_parse ($xmlParser, file_get_contents ($file)) Die (sprintf ("XML error:%s at line%d", xml_error_string ( Xml_get_error_code ($xmlParser)),
Xml_get_current_line_number ($xmlParser))); Xml_parser_free ($xmlParser); Private Function Startelement ($parser, $name, $attrs) {$this->currentname = $name; $this->currentattribute = $attrs; if ($this->currentno = = null) {$this->domrootobject = new Simpledocumentroot ($name); $this->currentno = $this->domrootobject; else {$this->currentno = $this->currentno->createnode ($name, $attrs); The Private function endelement ($parser, $name) {if ($this->currentname== $name) {$tag = $this ->currentno->getseq (); $this->currentno = $this->currentno->getpnodeobject (); if ($this->currentattribute!=null && sizeof ($this->currentattribute) >0) $this->currentno->setvalue ($name, Array (' value ' => $this->currentvalue,
' Attrs ' => $this->currentattribute)); else $this->currentno->setvalue ($name, $this->currentvalue); $this->currentno->removenode ($tag); else {$this->currentno = (is_a ($this->currentno, ' simpledocumentroot '))? Null:
$this->currentno->getpnodeobject (); The Private function Characterdata ($parser, $data) {$this->currentvalue = iconv (' UTF-8 ', ' GB2312 ', $data);} funct Ion __destruct () {unset ($this->domrootobject);}? >

File: simpledocumentbase.php

<?php/** *========================================================= * * @author Hahawen (older Youth) * @since 2004-12- @copyright Copyright (c), Nxcoder Group * *========================================================= *//** * AB Stract Class Simpledocumentbase * base class FOR XML file parse * All this pachage ' are work for XML file, and method is a Ction as DOM. * * 1\ add/update/remove data of XML file. * 2\ explode data to array. * 3\ Rebuild XML file * * @package SmartWeb.common.xml * @abstract * @version 1.0 */abstract Class simpledocumentbase{pri Vate $nodeTag = null; Private $attributes = Array (); Private $values = Array ();    Private $nodes = Array ();    function __construct ($nodeTag) {$this->nodetag = $nodeTag;    The Public Function Getnodetag () {return $this->nodetag;    The Public Function setvalues ($values) {$this->values = $values;    The Public Function SetValue ($name, $value) {$this->values[$name] = $value; } Public FunCtion GetValue ($name =null) {return $name ==null $this->values: $this->values[$name];    The Public Function RemoveValue ($name) {unset ($this->values["$name"));    The Public Function SetAttributes ($attributes) {$this->attributes = $attributes;    The Public Function setattribute ($name, $value) {$this->attributes[$name] = $value;     The Public Function getattribute ($name =null) {return $name ==null? $this->attributes: $this->attributes[$name];    The Public Function removeattribute ($name) {unset ($this->attributes["$name"));    The Public Function getnodessize () {return sizeof ($this->nodes);    } protected function Setnode ($name, $nodeId) {$this->nodes[$name] = $nodeId;    public abstract function CreateNode ($name, $attributes);    public abstract function Removenode ($name);    public abstract function GetNode ($name =null); protected function Getnodeid ($name =null) {return $name ==null? $this->nodes:$this->nodes[$name]; } protected function Createnodebyname ($ROOTNODEOBJ, $name, $attributes, $pId) {$tmpObject = $ROOTNODEOBJ->CR        Eatenodeobject ($pId, $name, $attributes); $key = Isset ($attributes [id])? $name. ' _ '. $attributes [id]: $name. '        _ '. $this->getnodessize ();     $this->setnode ($key, $tmpObject->getseq ());    return $tmpObject; } protected function Removenodebyname ($ROOTNODEOBJ, $name) {$rootNodeObj->removenodebyid ($this->getnodeid ($n        AME));     if (sizeof ($this->nodes) ==1) $this->nodes = Array ();    else unset ($this->nodes[$name]); } protected function Getnodebyname ($ROOTNODEOBJ, $name =null) {if ($name ==null) {$tmpList = array            ();            $tmpIds = $this->getnodeid ();            foreach ($tmpIds as $key => $id) $tmpList [$key] = $ROOTNODEOBJ->getnodebyid ($id);     return $tmpList;      else {$id = $this->getnodeid ($name);             if ($id ===null) {$tmpIds = $this->getnodeid (); foreach ($tmpIds as $tkey => $tid) {if (Strpos ($key, $name) ==0) {$id = $               Tid              Break     Return $ROOTNODEOBJ->getnodebyid ($id);     The Public Function Findnodebypath ($path) {$pos = Strpos ($path, ' | ');        if ($pos <=0) {return $this->getnode ($path);         else {$tmpObj = $this->getnode (substr ($path, 0, $pos)); Return Is_object ($TMPOBJ)?        $TMPOBJ->findnodebypath (substr ($path, $pos + 1)): null;        The Public Function Getsavedata () {$data = $this->values;        if (sizeof ($this->attributes) >0) $data [attrs] = $this->attributes;        $nodeList = $this->getnode ();        if ($nodeList ==null) return $data;        foreach ($nodeList as $key => $node) {$data [$key] = $node->getsavedata ();    return $data; The Public function Getsavexml ($level=0) {$prefixSpace = Str_pad ("", $level, "T");        $str = "$prefixSpace $this->nodetag";        foreach ($this->attributes as $key => $value) $str. = "$key =\" $value \ "";        $str. = ">\r\n"; foreach ($this->values as $key => $value) {if (Is_array ($value)) {$str. = "$prefixSpace \                t< $key ";                foreach ($value [attrs] as $attkey => $attvalue) $str. = "$attkey =\" $attvalue \ "";            $TMPSTR = $value [value];             else {$str. = "$prefixSpace \t< $key";            $TMPSTR = $value;            $TMPSTR = Trim (Trim ($tmpStr, "\ r \ n")); $str. = ($tmpStr ===null | | $tmpStr = = "")?        "/>\r\n": "> $tmpStr </$key >\r\n"; foreach ($this->getnode () as $node) $str. = $node->getsavexml ($level + 1).     \ r \ n ";     $str. = "$prefixSpace </$this->nodetag>";    return $str; function __destruct () {unset ($this->nodes, $this->attribUtes, $this->values);       }}?>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.