Skip to content

Repository files navigation

PHP XML Reader

Banner with cards catalog picture in background and PHP XML Reader title

phpversiondownloadslicensetestscodecov

PHP package to read XML with nice API, heavily inspired by stackoverflow answer.

About

PHP have native functions to read XML files with SimpleXML, but it's not easy to use and not very flexible. This package offer to read XML files as array, with a simple and flexible API.

Requirements

  • PHP version >= 8.0

Installation

You can install the package via composer:

composer require kiwilan/php-xml-reader

Usage

You can read XML from path or from content.

  • mapContent: boolean If a key has only @content key, return only the value of @content. Default: true.
  • failOnError: boolean Throw exception if XML is invalid. Default: true.
useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml', bool $mapContent = true, bool $failOnError = true);

Methods

MethodDescriptionType
$xml->getRoot()Value of root element?string
$xml->getRootNS()Namespaces of root elementstring[]
$xml->getRootAttributes()Attributes of root elementarray<string, mixed>
$xml->getRootAttributes('key');Value of key attribute of root elementmixed
$xml->getVersion();XML version?string
$xml->getEncoding();XML encoding?string
$xml->isValidXml();Check if XML is validbool
$xml->getPath();Path of XML file?string
$xml->getFilename();Filename of XML file?string
$xml->getConverter();Converter used to convert XML to array\Kiwilan\XmlReader\XmlConverter
$xml->save('path/to/file.xml');Save XML filebool
$xml->toArray();Convert XML to arrayarray<string, mixed>
$xml->__toString();Convert XML to stringstring

Advanced methods

MethodDescriptionTypeOptions
$xml->getContents()XML as multidimensional arrayarray<string, mixed>
$xml->find('key')Find key will return first value where that contain keyarray<string, mixed>strict, content, attributes
$xml->search('key')Search will return all values that contain keymixed
$xml->extract(...keys)Extract metadata key, if not found return nullmixed
XmlReader::parseContent(key)Parse content of entrymixed
XmlReader::parseAttributes(key)Parse attributes of entryarray<string, mixed> or null

Basic usage

XML as multidimensional array from root (safe).

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$contents = $xml->getContents();

Basic usage.

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$contents = $xml->getContents();
$title = $contents['metadata']['dc:title'] ?? null;

Find

Find key will return first value where key that contain title (safe).

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$title = $xml->find('title', strict: false);

Find key will return first value where key is dc:title (safe)

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$dcTitle = $xml->find('dc:title');

Find key will return first value where key that contain dc:title and return @content (safe)

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$dcCreator = $xml->find('dc:creator', content: true);

Find key will return first value where key contain dc:creator and return @attributes (safe)

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$dcCreator = $xml->find('dc:creator', attributes: true);

Search

Search will return all values that contain dc (safe)

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$dc = $xml->search('dc');

Extract

Extract metadata key, if not found return null (safe)

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$rootKey = $xml->extract('metadata');

Extract metadata and dc:title keys (safe)

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$subSubKey = $xml->extract(['metadata', 'dc:title']);

Get content

If you want to extract only @content you could use parseContent() method, if you want to extract only @attributes you could use parseAttributes() method.

Extract dc:title key and return @content (safe)

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$title = $xml->extract(['metadata', 'dc:title']);
$title = XmlReader::parseContent($title);

Extract dc:creator key and return @content (safe)

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$creator = $xml->extract(['metadata', 'dc:creator']);
$creator = XmlReader::parseContent($creator);

Extract dc:creator key and return @attributes (safe)

useKiwilan\XmlReader\XmlReader;
$xml = XmlReader::make('path/to/file.xml');
$creatorAttributes = $xml->extract(['metadata', 'dc:creator']);
$creatorAttributes = XmlReader::parseAttributes($creatorAttributes);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

  • spatie for spatie/package-skeleton-php

License

The MIT License (MIT). Please see License File for more information.

About

PHP package to read XML with nice API.

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages