Example of simplexml_load_file function usage in php, simplexml
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:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "ISO-8859-1"?>
<Note>
<To> George </to>
<From> John </from>
<Heading> Reminder <Body> Don't forget the meeting! </Body>
</Note>
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 output result is 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 (25) "Don't forget the meeting! "
}
Assume that there is an "ICBA. xml" file with the following content:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Dict num = "219" id = "219" name = "219">
<Key> sky </key>
<Pos> </pos>
<Acceptation> Array; </acceptation>
<Sent>
<Orig> The church tower stood against the sky like a finger pointing towards heaven. </orig>
<Trans> the tower of the Church is like a finger pointing to the sky under the background of the sky. </Trans>
</Sent>
<Sent>
<Orig> A balloon floated guest ss the sky. </orig>
<Trans> balloons floated across the sky. </Trans>
</Sent>
<Sent>
<Orig> A bolt of lightning setting up the sky. </orig>
<Trans> lightning strikes the sky. </Trans>
</Sent>
<Sent>
<Orig> A bright moving object appeared in the sky at sunset. </orig>
<Trans> A Moving shiny object appeared in the sky when the sun fell to the Western Hill. </Trans>
</Sent>
<Sent>
<Orig> A bright rainbow arched above. </orig>
<Trans> A bright rainbow hangs in the sky. </Trans>
</Sent>
</Dict>
In PHP, we can use the following method to obtain the desired value:
Copy codeThe 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 "<br/> example:". $ dictlist-> orig;
Echo "<br/> translation:". $ dictlist-> trans;
}
?>
"Part 1" will output:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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. "<br> ";
Echo $ xml-> town. "<br> id :";
Echo $ xml-> town ['id']. "<br> parent :";
Echo $ xml-> town ['parent']. "<br> ";
Echo "<br> loop reading: <br> ";
Foreach ($ xml-> user as $ users) {// There are multiple users, get an array, and output cyclically
Echo "------------------- <br> ";
Echo "name:". $ users-> name. "<br> ";
Echo "No.:". $ users-> age. "<br> ";
Echo "Gender:". $ users-> age ['sex']. "<br> ";
Echo "No.:". $ users-> height. "<br> ";
}
Echo "<br> loop reading: <br> ";
Foreach ($ xml-> town as $ towns) {// There are multiple users, get an array, and output cyclically
Echo "------------------- <br> ";
Echo "id:". $ towns ['id']. "<br> ";
Echo "attribution:". $ towns ['parent']. "<br> ";
Echo "region:". $ towns. "<br> ";
}
I hope this article will help you with PHP programming.