sabre/xml
An XML library for PHP you may not hate.
If you are writing or consuming API's in PHP, chances are that you need to work with XML. In some cases you may even prefer it.
You may have started with SimpleXML and after a while switched to using the DOM after realizing SimpleXML is really not that simple if you strictly use xml namespaces everywhere.
For writing XML, you may have found that using the DOM requires far too much
code, or you may simply generate your XML by echoing strings, knowing that
it may not be the best idea.
sabre/xml hopes to solve your issues, by wrapping XMLReader and
XMLWriter, and providing standard design patterns around:
- Quickly generating XML based on simple array structures,
- Providing a super simple XML-to-object mapping,
- Re-usability of parsers.
Writing XML
$xmlService = new Sabre\Xml\Service();
$xmlService->namespaceMap = ['http://example.org' => 'b'];
$xmlService->write('{http://example.org}book', [
'{http://example.org}title' => 'Cryptonomicon',
'{http://example.org}author' => 'Neil Stephenson',
]);
This will create the following document:
Cryptonomicon
Neil Stephenson
You can serialize objects by implementing Sabre\Xml\XmlSerializable. This
interface is designed to work identical to PHP 5.5's JsonSerializable.
Reading XML
Hello world
Fuzzy Pickles
sabre