How to use Simplexml_load_file function in PHP

Source: Internet
Author: User
Tags foreach php language php code

Example

XML file:

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<body>don ' t forget the meeting!</body>
</note>

PHP Code:

<?php
if (file_exists (' Test.xml '))
{
$xml = simplexml_load_file (' Test.xml ');
Var_dump ($xml);
}

Else
{
Exit (' Error. ');
}
?>

Output:

Object (SimpleXMLElement) #1 (4)
{
["To"]=> string (4) "George"
[' From ']=> string (4) ' John '
["Heading"]=> string (8) "Reminder"
["Body"]=> string "Don t forget the meeting!"
}

If there is a "iciba.xml" document, its contents are as follows:

The code is as follows Copy Code

<?xml version= "1.0" encoding= "UTF-8"?>
<dict num= "219" id= "219" name= "219" >
<key> Sky </key>
<pos></pos>
<acceptation>Array;Array;</acceptation>
<sent>
<orig>the church tower stood against the sky like a finger pointing towards
The spire of the <trans> church is like a finger pointing to the sky against the backdrop of the sky. </trans>
</sent>
<sent>
<orig>a balloon floated across the sky.</orig>
<trans> balloons float across the sky. </trans>
</sent>
<sent>
<orig>a bolt of lightning lit up the sky.</orig>
<trans> (a) lightning lights up the sky. </trans>
</sent>
<sent>
<orig>a Bright Moving object appeared in the sky at sunset.</orig>
<trans> Sunset, a moving shiny object appeared in the sky. </trans>
</sent>
<sent>
<orig>a Bright Rainbow Arched above.</orig>
<trans> a bright rainbow hangs in the sky. </trans>
</sent>
</dict> in the PHP language we can get the values we want in the following ways:

<?php
$xmldata = simplexml_load_file ("Iciba.xml");

Header ("content-type:text/html; Charset=utf-8 ");
Print_r ($xmldata); The first part www.111cn.net

$listcount = count ($xmldata->sent);

for ($i =0; $i < $listcount; $i + +) {//Part II
$dictlist = $xmldata->sent[$i];
echo "<br/> Example:". $dictlist->orig;
echo "<br/> Translation:". $dictlist->trans;
}
?> the "first part" will output:

SimpleXMLElement Object
(
[@attributes] => Array
(
[Num] => 219
[ID] => 219
[Name] => 219
)

[Key] => Sky
[POS] => simplexmlelement Object
(
)

    [acceptation] => array;array
    [sent] => Array
         (
            [0] => SimpleXMLElement Object
                 (
                     [orig] => the church tower stood against the sky like a finger pointing towards.
                     [Trans] the steeple of the => church is like a finger pointing to the sky against the sky.
               )

            [1] => simplexmlelement Object
                 (
                     [orig] => A Balloon floated across the sky.
                     [trans] => balloons float across the sky.
               )

            [2] => simplexmlelement Object
                 (
                     [orig] => A Bolt of lightning lit up the sky.
                     [Trans] => (together) lightning illuminates the sky.
               )

            [3] => simplexmlelement Object
                 (
                     [orig] => A Bright moving object appeared in the sky at sunset.
                     [Trans] => sunset, a moving shiny object appeared in the sky.
               )

[4] => simplexmlelement Object
(
[orig] => A bright rainbow arched above.
[Trans] => a bright rainbow hangs in the sky.
)

)

"Part Two" will output:

Example: The church tower stood against the sky like a finger pointing towards.
The steeple of the church is like a finger pointing to the sky against the sky.
Example: A balloon floated across the sky.
The balloon floats across the sky.
Example: A bolt of lightning lit up the sky.
Lightning lit up the sky.
Example: A Bright Moving Object appeared the sky at sunset.
Translation: Sunset, a moving shiny object appeared in the sky.
Example: A bright rainbow arched above.
A bright rainbow hangs in the sky.

example, a deeper traversal of the output generation table

The code is as follows Copy Code

Eader ("content-type:text/html; Charset=utf-8 "); Set encoding
$xml = simplexml_load_file (' A.xml '); Loading an XML file $lists is the same as the root node of an XML file
echo $xml->company. " <br> ";
echo $xml->town. " <br>id: ";
echo $xml->town[' id ']. " <br>parent: ";
echo $xml->town[' parent ']. " <br> ";

echo "<br> cyclic read:<br>";
foreach ($xml->user as $users) {//has multiple user, gets the array, loops the output
echo "-------------------<br>";
echo "Name:". $users->name. " <br> ";
echo "Number:". $users->age. " <br> ";
echo "Gender:" $users->age[' sex ']. " <br> ";
echo "Serial number:". $users->height. " <br> ";
}

echo "<br> cyclic read:<br>";
foreach ($xml->town as $towns) {//has multiple user, gets the array, loops the output
echo "-------------------<br>";
echo "ID:". $towns [' id ']. " <br> ";
echo "Attribution:" $towns [' parent ']. " <br> ";
echo "region:". $towns. " <br> ";
}

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.