Example of simplexml_load_file function usage in PHP, simplexml_php tutorial

Source: Internet
Author: User

Example of simplexml_load_file function usage in PHP, simplexml


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

In PHP, after the simplexml_load_file () function loads an XML document into an object, we can use the object returned by this function to do the related operations, and let's look at a few test examples.

example, the XML file code is as follows:
Copy CodeThe code is as follows: <?xml version= "1.0" encoding= "Iso-8859-1"?>

George
John
Reminder
Don ' t forget the meeting!

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

The results of the run output are as follows:
Copy CodeThe code is 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" file, its contents are as follows:
Copy CodeThe code is as follows: <?xml version= "1.0" encoding= "UTF-8"?>

Sky

Array;array;

The church tower stood against the sky like a finger pointing towards heaven.
The spire of the church is like a finger pointing to the sky against the sky.


A balloon floated across the sky.
The balloon floated across the sky.


A bolt of lightning lit up the sky.
(together) lightning illuminates the sky.


A Bright Moving object appeared in the sky at sunset.
Sunset, a moving shiny object appeared in the sky.


A bright rainbow arched above.
A curved bright rainbow hangs in the sky.



In the PHP language we can get the values we want in the following ways:
Copy CodeThe code is as follows: <?php
$xmldata = simplexml_load_file ("Iciba.xml");

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

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

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

The "first part" will output:
Copy the 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 heaven.
[trans] = The spire of the church is like a finger pointing to the sky against the backdrop of the sky.
)

[1] = = SimpleXMLElement Object
(
[Orig] = A balloon floated across the sky.
[Trans] = balloon floats 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.
When [trans] = sunset, a moving shiny object appeared in the sky.
)

[4] = = SimpleXMLElement Object
(
[Orig] = A bright rainbow arched above.
[trans] = a curved bright rainbow hangs in the sky.
)

)

)

"Part Two" will output:
Copy the Code code as follows:
Example: The church tower stood against the sky like a finger pointing towards heaven.
The spire of the church is like a finger pointing to the sky against the backdrop of the sky.
Example: A balloon floated across the sky.
The balloon floated across the sky.
Example: A bolt of lightning lit up the sky.
(a) lightning illuminates the sky.
Example: A Bright Moving object appeared in the sky at sunset.
Translation: When sunset, a moving shiny object appeared in the sky.
Example: A bright rainbow arched above.
Translation: A bright rainbow hangs in the sky.

For example, a more in-depth traversal of the output generated table, the code is as follows:
Copy the Code code as follows: Eader ("content-type:text/html; Charset=utf-8 "); Set encoding
$xml = simplexml_load_file (' A.xml '); Loading the XML file $lists is the same as the root node of the XML file
echo $xml->company. "
";
echo $xml->town. "
ID: ";
echo $xml->town[' id ']. "
Parent: ";
echo $xml->town[' parent '].
";

echo "
Loop read:
";
foreach ($xml->user as $users) {//has more than one user, gets an array, loops out
echo "-------------------
";
echo "Name:". $users->name. "
";
echo "Number:" $users->age. "
";
echo "Gender:". $users->age[' sex ']. "
";
echo "Serial number:" $users->height. "
";
}

echo "
Loop read:
";
foreach ($xml->town as $towns) {//has more than one user, gets an array, loops out
echo "-------------------
";
echo "ID:". $towns [' id ']. "
";
echo "Attribution:". $towns [' Parent '].
";
echo "region:". $towns. "
";
}

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/910605.html www.bkjia.com true http://www.bkjia.com/PHPjc/910605.html techarticle PHP simplexml_load_file Function Usage example, SimpleXML This example describes the Simplexml_load_file function usage in PHP. Share to everyone for your reference. The specific usage analysis is as follows: in ...

  • 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.