The application example _php technique of Php+xml programming simplexml

Source: Internet
Author: User

This paper illustrates the application of the simplexml of Php+xml programming. Share to everyone for your reference. Specifically as follows:

SimpleXML's core idea: manipulating an XML file in an object-oriented manner, which converts all elements of an XML file into objects.

XML Document: Words.xml

Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<words>
<word>
<en>boy</en>
<ch> Boy </ch>
</word>
<word>
<en>girl</en>
<ch> Girls </ch>
</word>
<word>
<en>teacher</en>
<ch> Teacher </ch>
</word>
<word>
<en>beauty</en>
<ch> Beauty </ch>
</word>
</words>

SimpleXML Use examples:
Copy Code code as follows:
<?php
echo "<pre>";
$words = simplexml_load_file ("Words.xml")//Returns an array object that can be viewed with print_r () or Var_dump ()
Var_dump ($words);
?>

Read content:

Copy Code code as follows:
<?php
echo "<pre>";
$words = simplexml_load_file ("Words.xml")//Returns an array object that can be viewed with print_r () or Var_dump ()
Echo $words->word[2];
foreach ($words->word as $row) {//$row or an object
Print_r ($row);
echo $row->ch. " }
?>

The second section of the code output results:
Copy Code code as follows:
SimpleXMLElement Object
(
[En] => boy
[CH] => Boy
)
Boy
SimpleXMLElement Object
(
[En] => girl
[CH] => Girl
)
Girl
SimpleXMLElement Object
(
[En] => teacher
[CH] => teacher
)
Teacher

I hope this article will help you with the Php+xml program design.

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.