_php techniques for simplexml_load_file function usages in PHP

Source: Internet
Author: User

This article illustrates the use of Simplexml_load_file functions in PHP. Share to everyone for your reference. The specific usage analysis is as follows:

After the simplexml_load_file () function in PHP loads an XML document into an object, we can use the object returned by this function to do the relevant operations, and we'll look at several test instances.

example, the XML file code is as follows:

Copy Code code as follows:
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<note>
<to>George</to>
<from>John</from>

<body>don ' t forget the meeting!</body>
</note>

The PHP code is as follows:
Copy Code code as follows:
<?php
if (file_exists (' Test.xml '))
{
$xml = simplexml_load_file (' Test.xml ');
Var_dump ($xml);
}
Else
{
Exit (' Error. ');
}
?>


The results of running the output are as follows:
Copy Code code as follows:

Object (SimpleXMLElement) #1 (4) {
["To"]=>
String (6) "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:
Copy Code code as follows:
<?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 use the following methods to get the values we want:
Copy Code code as follows:
<?php
$xmldata = simplexml_load_file ("Iciba.xml");

Header ("content-type:text/html; Charset=utf-8 ");
Print_r ($xmldata); First part

$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:

Copy Code code as follows:

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:

Copy Code code as follows:

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 more in-depth traversal of the output generated form, the code is as follows:

Copy Code code as follows:
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> ";
}

I hope this article will help you with your PHP program design.

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.