Home  >  Article  >  Backend Development  >  How to use PHP's SimpleXML extension?

How to use PHP's SimpleXML extension?

WBOY
WBOYOriginal
2023-06-01 14:31:361833browse

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.

70ce2de6a84806f08679d9fafa3cdaff
e9e1bb6e3b5d3d4b1348b0243e24bc37

<person>
    <name>Tom</name>
    <age>20</age>
</person>
<person>
    <name>Mike</name>
    <age>25</age>
</person>

b8ee975456a9852fac1328a9d6bf75ed

f2c27b2124589d9a9c8f2f6fcda2f3b8

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
            )
    )

)

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;

}
print_r($names);

This will output the following:

Array
(

[0] => Tom
[1] => Mike

)

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.

5b24f4f87e3461e0c9af32b440fdc3eaformatOutput = 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:

52b189f45abba88b2989c5c1f30b7a34
e0d0d05e11c889e697bfcd6b0efd6ec6
0833d296851a1e081f2175a4cbfa7135

<name>Tom</name>
<age>20</age>

cb884f44f2f1937276917c04c3676183
0833d296851a1e081f2175a4cbfa7135

<name>Mike</name>
<age>25</age>

cb884f44f2f1937276917c04c3676183
b8ee975456a9852fac1328a9d6bf75ed

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!

Statement:
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