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 | 10 Comments »

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

  1. เว็บเช็คผลบอล สกอร์บอลออนไลน์ ค่านํ้าแทงบอล เรทบอลดี Says:
    June 28th, 2026 at 23:56

    … [Trackback]

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

  2. sabaaibet Says:
    June 29th, 2026 at 08:06

    … [Trackback]

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

  3. บิ้วอิน เชียงใหม่ Says:
    July 13th, 2026 at 19:21

    … [Trackback]

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

  4. หนังเอวี Says:
    July 19th, 2026 at 22:15

    … [Trackback]

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

  5. abigcandywin.org Says:
    July 21st, 2026 at 23:29

    … [Trackback]

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

  6. Huay2k ครบจริงทั้งไทย Says:
    July 28th, 2026 at 22:41

    … [Trackback]

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

  7. แทงหวยออนไลน์เกาหลี Says:
    July 31st, 2026 at 18:02

    … [Trackback]

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

  8. รับสร้างบ้านไม้สักทอง Says:
    August 1st, 2026 at 21:32

    … [Trackback]

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

  9. ปั้มวิวไลฟ์สด facebook Says:
    August 16th, 2026 at 11:10

    … [Trackback]

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

  10. team building กิจกรรม Says:
    August 18th, 2026 at 18:24

    … [Trackback]

    […] Here you can find 91389 additional Info on that Topic: compdigitec.com/labs/2008/09/07/simplexml-solution-for-xml-manipulation-in-php/ […]

Comments