How to use PHP simplexml

Source: Internet
Author: User

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%" >&nbsp;</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
)
)
)

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.