How to use PHP's SimpleXML extension?

WBOY
Release: 2023-06-01 14:32:02
Original
1753 people have browsed it

PHP's SimpleXML extension is a tool for parsing and generating XML documents. In applications, it is often used to interact with web services, such as getting data from or sending data to web services.

In this article, we’ll cover how to use PHP’s SimpleXML extension and provide some sample code to help you get started with it.

Step 1: Load XML file

First, let’s see how to use the SimpleXML extension to load an XML file.


 Tom 20   Mike 25 
Copy after login

$xml = simplexml_load_file('test.xml');
print_r($xml);
?>

Execute the script, we will get the following output:

SimpleXMLElement Object
(

[person] => Array ( [0] => SimpleXMLElement Object ( [name] => Tom [age] => 20 ) [1] => SimpleXMLElement Object ( [name] => Mike [age] => 25 ) )
Copy after login

)

SimpleXML extension function simplexml_load_file() loads the XML file into a SimpleXMLElement object, which contains all elements in the XML file. In the above example, we print out the SimpleXMLElement object, which displays the elements and attributes in the XML file in the form of an array.

Step 2: Access XML elements

Next, we will demonstrate how to use the SimpleXML extension to access XML elements.

Using the SimpleXML extension, we can access XML elements and attributes through array items or object properties. For example, in the above example, we can access the first person's name like this:

echo $xml->person[0]->name;

This will output the following Content:

Tom

We can also access everyone's names and store them in an array:

foreach ($xml->person as $person ) {

$names[] = (string) $person->name;
Copy after login

}
print_r($names);

This will output the following:

Array
(

[0] => Tom [1] => Mike
Copy after login

)

In the above example, we use a foreach loop to loop through everyone, store each person's name in an array, and use cast to convert the SimpleXMLElement object to a string.

Step 3: Modify XML elements

Using the SimpleXML extension, we can also modify XML elements and attributes. For example, in the example above, we could change the first person's age to 21 and update the XML file:

$xml->person[0]->age = 21;
$xml->asXML('test.xml');

In the above example, we change the age of the first person from 20 to 21 and use the asXML() function of the SimpleXMLElement object to The updated XML file is saved back to the original folder.

Step 4: Create XML files

In addition to loading and modifying XML files, we can also use the SimpleXML extension to create new XML files. For example, the following script uses the SimpleXML extension to create a new XML file containing two elements and one attribute.

$dom = new DOMDocument();
$dom->formatOutput = true;

$persons = $dom->createElement(' persons');
$person1 = $dom->createElement('person');
$person2 = $dom->createElement('person');

$name1 = $ dom->createElement('name', 'Tom');
$age1 = $dom->createElement('age', '20');
$name2 = $dom->createElement( 'name', 'Mike');
$age2 = $dom->createElement('age', '25');

$person1->appendChild($name1);
$person1->appendChild($age1);
$person2->appendChild($name2);
$person2->appendChild($age2);

$persons-> ;appendChild($person1);
$persons->appendChild($person2);

$xml = $dom->appendChild($persons);
$xml->setAttribute ('type', 'people');

echo $dom->saveXML();
?>

Execute the script and we will get the following output:



Tom 20
Copy after login


Mike 25
Copy after login


In the above example, we created the XML using the DOMDocument object file and then use the created DOM elements to build the XML file.

Summary

In this article, we introduced the SimpleXML extension for PHP and provided some sample code to help you get started using it. Using the SimpleXML extension, we can easily load, access, modify and create XML files. The SimpleXML extension is a very useful tool if you need to interact with XML files in your application.

The above is the detailed content of How to use PHP's SimpleXML extension?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!