fawno/simple-xml-extended

SimpleXMLElement Extended class

1.1.2 2020-02-20 10:31 UTC

This package is auto-updated.

Last update: 2024-11-21 18:12:56 UTC


README

GitHub license GitHub release Packagist Packagist Downloads GitHub issues GitHub forks GitHub stars PHP

SimpleXMLElement Extended class SimpleXMLExtended add a new method for create CData nodes. Also added a new method for output e nice format XML.

Requirements

  • libxml PHP extension (enabled by default).
  • SimpleXML PHP extension (enabled by default).
  • DOM PHP extension (enabled by default).

Installation

You can install this plugin into your application using composer:

  composer require fawno/simple-xml-extended

or, clone/download this repo, and include src/SimpleXMLExtended.php in your project.

Usage

  use Fawno\SimpleXMLExtended\SimpleXMLExtended;

  // Get a SimpleXMLExtended object from a DOM node
  $xml = simplexml_import_dom($dom, SimpleXMLExtended::class);

  // Interprets an XML file into an SimpleXMLExtended object
  $xml = simplexml_load_file($xml_file, SimpleXMLExtended::class);

  // Interprets a string of XML into an SimpleXMLExtended object
  $xml = simplexml_load_string($xml_string, SimpleXMLExtended::class);

  // Creates a new SimpleXMLExtended object
  $xml = new SimpleXMLExtended($xml_string);

  // Adds a child element to the XML node as cdata
  $xml->addChildCData('node_cdata', 'data as cdata');

  // Return a well-formed and nice formated XML string based on SimpleXMLExtended element
  $xml->formatXML()

Example

  require 'src/SimpleXMLExtended.php';

  use Fawno\SimpleXMLExtended\SimpleXMLExtended;

  $root = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><root/>');

  $root->addChildCData('node_cdata', 'data as cdata');

  print_r($root->asXML());
  /*
    Output:
      <?xml version="1.0" encoding="UTF-8"?>
      <root><node_cdata><![CDATA[data as cdata]]></node_cdata></root>
  */

  print_r($root->formatXML());
  /*
    Output:
      <?xml version="1.0" encoding="UTF-8"?>
      <root>
        <node_cdata><![CDATA[data as cdata]]></node_cdata>
      </root>
  */