XML helper for CodeIgniter

Example

$this->load->helper('xml');
 
$dom = xml_dom();
$book = xml_add_child($dom, 'book');
 
xml_add_child($book, 'title', 'Hyperion');
$author = xml_add_child($book, 'author', 'Dan Simmons');		
xml_add_attribute($author, 'birthdate', '1948-04-04');
 
xml_print($dom);

Result

<?xml version="1.0"?>
<book>
  <title>Hyperion</title>
  <author birthdate="1948-04-04">Dan Simmons</author>
</book>

Functions

Create XML document

xml_dom()

Add child to XML element

xml_add_child($parent, $name, $value = NULL, $cdata = FALSE)
  • parent: XML element
  • name: string
  • value: string
  • cdata: boolean

Add attribute to XML element

xml_add_attribute($node, $name, $value = NULL)
  • node: XML element
  • name: string
  • value: string

Print XML document

xml_print($dom, $return = FALSE)
  • dom: XML element
  • return: boolean (if TRUE, just returns XML as a string)
 

Feedback

thanks
li
May 17, 2009
#1
thank you very much
jm
August 20, 2009
#2
very useful thank you
viji
October 24, 2009
#3
good
amru
October 29, 2009
#4
Thank you, thats useful
RRR
November 17, 2009
#5
GOOD WORK!
szok in poland :)
May 7, 2010
#6
So how would you read an xml file modify an attribute and then save the new xml using this helper?
Farhad
May 11, 2010
#7
Farhad, this helper does not XML parsing.
Jérôme Jaglale
May 11, 2010
#8
superb
electric_bit
May 30, 2010
#9
how to make it output encoding too with xml_dom()?
star
June 7, 2010
#10
In the helper, replace
DOMDocument('1.0');

by
DOMDocument('1.0', 'iso-8859-1')
Jérôme Jaglale
June 7, 2010
#11
Excelente!!

July 21, 2010
#12