Download the PHP package lucid/xml without Composer
On this page you can find all versions of the php package lucid/xml. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package xml
XML writer and parser utilities
Installing
Testing
Run tests with:
The Parser
The Parser
class can parse xml string, files, DOMDocuments, and DOMElements
into a php array.
Parsing xml strings
Parsing xml files
Parsing a DOMDocument
Parsing a DOMElement
Parser Options
Dealing with Attributes
Xml attributes are captured as an deticated section within the actual node data.
The section key defaults to @attributes
, but can be changed using the setAttributesKey
method.
Merging attributes
Setting Parser::mergeAttributes(true)
will merge any attributes as key/value
into the data set.
The above example will output something simmilar to this:
Normalizing keys
You may specifay how keys are being transformed by setting a key normalizer callback.
The default normalizer transforms dashes to underscores and camelcase to snakecase notation.
Set index key
This forces the parser to treat nodes with a nodeName of the given key to be handled as list.
Set a pluralizer
By default the parser will parse xml structures like
To something like:
Setting a pluralizer can fix this.
Note, that a pluralizer can be any callable that takes a string and returns a string.
The Writer
Dumping php data to a xml string
Dumping php data to a DOMDocument
Note: this will create an instance of Lucid\Xml\Dom\DOMDocument
.
Writer options
Set the normalizer instance
Normaly, the NormalizerInterface
implementation is set for you when instantiating a new Writer
, however you can set your own normalizer instance.
Note: the normalizer must implement the Lucid\Xml\Normalizer\NormalizerInterface
interface.
Set the inflector
The inflector is the exact oppoite of the Parser's pluralizer. It singularizes strings.
Set the document encoding
Default encoding is UTF-8
.
Set an attribute key map
This is usefull if you want to output certain keys as xml attribute
Note: you can also use use addMappedAttribute($nodeName, $attributeName)
to add more mapped attributes.
Set value keys
The data structure above would dump the following xml string
However, if you need the value node as actual value of the parent node, you may
use Writer::useKeyAsValue(string $key)
to do so
now dumps:
Writing indexed array structure
Indexed arrays will create xml structures like the example below:
You can change the node names associated with indexed items by using the
useKeyAsIndex(string $key)
method.
Writing arrays with none valid index keys
Arrays containing invalid indices (e.g. unordererd lists) will be treated slightly different.