Php + xml programming-SimpleXML application example

Source: Internet
Author: User
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.

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.