- Introduction
- Installing/Configuring
- Requirements
- Installation
- Runtime Configuration
- Resource Types
- Predefined Constants
- Examples
- Basic SimpleXML usage
- Dealing with XML errors
- SimpleXMLElement — The SimpleXMLElement class
- SimpleXMLElement::addAttribute — Adds an attribute to the SimpleXML element
- SimpleXMLElement::addChild — Adds a child element to the XML node
- SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element
- SimpleXMLElement::attributes — Identifies an element's attributes
- SimpleXMLElement::children — Finds children of given node
- SimpleXMLElement::__construct — Creates a new SimpleXMLElement object
- SimpleXMLElement::count — Counts the children of an element
- SimpleXMLElement::getDocNamespaces — Returns namespaces declared in document
- SimpleXMLElement::getName — Gets the name of the XML element
- SimpleXMLElement::getNamespaces — Returns namespaces used in document
- SimpleXMLElement::registerXPathNamespace — Creates a prefix/ns context for the next XPath query
- SimpleXMLElement::saveXML — Alias of SimpleXMLElement::asXML
- SimpleXMLElement::__toString — Returns the string content
- SimpleXMLElement::xpath — Runs XPath query on XML data
- SimpleXMLIterator — The SimpleXMLIterator class
- SimpleXMLIterator::current — Returns the current element
- SimpleXMLIterator::getChildren — Returns the sub-elements of the current element
- SimpleXMLIterator::hasChildren — Checks whether the current element has sub elements
- SimpleXMLIterator::key — Return current key
- SimpleXMLIterator::next — Move to next element
- SimpleXMLIterator::rewind — Rewind to the first element
- SimpleXMLIterator::valid — Check whether the current element is valid
- SimpleXML Functions
- simplexml_import_dom — Get a SimpleXMLElement object from a DOM node
- simplexml_load_file — Interprets an XML file into an object
- simplexml_load_string — Interprets a string of XML into an object
soloman at textgrid dot com ¶
11 years ago
Three line xml2array:
Ta da!
whyme ¶
9 years ago
Simple means simple. If you know the structure and just want the value of a tag:
Warning, numbers can come out as strings, empty elements like come out as array[0]
xaviered at gmail dot com ¶
10 years ago
Here is a recursive function that will convert a given SimpleXMLElement object into an array, preserving namespaces and attributes.
xananax at yelostudio dot com ¶
11 years ago
None of the XML2Array functions that I found satisfied me completely; Their results did not always fit the project I was working on, and I found none that would account for repeating XML elements [such as
]
So I rolled out my own; hope it helps someone.
maikel at yuluma dot com ¶
8 years ago
In reply to soloman at textgrid dot com,
2 line XML2Array:
$xml = simplexml_load_string[$file];
$array = [array]$xml;
emmanuel ¶
12 years ago
dynamic sql in php using xml:
test.xml:
SELECT * FROM USERS
WHERE id = %d
WHERE username = "%s";
index.php:
mahmutta at gmail dot com ¶
12 years ago
while using simple xml and get double or float int value from xml object for using math operations [+ * - / ] some errors happens on the operation, this is because of simple xml returns everythings to objects.
exmple;
oscargodson at gmail dot com ¶
13 years ago
To add to what others have said, you can't directly put a $_GET or $_POST value into a variable then into an attribute using SimpleXML. You must first convert it to an integer.
This will NOT work
You will get something like:
Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/mysite/index.php on line 10
However, this WILL work and is much simpler then using [string] or other methods.
philipp at strazny dot com ¶
11 years ago
Here's a quick way to dump the nodeValues from SimpleXML into an array using the path to each nodeValue as key. The paths are compatible with e.g. DOMXPath. I use this when I need to update values externally [i.e. in code that doesn't know about the underlying xml]. Then I use DOMXPath to find the node containing the original value and update it.
dkrnl at yandex dot ru ¶
8 years ago
Wrapper XMLReader class, for simple SAX-reading huge xml:
//github.com/dkrnl/SimpleXMLReader
Usage example: //github.com/dkrnl/SimpleXMLReader/blob/master/examples/example1.php
reibeltel at gmail dot com ¶
6 months ago
I rewrite the function to convert xml object to array because my case is more simple:
function xmlObjToArr[$obj] {
$array = [];
foreach[$obj as $item]{
$row = [];
foreach[$item as $key => $val]{
$col = array[
[string]$key => [string]$val
];
array_push[$row,$col];
}
array_push[$array,$row];
}
return $array;
}
aalaap at gmail dot com ¶
14 years ago
Here are two quick and dirty functions that use SimpleXML to detect if a feed xml is RSS or ATOM:
The functions take in the full text feed [retrieved via cURL, for example] and return a true or a false based on the result.
thedoc8786 at gmail dot com ¶
10 years ago
I know it is over-done, but the following is a super-short example of a XML to Array conversion function [recursive]:
sherwinterunez at yahoo dot com ¶
12 years ago
Serge ¶
7 years ago
XML data values should not contain "&" and that need to be replaced by html-entity "&"
You can use this code to replace lonely "&" to "&":
This will replace just "&" into "&" but dont touches other html-entities like " ", "<" etc and, of course, "&".
P.S. In regexp max length is 6 becouse I found that is the maximum length of possible html entity using this code:
minus two is for first "&" and last ";" in an any html entity.
phil at dier dot us ¶
11 years ago
Here's a function I came up with to convert an associative array to XML. Works for multidimensional arrays as well.
bxt at die-optimisten dot net ¶
13 years ago
Addition to QLeap's post:
SimpleXML will return a reference to an object containing the node value and you can't use references in session variables as there is no feasible way to restore a reference to another variable.
This won't work too:
$val=$this->xml->node->attributes[]->name;
echo $array[$val]; // will cause a warning because of the wrong index type.
You have to convert/cast to a String first:
echo $array[[string]$val];
This will work as expected, because converting will call the __toString[] method. Therefor echo works too:
echo $val; // will display the name
QLeap ¶
13 years ago
Storing SimpleXMLElement values in $_SESSION does not work. Saving the results as an object or individual elements of the object will result in the dreaded "Warning: session_start[] [function.session-start]: Node no longer exists" error.
For example, this does not work:
$xml = new SimpleXMLElement[$page];
$country = $xml->Response->Placemark->AddressDetails->Country->CountryNameCode;
$_SESSION['country'] = $country;
This will work:
$_SESSION['country'] = [string] $country;
gwhitescarver at yahoo dot com ¶
14 years ago
Moving some code from a PHP 5.2.6 / Windows environment to a 5.2.0 / Linux environment, I somehow lost access to a plain text node within a SimpleXML Object. On a var_dump of $xml_node, a [0] element was shown as the string '12'. However, $xml_node[0] was evaluating NULL in 5.2.0. You can see below the code change I made, pulling my data out of the raw XML with a regular expression. Hope this is useful to someone.
//In some versions of PHP it seems we cannot access the [0] element of a SimpleXML Object. Doesn't work in 5.2.0:
//$count = $xml_node[0];
//grab the raw XML:
$count = [$xml_node->asXML[]];
//pull out the number between the closing and opening brace of the xml:
$count = preg_replace['/.*>[\d*]