Use SimpleXML to process XML

Source: Internet
Author: User
Use SimpleXML to process XML
When developing Web applications, in order to reduce the I/O overhead of reading and writing databases, some configuration information is often stored in a flat file such as XML and json, and then through the script language such as PHP, javascript to operate these files.
PHP has two main methods to operate XML: DOM and SimpleXML. The two methods parse XML by analyzing the entire XML document and providing APIs to access elements in the tree. SimpleXML is a new feature of PHP 5, designed to easily complete some common XML processing tasks.
The following is a simple example of how to format a group 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. $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

  1. Use simpleXML to process XML
  2. // Use simpleXML to process XML
  3. Foreach ($ child-> children () as $ item)
  4. }
  5. }
  6. $ Xml = simplexml_load_file ('./simple. XML ');
  7. // Var_dump ($ xml );
  8. // Echo $ xml-> getName ();
  9. // Var_dump ($ xml-> children ());
  10. $ Record = '';
  11. Foreach ($ xml-> children () as $ child)
  12. {
  13. $ Record. ='
  14. {
  15. // Var_dump ($ child );
  16. $ Record. ='
  17. $ Record. ='
  18. Echo $ record;
  19. ?>
  20. '; '; ';
      Name Price Description Calories
    '. $ Child-> attributes ().''. $ Item .'

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.