This article mainly introduces simplexml_load_file function usage in php, and describes in detail the methods used by simplexml_load_file to read XML files in the form of instances. It is very practical and useful.
This article mainly introduces simplexml_load_file function usage in php, and describes in detail the methods used by simplexml_load_file to read XML files in the form of instances. It is very practical and useful.
This example describes the usage of simplexml_load_file in php. 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.
)