• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Jetpack a2z

Jetpack a2z

Jetpack a2z

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / Jetpack_Sitemap_Buffer::array_to_xml_string() – 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’.

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;
        }
      }
     

     View on GitHub View on Trac

    Share this:

    • Click to share on Twitter (Opens in new window)
    • Click to share on Facebook (Opens in new window)
    • Click to share on LinkedIn (Opens in new window)
    • Click to share on Pinterest (Opens in new window)

    Related

Published: 27 November 2019 | Last updated: 27 November 2019

Primary Sidebar

Information

Function name: Jetpack_Sitemap_Buffer::array_to_xml_string
Class ref: Jetpack_Sitemap_Buffer
Plugin ref: Jetpack
Version: 9.4
Sourcefile: modules/sitemaps/sitemap-buffer.php
File ref: modules/sitemaps/sitemap-buffer.php
Deprecated?: No
API Letters: A,B,J,S,T

Footer

Jetpack a2z
Jetpack a2z
Jetpack a2z
WordPress 5.6.2
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  jetpack.wp-a2z.org
© Copyright Jetpack a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites