Analyze a simple PHP class with XML documents

xiaoxiao2021-04-10  360

attribute = array (); $ this-> child = array (); $ this-> depth = 0; $ this-> parent = NULL; $ this-> Data = '; $ this-> tagname = $ tag;} function settagname ($ tag) {$ this-> tagname = $ TAG } Function setParent ($ this-> parent = & $ parent;} function setttribute ($ this-> attribute) {$ this-> attribute [$ name] = $ value;} Function Appendchild (& $ Child) {$ i = count ($ this-> child); $ this-> child [$ i] = & $ child;} Function setData ($ data) {$ this-> data = $ data;} function getattr ) {RETURN $ this-> attribute;} function getproperty ($ name) {return $ this-> attribute [$ name];} Function getData () {RETURN $ this-> data;} function getParent () {RETURN $ THIS -> parent;} function getchild () {RETURN $ this-> child;} Function getChildbyName ($ name) {$ Total = Count ($ this-> child); for ($ I = 0; $ i <$ TOTAL; $ I ) {IF ($ TH) IS-> Child [$ I] -> attribute ['name'] == $ name) {return $ this-> child [$ I];}} return null;} // Get a TAG node function getElementsBytagname ($ Tag) {$ Vector = array (); $ tree = & $ this; $ this -> _ getElementBytagname ($ TREE, $ TAG, $ Vector); Return $ Vector;} Function _GetElementBytagname ($ TREE, $ TAG, & $ Vector ) {IF ($ TREE-> Tagname == $ tag) Array_Push ($ Vector, $ Tree); $ TOTAL = Count ($ Tree-> Child); for ($ I = 0; $ i <$ TOTAL; $ I ) $ This -> _ getElementBytagname ($ TREE->

Child [$ I], $ TAG, $ Vector);}} // XML document parsing class xmldoc {var $ parse; // xml parsing pointer VAR $ xmltree; // Generated XML tree var $ xmlfile; // will be resolved XML document var $ xmldata; // will be parsed XML data var $ error; // Error message var $ NOWTAG; // The current pointing node var $ TREEDATA; // Traverse the generated XML tree, etc., the data VAR $ maxdepth; // This tree is the largest depth Var $ encode; // XML document encoding method var $ chs; // character conversion function Xmldoc () {// adopts the default ISO-8859-1 $ this-> Parser = XML_Parser_create () ; xml_parser_set_option ($ this-> parser, XML_OPTION_CASE_FOLDING, 0); xml_set_object ($ this-> parser, & $ this); xml_set_element_handler ($ this-> parser, '_ StartElement', '_ EndElement'); xml_set_character_data_handler ($ this-> Parser, '_ cdata'); $ this-> stack = array (); $ this-> xmltree = null; $ this-> nowtag = null; $ this-> maxdepth = 0;} function loadFromfile ($ file) {$ this-> xmlfile = fopen ($ file, 'r'); if (! $ this-> xmlfile) {$ this-> error = 'can't open XML file'; return false;} $ this-> xmldata = '' $ This-> treedata = '; return true;} Function setXmldata ($ data) {if ($ this-> xmlfile) fclose ($ this-> xmlfile); $ this-> xmldata = $ data; $ this- > TR Eedata = '';} // Add a new node function appendchild ($ child) {if ($ this-> xmltree == null) {$ child-> depth = 1; $ this-> xmltree = & $ child; $ this-> nowtag = & $ this-> xmltree;} else {$ i = count ($ this-> nowtag-> child); $ this-> nowtag-> child [$ I] = & $ child ; $ This-> NOWTAG; $ child-> depth = $ this-> nowtag-> depth 1; unset ($ this-> nowtag); $ this-> nowtag = & $ child; } $ This-> MaxDepth = ($ this-> maxdepth <$ this->

NOWTAG-> Depth? $ This-> NOWTAG-> Depth: $ this-> maxdepth;}

// Generate a new node Function & CreateElement ($ TAG) {$ Element = New XMLTAG ($ TAG); Return $ Element;} Function _StartElement ($ Parser, $ Element, $ attribute) {$ tag = new xmltag (); $ tag-> tagname = $ element; $ tag-> attribute = $ attribute; if ($ this-> xmltree == null) {$ tag-> parent = null; $ tag-> depth = 1; $ this-> XMLTree = & $ TAG; $ this-> nowtag = & $ tag;} else {$ i = count ($ this-> nowtag-> child); $ this-> nowtag-> child [$ I] = & $ tag ; $ TAG-> Parent = & $ THIS-> NOWTAG; $ TAG-> Depth = $ THIS-> NOWTAG-> DEPTH 1; unset ($ this-> nowtag); $ this-> nowtag = & $ TAG; } $ This-> maxdepth = ($ this-> maxdepth <$ this-> nowtag-> defth)? $ This-> NOWTAG-> Depth: $ this-> maxdepth;} function _cdata ($ paraser, $ data) { $ this-> nowtag-> data = $ data;} function _endelement ($ PARSER, $ ELEMENT) {$ PARENT = & $ this-> NOWTAG-> Parent; unset ($ this-> nowtag); $ this-> nowtag = & $ PARENT;} // Start parsing XML document Function Parse () {if ($ this-> xmlfile) {$ this-> xmldata = '; while (! feof ($ this-> xmlfile) {$ THIS -> xmldata. = Fread ($ this-> XMLFILE, 4096);}} fclose ($ this-> xmlfile); if ($ this-> xmldata) {// $ this-> JudgeEncode (); if (! Xml_parse ($ this-> Parser, $ this-> XMLData)) {$ code = xml_get_error_code ($ this-> parser); $ col = xml_get_current_column_number ($ this-> parser); $ line = xml_get_current_line_number ($ this-> parser); $ this-> error = "XML error: $ COL at Line $ Line: ". XML_ERROR_STRING ($ code); Return False;}} XML_Parser_Free ($ this-> paser);

Return True;} // Determine the encoding method function JudgeEncode () {$ start = STRPOS ($ this-> xmldata, ' xmlData, '>); $ STR = SUBSTR ($ THIS-> XMLData, $ START, $ END- $ Start); $ POS = STRPOS ($ STR, 'Encoding'); if ($ POS! == false) {$ str = Substr ($ Str, $ POS); $ POS = STRPOS ($ STR, '='); $ Str = Substr ($ Str, $ POS 1); $ POS = 0; While (Empty ($ STR [$ POS]) $ POS $ This-> eNCode = ''; while (! Empty ($ STR [$ POS]) && $ STR [$ POS]! = '?) {IF ($ STR [$ POS]! =' "'&& $ STR [$ POS]! = "'" $ this-> eNCode. = $ STR [$ POS]; $ POS ;}} $ this-> chs = new Chinese (' UTF-8 ', $ this-> Encode);} // According to the node name, the value of a node is modified Function ChangeValuebyName ($ Name, $ Name, $ Value) {Return $ this -> _ ChangeValuebyName ($ this-> Xmltree, $ name, $ value);} function _ChangeValuebyName ($ Tree, $ Name, $ VALUE) {if ($ Tree-> Attribute) {While ($ Tree-> Attribute) {IF ($ K = 'Name' && $ V = $ NAME) {$ TREE-> DATA = $ value; return true;}}} $ TOTAL = Count ($ Tree-> Child); for ($ I = 0; $ TOTAL; $ I ) {$ Result = $ THIS -> _ ChangeValuebyName ($ I), $ Name, $ Value; if ($ results == true) Break;} Return $ Result;

// According to the node name to modify the property of a node in the tree ($ Name, $ Attr, $ Value) {Return $ this -> _ ChangeAttrbyName ($ THIS-> XMLTree, $ Name, $ Attr, $ Value);} Function _ChangeAttrbyName (& $ TREE, $ Name, $ Attr, $ Value) {if ($ Tree-> Attribute)) {While ($ K, $ V) = Each ($ Tree-> Atttibute)) { IF ($ K == 'Name' && $ V == $ Name) {$ Tree-> Attribute [$ attr] = $ value; return true;}}} $ total = count ($ Tree-> Child); for ($ I = 0; $ i ) {$ Result = $ this -> _ ChangeAttrbyName ($ Tree-> CHild [$ I], $ Name, $ Attr, $ Value; if ($ Result = = true) Break;} Return $ Result;} // Get root node function getDocumentElement () {return $ this-> xmltree;} // Traverse the generated XML tree, regenerate XML document Function WalkTree () {$ this-> Treedata = '; $ this -> _ walktree ($ this-> xmltree); return $ this-> treedata;} // recursive traversal function _walktree ($ trees) {$ this-> treedata. =' <'. $ Tree -> Tagname. ''; If (is_ARRAY ($ Tree-> Attr {While ($ key, $ value) = Each ($ TREE-> Attribute)) {$ this-> treedata. = "$ key = /" $ value / ";}} $ this-> Treedata. = '>'. $ Tree-> Data; $ TITAL = Count ($ Tree-> Child); for ($ I = 0; $ I <$ TOTAL; $ I ) {$ this -> _ WalkTree ($ Tree " -> Child [$ I]);} $ this-> treedata. = ' tagname. "> / n";} // Get Error message Function gerror () {Return $ this-> Error;

转载请注明原文地址:https://www.9cbs.com/read-133445.html

New Post(0)