XML helper for CodeIgniter

Download xml_helper.zip (PHP 5)

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
Aug 20, 2009
#2
very useful thank you
viji
Oct 24, 2009
#3
good
amru
Oct 29, 2009
#4
Thank you, thats useful
RRR
Nov 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
Jun 7, 2010
#10
In the helper, replace
DOMDocument('1.0');

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

Jul 21, 2010
#12
THIS IS A GREAT..
VIJU
Aug 18, 2010
#13
Hi. I have a problem.

Call to a member function createElement() on a non-object in \application\helpers\MY_xml_helper.php on line 52

Can you help me?

Sep 2, 2010
#14
I ran into the same error (member function createElement() on a non-object) when I was trying to produce xml with nested elements. For example, for this xml result:

&lt;?xml version="1.0"?&gt;
<book>
<author>
<birthdate>1948-04-04</birthdate>
</author>
</book>

The code is as follows:

$xml = xml_dom();
$book = xml_add_child($xml, 'book');
$author = xml_add_child($book,'author');
xml_add_child($author,'birhtdate','1948-04-04');
David
Sep 16, 2010
#15
Note that the formatting is kind of messy in my comment above, hopefully this is still helpful.
David
Sep 16, 2010
#16
I would like to get data from xml file. How could I do that? If you could give me a favour, please exlpain.
Htet Nyi
Nov 2, 2010
#17
Good tutorial
chamil sanjeewa
Dec 13, 2010
#18
For those getting a message similar to ... "Call to a member function createElement() on a non-object in" ... try the following...

When adding a child to a child of the dom, for some reason it was getting hung up while trying doc owner. The solution was to modify the help and my method calls so that Im pass the xml_dom additionally when calling xml_add_child or xml_add_attribute.

Replace the functions with the following ...


function xml_add_child($dom, $parent, $name, $value = NULL, $cdata = FALSE)
{
$child = $dom->createElement($name);
$parent->appendChild($child);

if($value != NULL)
{
if ($cdata)
{
$child->appendChild($dom->createCdataSection($value));
}
else
{
$child->appendChild($dom->createTextNode($value));
}
}

return $child;
}



function xml_add_attribute($dom, $node, $name, $value = NULL)
{
$attribute = $dom->createAttribute($name);
$node->appendChild($attribute);

if($value != NULL)
{
$attribute_value = $dom->createTextNode($value);
$attribute->appendChild($attribute_value);
}

return $node;
}
Daniel Smith
Dec 15, 2010
#19
Thanks man!! you saved a life of a Brazilian hehehe!!
Diego HF
Dec 22, 2010
#20
Thanks champ!
Vik
Jan 21, 2011
#21
Thanks excellent helper
otem
Apr 7, 2011
#22
fsdfdsf
sfsdf
May 5, 2011
#23
<a href="http://http://maestric.com/doc/php/codeigniter_xml">fs</a>
fas
May 17, 2011
#24
How can i create child with sub child example bellow.

<HotelRoomAvailabilityRequest>
<hotelId>106347</hotelId>
<arrivalDate>08/01/2011</arrivalDate>
<departureDate>08/03/2011</departureDate>
<includeDetails>true</includeDetails>
<RoomGroup>
<Room>
<numberOfAdults>2</numberOfAdults>
<numberOfChildren>2</numberOfChildren>
<childAges>5,7</childAges>
</Room>
</RoomGroup>
</HotelRoomAvailabilityRequest>

Can you help me?
Sumon
Nov 2, 2011
#25
FYI, in xml_add_child() $name has to be a valid XML name. If you pass it something invalid, you'll get:

Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in system/application/helpers/MY_xml_helper.php

Adding this into xml_add_child() helps to sanitize the name so it doesn't fail:

$name = preg_replace('/[^\w0-9-]/', '', $name);

It'd be great to have an XML sanitize function to make names valid. :)
Jeremy
Nov 17, 2011
#26
this is good...
Hary
Nov 27, 2011
#27
When i view the xml in the browser it is only being displayed as a test.

How could i make it be formatted as an xml structure.

Dec 4, 2011
#28
Can I know how to load an existing xml file? tnx..
alfred
Dec 13, 2011
#29
Rocks!! :):)
Carlos
Jan 26, 2012
#30
This is great :)
Elieser
Feb 9, 2012
#31
Nice:)
Hameed
Mar 1, 2012
#32
nice
ss
Mar 1, 2012
#33
my output is not in xml format....plz help????
munmun poddar
May 8, 2012
#34
wonderfull! thank you
gfox
Sep 23, 2012
#35
How i can call sitemap in codeigniter
www.mysite/sitemap.xml
and output should be like this
&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
<urlset
>

&lt;!-- created with Free Online Sitemap Generator www.xml-sitemaps.com --&gt;

<url>

<loc>http://www.mysite/</loc>

<changefreq>always</changefreq>

</url>

<url>

<loc>http://www.mysite/index/join_now</loc>

<changefreq>always</changefreq>
</url>

<url>
<loc>http://www.mysite/index/about_us</loc>
<changefreq>always</changefreq>
</url>
<url>
<loc>http://www.mysite/index/contact_us</loc>
<changefreq>always</changefreq>
</url>
</urlset>
Sadaf Baig
Sep 24, 2012
#36
how would you use this to load an existing xml document with multiple namespaces, then query for an existing node in a given namespace and extend it bu adding an attribute in that namespace?
tim
Sep 25, 2012
#37
Great helper class! Thanks!

For those who're asking how to display the output as XML formatted document; you should use the output class to set the document type to "text/xml", and it goes like this:

Put this line before the "xml_print" method:

$this->output->set_content_type('text/xml');


Regards.
Ali El-Wafdy
Oct 19, 2012
#38
Thanks much
dinhtana4
Apr 16, 2013
#39
thanks .....friendss !!!

May 20, 2013
#40
this is great! thanks pal!
rio
May 24, 2013
#41
Excellent This is very HelpFul for me
Thanks for Posting;

Me: Waqas Ali
From Lahore - Pakistan;
Waqas Ali
Jun 6, 2013
#42
hello, help me please sir.
it has error message >>

in line : $dom = xml_dom();

"Fatal error: Call to undefined function xml_dom()"
how can i do ??
thank you.

kangwan from thailand
Aug 23, 2013
#43
You need to download the helper on the link on the top of the page and save it in the folder application/helpers and call it like $this->load->helper('My_xml_helper');
Wagner Andrade
Sep 23, 2013
#44
thank you Mr.Wagner Andrade
kangwan
Sep 24, 2013
#45
Thank you! (Also love Dan Simmons)
Nexus Code
Oct 26, 2013
#46
Thank you. You made good helper!
Arthur
Oct 28, 2013
#47
All things best how to download .xml file ...plz help me dear
manish
Jun 10, 2015
#48
HELLO,

all off you guys
iam just use these code and tested then output Dan Simmons
its not giving in xml format
Mithilesh Sah
Dec 21, 2015
#49
Hi I need to create one node which will look like this
<MyNode >

So how can I add that to a node.
please suggest me.
Arun Sharma
Feb 24, 2017
#50