Home  >  Article  >  Backend Development  >  Processing XML with SimpleXML

Processing XML with SimpleXML

WBOY
WBOYOriginal
2016-07-25 09:01:38932browse
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.
  1. Belgian Waffles
  2. $5.95
  3. two of our famous Belgian Waffles with plenty of real maple syrup
  4. 650
  5. Strawberry Belgian Waffles
  6. < price>$7.95
  7. light Belgian waffles covered with strawberries and whipped cream
  8. 900
  9. Berry-Berry Belgian Waffles
  10. $8.95
  11. light Belgian waffles covered with an assortment of fresh berries and whipped cream
  12. 900
Copy code
  1. Use simpleXML processing XML
  2. < /tr>
  3. // Use simpleXML to process XML
  4. $xml = simplexml_load_file('./simple.xml');
  5. //var_dump($xml);
  6. //echo $xml->getName ();
  7. //var_dump($xml->children());
  8. $record = '';
  9. foreach ($xml->children() as $child)
  10. {
  11. $record .= '< tr>
  12. ';
  13. foreach ($child->children() as $item)
  14. {
  15. //var_dump($child) ;
  16. $record .= '
  17. ';
  18. }
  19. $record .= '
  20. ';
  21. }
  22. echo $record;
  23. ?>
  24.   name price description calories '. $child->attributes(). ' '. $item .'
Copy code
Processing XML with SimpleXML


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