SimpleXML: Creating a SimpleXMLElement


PHP SimpleXML Step By Step Tutorial - Part 1: The SimpleXML extension is another xml parser available in PHP5. We ever talk about xml parser tips at here. SimpleXML make our job more easy. I takes a different approach to handling the tree. This extension has a single class type, three functions, and six class methods.

Ok, first part of this tutorial series about SimpleXML, we create a XML document use SimpleXMLElement. We use new keyword to be directly instantiated using an XML document within a string as its data source. See like this:

$xml = "<library><book>PHP EveryDay</book></library>";
$sxe = new SimpleXMLElement($xml);

SimpleXML have asXML() method. With this method, we can output a document or substree to a string or a file. If you want to print to browser, you can use like this:

$xml = "<library><book>PHP EveryDay</book></library>";
$sxe = new SimpleXMLElement($xml);
print $sxe->asXML();

You can see above. When no parameter is passed to this method, the results are returned as a string. If we want to save to a file, we can use like this:

$xml = "<library><book>PHP EveryDay</book></library>";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML("test.xml");


Series this article:
SimpleXML: Creating a SimpleXMLElement
SimpleXML: Creating XML Document From Database
SimpleXML: Accessing Element
SimpleXML: Using Iterable Object
SimpleXML: Accessing Unknown Element
SimpleXML: Creating File Contain Part of Another XML Document
SimpleXML: Mixing With DOM Interoperability

Bookmark and Share Tag: SimpleXML, SimpleXMLElement, XML Category: PHP Basic Post : April 17th 2008 Read: 67,617

advertisements


blog comments powered by Disqus