This article mainly introduces simplexml_load_file function usage in php, and describes in detail how simplexml_load_file function reads XML files in the form of an instance, which is very useful, for more information about how to use simplexml_load_file in php, see the following example. Share it with you for your reference. The usage analysis is as follows:
In php, after the simplexml_load_file () function loads the XML document into an object, we can use the objects returned by the function to perform related operations. let's take a look at several test instances.
For example, the XML file code is as follows:
The 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:
The code is as follows:
<? Php
If (file_exists ('test. XML '))
{
$ Xml = simplexml_load_file ('test. XML ');
Var_dump ($ xml );
}
Else
{
Exit ('error .');
}
?>
The output result is as follows:
The code is as follows:
Object (SimpleXMLElement) #1 (4 ){
["To"] =>
String (6) "George"
["From"] =>
String (4) "John"
["Heading"] =>
String (8) "Reminder"
["Body"] =>
String (25) "Don't forget the meeting! "
}
Assume that there is an "ICBA. xml" file with the following content:
The code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
Sky
Array;
The church tower stood against the sky like a finger pointing towards heaven.
The tower of the church is like a finger pointing to the sky.
A balloon floated into SS the sky.
The balloon floated across the sky.
A bolt of lightning pointer up the sky.
(A) lightning illuminates the sky.
A bright moving object appeared in the sky at sunset.
A moving shiny object appeared in the sky.
A bright rainbow arched ABVE.
A bright rainbow hangs in the sky.
In PHP, we can use the following method to obtain the desired value:
The code is as follows:
<? Php
$ Xmldata = simplexml_load_file ("ICBA. 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 ++) {// The second part
$ Dictlist = $ xmldata-> sent [$ I];
Echo"
Example: ". $ dictlist-> orig;
Echo"
Translation: ". $ dictlist-> trans;
}
?>
"Part 1" will output:
The code is as follows:
SimpleXMLElement Object
(
[@ Attributes] => Array
(
[Num] = & gt; 219
[Id] = & gt; 219
[Name] = & gt; 219
)
[Key] => sky
[Pos] => SimpleXMLElement Object
(
)
[Acceptation] => Array;
[Sent] => Array
(
[0] => SimpleXMLElement Object
(
[Orig] => The church tower stood against the sky like a finger pointing towards heaven.
[Trans] => The Tower of the church is like a finger pointing to the sky under the background of the sky.
)
[1] => SimpleXMLElement Object
(
[Orig] => A balloon floated login SS the sky.
[Trans] => balloons floated across the sky.
)
[2] => SimpleXMLElement Object
(
[Orig] => A bolt of lightning setting up the sky.
[Trans] => lightning strikes the sky.
)
[3] => SimpleXMLElement Object
(
[Orig] => A bright moving object appeared in the sky at sunset.
[Trans] => a moving shiny object appeared in the sky at sunset.
)
[4] => SimpleXMLElement Object
(
[Orig] => A bright rainbow arched above.
[Trans] => a bright rainbow hangs in the sky.
)
)
)
"Part 2" will output:
The code is as follows:
Example: The church tower stood against the sky like a finger pointing towards heaven.
The tower of the church is like a finger pointing to the sky.
Example: A balloon floated into SS the sky.
Translation: balloons floated across the sky.
Example: A bolt of lightning sums up the sky.
(A) lightning illuminates the sky.
Example: A bright moving object appeared in the sky at sunset.
A moving shiny object appeared in the sky when the sun fell down.
Example: A bright rainbow arched above.
A bright rainbow hangs in the sky.
For example, the code is as follows:
The code is as follows:
Eader ("content-type: text/html; charset = utf-8"); // sets the encoding
$ Xml = simplexml_load_file ('A. XML'); // load the xml file $ the lists and xml file root nodes are the same
Echo $ xml-> company ."
";
Echo $ xml-> town ."
Id :";
Echo $ xml-> town ['id']."
Parent :";
Echo $ xml-> town ['parent']."
";
Echo"
Loop read:
";
Foreach ($ xml-> user as $ users) {// There are multiple users, get an array, and output cyclically
Echo "-------------------
";
Echo "name:". $ users-> name ."
";
Echo "No.:". $ users-> age ."
";
Echo "gender:". $ users-> age ['sex']."
";
Echo "No.:". $ users-> height ."
";
}
Echo"
Loop read:
";
Foreach ($ xml-> town as $ towns) {// There are multiple users, get an array, and output cyclically
Echo "-------------------
";
Echo "id:". $ towns ['id']."
";
Echo "attribution:". $ towns ['parent']."
";
Echo "region:". $ towns ."
";
}
I hope this article will help you with PHP programming.