PHP XML processing XML code is as follows
| The code is as follows |
Copy Code |
<?xml version= "1.0" encoding= "UTF-8"?> <!--edited with XML Spy v2007 (http://www.altova.com)--> <breakfast_menu> <food id= "1" > <name>belgian waffles</name> <price>$5.95</price> <description>two famous Belgian waffles with plenty to real maple syrup</description> <calories>650</calories> </food> <food id= "2" > <name>strawberry Belgian waffles</name> <price>$7.95</price> <description>light Belgian waffles covered with strawberries whipped and cream</description> <calories>900</calories> </food> <food id= "3" > <name>berry-berry Belgian waffles</name> <price>$8.95</price> <description>light Belgian Waffles covered with an assortment of fresh berries and whipped <calories>900</calories> </food> </breakfast_menu> |
PHP resolves XML code
| The code is as follows |
Copy Code |
| <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/> <title> Use simplexml processing xml</title>
<body> <table border= "1" cellpadding= "0" cellspacing= "0" width= ">" </tbody> <TR bgcolor= "Green" > <th width= "5%" > </th> <th width= "20%" >name</th> <th width= "10%" >price</th> <th width= "55%" >description</th> <th width= "10%" >calories</th> </tr> <?php Using SimpleXML to process XML $xml = Simplexml_load_file ('./simple.xml '); Var_dump ($xml); echo $xml->getname (); Var_dump ($xml->children ()); $record = '; foreach ($xml->children () as $child) { $record. = ' <tr><td> '. $child->attributes (). ' </td> '; foreach ($child->children () as $item) { Var_dump ($child); $record. = ' <td> '. $item. ' </td> '; } $record. = ' </tr> '; } Echo $record; ?> </tbody> </table> </body>
|
converting XML to an array structure
| The code is as follows |
Copy Code |
| Private Function Change_simple_xml_to_array ($obj) { $i = 0; Start traversal from the two-tier child node under the root node foreach ($obj->children () as $value) { If a sibling, same tag name occurs, a one-dimensional array is added; if (Isset ($last _name) && $value->getname () = = $last _name) { The second dimension is pushed into the second-dimensional array if the same name is found. if ($i ==1) $arr [$last _name]=array ($arr [$last _name]); traversing child nodes; Array_push ($arr [$last _name], $this->change_simple_xml_to_array ($value)); }else{ $last _name = $value->getname (); if (Count ($value->children ()) ==0) $arr [$last _name] = $value. ""; else $arr [$last _name] = $this->change_simple_xml_to_array ($value); } $i + +; } return $arr; } —————— XML Document —————— <?xml version= "1.0" encoding= "Utf-8"?> <use> <login id= "1" > <user>bddg</user> <pas>abcdefg</pas> <permission><fdsa>5</fdsa></permission> <online_time>2</online_time> </login> <login id= "2" > <user>baidongdaogu</user> <pas>aaa</pas> <permission>1</permission> <online_time>2</online_time> </login> <login id= "3" > <user>baidongdaogu</user> <pas>aaa</pas> <permission>1</permission> <online_time>2</online_time> </login> </use> ———— the converted array ———— Array ( [Login] => Array ( [0] => Array ( [User] => BDDG [Pas] => ABCDEFG [Permission] => Array ( [FDSA] => 5 ) [Online_time] => 2 ) [1] => Array ( [User] => Baidongdaogu [Pas] => AAA [Permission] => 1 [Online_time] => 2 ) [2] => Array ( [User] => Baidongdaogu [Pas] => AAA [Permission] => 1 [Online_time] => 2 ) ) ) |