Processing XML with SimpleXML
Release: 2016-07-25 09:01:38
Original
969 people have browsed it
When developing web applications, in order to reduce the I/O overhead of reading and writing databases, some configuration information is often stored in flat files such as XML and json, and then these files are operated through scripting languages such as PHP and javascript.
There are two main ways to operate XML in PHP: one is through DOM, and the other is through SimpleXML. The principle of these two methods of parsing XML is to analyze the entire XML document and provide APIs to access elements in the tree. SimpleXML is a new feature of PHP 5, which aims to easily complete some common XML processing tasks.
Below is a simple example showing how to format a set of XML data through SimpleXML.
-
-
- Belgian Waffles
- $5.95
- two of our famous Belgian Waffles with plenty of real maple syrup
- 650
-
-
- Strawberry Belgian Waffles
- < price>$7.95
- light Belgian waffles covered with strawberries and whipped cream
- 900
-
-
- Berry-Berry Belgian Waffles
- $8.95
- light Belgian waffles covered with an assortment of fresh berries and whipped cream
- 900
-
Copy code
- Use simpleXML processing XML
-
-
|
name |
price |
description |
calories |
- < /tr>
- // Use simpleXML to process XML
- $xml = simplexml_load_file('./simple.xml');
- //var_dump($xml);
- //echo $xml->getName ();
- //var_dump($xml->children());
- $record = '';
- foreach ($xml->children() as $child)
- {
- $record .= '< tr>
'. $child->attributes(). ' | ';
- foreach ($child->children() as $item)
- {
- //var_dump($child) ;
- $record .= '
'. $item .' | ';
- }
- $record .= '
';
- }
- echo $record;
- ?>
Copy code
|
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 Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31