You appear to be a bot. Output may be restricted
Description
Render an associative array as an XML string. This is needed because SimpleXMLElement only handles valid XML, but we sometimes want to pass around (possibly invalid) fragments. Note that 'null' values make a tag self-closing; this is only sometimes correct (depending on the version of HTML/XML); see the list of 'void tags'.
Example: array(
- 'html' => array( |<html xmlns="foo">
- ), |</html>
‘head’ => array( | |
---|---|
), | |
‘body’ => array( ==> | |
‘h2′ => ‘Some thing’, |
Some thing |
‘p’ => ‘it’s all up ons’, |
it’s all up ons |
‘br’ => null, | |
), |
Usage
$string|DOMDocument = Jetpack_Sitemap_Buffer::array_to_xml_string( $array, $parent, $root );
Parameters
- $array
- ( array ) required – A recursive associative array of tag/child relationships.
- $parent
- ( DOMElement ) optional – (optional) an element to which new children should be added.
- $root
- ( DOMDocument ) optional – (optional) the parent document.
Returns
string|DOMDocument The rendered XML string or an object if root element is specified.
Source
File name: jetpack/modules/sitemaps/sitemap-buffer.php
Lines:
1 to 37 of 37
protected function array_to_xml_string( $array, $parent = null, $root = null ) { $return_string = false; if ( null === $parent ) { $return_string = true; $parent = $root = new DOMDocument(); } if ( is_array( $array ) ) { foreach ( $array as $key => $value ) { $element = $root->createElement( $key ); $parent->appendChild( $element ); if ( is_array( $value ) ) { foreach ( $value as $child_key => $child_value ) { $child = $root->createElement( $child_key ); $element->appendChild( $child ); $child->appendChild( self::array_to_xml_string( $child_value, $child, $root ) ); } } else { $element->appendChild( $root->createTextNode( $value ) ); } } } else { $element = $root->createTextNode( $array ); $parent->appendChild( $element ); } if ( $return_string ) { return $root->saveHTML(); } else { return $element; } }