pn/epp-xml2array

XML/EPP to a simple readable array, including Namespace and CDATA support

dev-master 2015-11-14 12:47 UTC

This package is not auto-updated.

Last update: 2024-04-17 16:44:58 UTC


README

An EPP (http://en.wikipedia.org/wiki/Extensible_Provisioning_Protocol), with Namespace support, to a simple Array. Still usable for normal XML too. CDATA is supported.

Why not use xyz?

For regular XML you can use this (probably) fine:

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

But it doesn't work "out of the box" with namespaces. Also, simplexml_load_string somehow removes certain attributes. For example, if your EPP had:

<domain:contact type="billing">P-ABC120</domain:contact>

It will remove the "type" attribute. And for EPP any missing information makes those methods unusable.

How to use

First, load the XML:

PN\Xml2Array::loadXML($xml_string);
//or
PN\Xml2Array::loadXML(file_get_contents('somefile.xml'));

Get the whole array from your complete EPP string:

print_r( PN\Xml2Array::getArray() );

Get all the Namespace prefixes + URI's as an array:

print_r( PN\Xml2Array::getNamespaces() );

Get a part of the EPP in array by giving the prefix:

print_r( PN\Xml2Array::getArrayNS('contact') );
Added in v1.1

Too lazy to look where in the array/xml a tag is? Or the tag could be changing in different xml files? See example1.php, use it like this:

print_r( PN\Xml2Array::getArrayElement('domain', 'pw', true ) );
Added in v1.3

Want to get an attribute like:

<result code="1000">...

See example1.php, use it like this:

print_r( Xml2Array::getArrayAttribute(null, 'result', 'code', true) );

Simple as that.

License

MIT