$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>
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
THIS IS A GREAT..
VIJU
August 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?
September 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:
<?xml version="1.0"?>
<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
September 16, 2010
#15
Note that the formatting is kind of messy in my comment above, hopefully this is still helpful.
David
September 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
November 2, 2010
#17
Good tutorial
chamil sanjeewa
December 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
December 15, 2010
#19
Thanks man!! you saved a life of a Brazilian hehehe!!
Diego HF
December 22, 2010
#20
Thanks champ!
Vik
January 21, 2011
#21
Thanks excellent helper
otem
April 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
November 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
November 17, 2011
#26
this is good...
Hary
November 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.
December 4, 2011
#28
Can I know how to load an existing xml file? tnx..
alfred
December 13, 2011
#29
Rocks!! :):)
Carlos
January 26, 2012
#30
This is great :)
Elieser
February 9, 2012
#31
Nice:)
Hameed
March 1, 2012
#32
nice
ss
March 1, 2012
#33
my output is not in xml format....plz help????
munmun poddar
May 8, 2012
#34