PHP simplexml_load_file function using _php tutorial

Source: Internet
Author: User
Example

XML file:

The code is as follows



George
John
Reminder
Don ' t forget the meeting!

PHP Code:

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" file, its contents are as follows:

The code is as follows



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:

$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 "
Example: ". $dictlist->orig;
echo "
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 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:

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.

Example, a more in-depth traversal of the output generated table

The code is 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. "
";
}

http://www.bkjia.com/PHPjc/733192.html www.bkjia.com true http://www.bkjia.com/PHPjc/733192.html techarticle example XML file: Code as follows? XML version=1.0 encoding=iso-8859-1 Note togeorge/to fromjohn/from headingreminder/heading bodyDon ' T forget the Meeting!/body/note PHP code: ...

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