This article mainly introduces the application of SimpleXML in php + xml programming. The example shows how SimpleXML functions operate XML files, for more information about SimpleXML programming in php and xml, see the following example. Share it with you for your reference. The details are as follows:
SimpleXML's core idea is to operate xml files in an object-oriented manner, which converts all elements of the xml file into objects.
Xml document: words. xml
The code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
Boy
Boy
Girl
Girl
Teacher
Instructor
Beauty
Beauty
Simplexml example:
The code is as follows:
<? Php
Echo"
";
$ Words = simplexml_load_file ("words. xml"); // returns an array object, which can be viewed using print_r () or var_dump ().
Var_dump ($ words );
?>Read content:
The code is as follows:
<? Php
Echo"
";
$ Words = simplexml_load_file ("words. xml"); // returns an array object, which can be viewed using print_r () or var_dump ().
// Echo $ words-> word [2];
Foreach ($ words-> word as $ row) {// $ row is still an object
Print_r ($ row );
Echo $ row-> ch. ""; // In fact, $ row-> ch is still an object, but it cannot be echo
}
?>
Output result of the second code:
The code is as follows:
SimpleXMLElement Object
(
[En] => boy
[Ch] => boy
)
Boy
SimpleXMLElement Object
(
[En] => girl
[Ch] => Girl
)
Girl
SimpleXMLElement Object
(
[En] => teacher
[Ch] => instructor
)
Instructor
I hope this article will help you with php + xml programming.