Compdigitec Labs

« | Home | »

SimpleXML – solution for XML manipulation in PHP

By admin | September 7, 2008

If you ever do get the need to manipulate (read/write) XML, you could try using the PHP DOM API, but it’s very difficult to use the DOM. An good extension-less way to access and manipulate XML instead is SimpleXML. It’s very easy to use compared to DOM and features a easy way to access attributes and tags in any XML document.

XML file (sample.xml)
<?xml version=”1.0″ ?>
<root><example>This is example number 1</example><example><tag>This is a tag.</tag></example></root>

Reading a XML file
<?php
$a = new SimpleXMLElement(file_get_contents(“sample.xml”));
echo $a->example[0];
?>

The example above will read the sample XML file above and will output the contents of the first <example> element.

Writing a XML file

<?php
$a = new SimpleXMLElement(‘<?xml version=”1.0″ ?><root></root>’);
for($i = 1; $i <= 9; $i++) {
$a->addChild(“element$i”,”Hello”);
}
echo $a->asXML();
?>

The above example will create the following XML file:

<?xml version=”1.0″?>
<root><element1>Hello</element1><element2>Hello</element2><element3>Hello</element3><element4>Hello</element4><element5>Hello</element5><element6>Hello</element6><element7>Hello</element7><element8>Hello</element8><element9>Hello</element9></root>

So as you can see, you can very easily manipulate XML with SimpleXML. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: (X)HTML, PHP | 19 Comments »

19 Responses to “SimpleXML – solution for XML manipulation in PHP”

  1. สล็อตวอเลท ไม่มีขั้นต่ำ Says:
    March 18th, 2022 at 22:07

    … [Trackback]

    […] There you can find 35678 more Information on that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  2. rent scooters in madeira beach Says:
    March 23rd, 2022 at 18:30

    … [Trackback]

    […] Find More on on that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  3. 늑대닷컴2 Says:
    March 31st, 2022 at 03:07

    … [Trackback]

    […] Find More here to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  4. nova88 Says:
    April 7th, 2022 at 15:33

    … [Trackback]

    […] Information to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  5. binb365 Says:
    April 13th, 2022 at 05:08

    … [Trackback]

    […] Read More to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  6. sbo Says:
    June 16th, 2022 at 20:42

    … [Trackback]

    […] Find More to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  7. 테니스 서브 핫테TV Says:
    June 27th, 2022 at 12:04

    … [Trackback]

    […] Read More here to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  8. sbo Says:
    August 14th, 2022 at 18:54

    … [Trackback]

    […] Info to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  9. jav Says:
    September 12th, 2022 at 20:28

    … [Trackback]

    […] Find More on that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  10. อ่านมังงะ Says:
    December 11th, 2022 at 09:59

    … [Trackback]

    […] Find More Information here to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  11. male enhancement pillsays: Says:
    January 7th, 2023 at 07:33

    … [Trackback]

    […] Find More Information here on that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  12. maryland blue crab Says:
    January 19th, 2023 at 18:25

    … [Trackback]

    […] Read More on that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  13. Silver Sands Says:
    January 23rd, 2023 at 02:33

    … [Trackback]

    […] Find More Information here to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  14. psilocybin grow kits with spores​ Says:
    January 24th, 2023 at 14:42

    … [Trackback]

    […] Find More to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  15. sphynx cat for sale Says:
    January 26th, 2023 at 17:52

    … [Trackback]

    […] Read More to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  16. dark liberty cap mushrooms Says:
    January 28th, 2023 at 14:44

    … [Trackback]

    […] Find More Info here to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  17. สล็อตวอเลท Says:
    March 15th, 2023 at 20:41

    … [Trackback]

    […] Information to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  18. weed delivery toronto Says:
    March 16th, 2023 at 06:44

    … [Trackback]

    […] Find More on to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

  19. mp3 juice Says:
    March 28th, 2023 at 06:38

    … [Trackback]

    […] Here you will find 30481 more Information to that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

Comments