<?php
$list = array(
"eggs",
"bread",
"milk",
"bananas",
"bacon",
"cheese"
);
$xml = new SimpleXMLElement("<list />");
foreach($list as $item) {
$xml->addChild("item", $item);
}
// for nice output
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom->formatOutput = true;
echo $dom->saveXML();
The starting point is the array that will be our list,
then a SimpleXMLElementobject is
instantiated with a root tag that forms the basis
for the document. In XML, everything
has to be in a tag so an <item>tag has been
introduced in order to contain each list item.
The final block only makes the output prettier,
which isn’t usually important because
XML is for machines, not for humans.
To get the XML to convert from a SimpleXM
LElementobject, call the asXML()method
on that object, which returns a string. The
string, however is all on one line!
The previous example instead converted
from SimpleXMLElementto DOMElement, and
then grabbed the DOMDocumentfrom that.
Set the formatOutputto true, so when a call
is made to DOMDocument::saveXML
to ask it to return the XML as a string), the re‐
sulting output will be nicely formatted.
XML’s abilities to represent attributes, children,
and character data all provide a more
powerful and descriptive way to represent data
than, for example, JSON. These same
features make XML a great way to represent very
detailed information, including datatype information,
so it’s a great choice when those details really do matter.
It can include
and each element can have
attributes that cover even more information.
XML in PHP
There are many ways we can work with XML in PHP,
and they’re all useful in differentsituations. There are
three main approaches to choose from and they all have their
advantages and disadvantages:
APIs are all about integration between systems and sometimes the choice of data format
will be dictated by whatever is on the other end of the relationship. XML is particularly
popular among many enterprise technology platforms such as Java, Oracle, and .NET,
1. SimpleXMLis the most approachable, and my
personal favorite. It is easy to use and understand,
is well documented, and provides a simple interface
as the namesuggests for getting the job done.
SimpleXML does have some limitations, but it
is recommended for most applications.
2. DOM is handy when a project encounters some
of the limitations in SimpleXML.It’s more powerful
and therefore more complicated to use, but there are a small
number of operations that can’t be done with
SimpleXML. There are built-in func‐tions to allow
conversion between these two formats, so it’s very common to use
$list = array(
"eggs",
"bread",
"milk",
"bananas",
"bacon",
"cheese"
);
$xml = new SimpleXMLElement("<list />");
foreach($list as $item) {
$xml->addChild("item", $item);
}
// for nice output
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom->formatOutput = true;
echo $dom->saveXML();
The starting point is the array that will be our list,
then a SimpleXMLElementobject is
instantiated with a root tag that forms the basis
for the document. In XML, everything
has to be in a tag so an <item>tag has been
introduced in order to contain each list item.
The final block only makes the output prettier,
which isn’t usually important because
XML is for machines, not for humans.
To get the XML to convert from a SimpleXM
LElementobject, call the asXML()method
on that object, which returns a string. The
string, however is all on one line!
The previous example instead converted
from SimpleXMLElementto DOMElement, and
then grabbed the DOMDocumentfrom that.
Set the formatOutputto true, so when a call
is made to DOMDocument::saveXML
to ask it to return the XML as a string), the re‐
sulting output will be nicely formatted.
XML’s abilities to represent attributes, children,
and character data all provide a more
powerful and descriptive way to represent data
than, for example, JSON. These same
features make XML a great way to represent very
detailed information, including datatype information,
so it’s a great choice when those details really do matter.
It can include
and each element can have
attributes that cover even more information.
XML in PHP
There are many ways we can work with XML in PHP,
and they’re all useful in differentsituations. There are
three main approaches to choose from and they all have their
advantages and disadvantages:
PHP: JSON - Manual - PHP: Hypertext Preprocessor
APIs are all about integration between systems and sometimes the choice of data format
will be dictated by whatever is on the other end of the relationship. XML is particularly
popular among many enterprise technology platforms such as Java, Oracle, and .NET,
JSON with PHP - Tutorials for Sqoop, ITIL, Jackson ...
Introduction to JSON and PHP - ITNewb: Your Source to ...
PHP: JSON Functions - Manual - PHP: Hypertext …
JSON Tutorial - W3Schools Online Web Tutorials
PECL :: Package :: json - PECL :: The PHP Extension ...
How To Parse JSON With PHP - WebDevTutsDepot — …
PHP JSON installation and json_decode() function | JSON …
AJAX, JSON, jQuery, and PHP - Stack Overflow
Working with JSON in PHP jQuery | Packt
1. SimpleXMLis the most approachable, and my
personal favorite. It is easy to use and understand,
is well documented, and provides a simple interface
as the namesuggests for getting the job done.
SimpleXML does have some limitations, but it
is recommended for most applications.
2. DOM is handy when a project encounters some
of the limitations in SimpleXML.It’s more powerful
and therefore more complicated to use, but there are a small
number of operations that can’t be done with
SimpleXML. There are built-in func‐tions to allow
conversion between these two formats, so it’s very common to use