Download the PHP package arc/xml without Composer
On this page you can find all versions of the php package arc/xml. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package xml
Short Description Ariadne Component Library: xml writer and parser Component
License MIT
Homepage https://github.com/Ariadne-CMS/arc/wiki
Informations about the package xml
ARC: Ariadne Component Library
A flexible component library for PHP
The Ariadne Component Library is a spinoff from the Ariadne Web Application Framework and Content Management System [ http://www.ariadne-cms.org/ ]
arc/xml
This component provides a unified xml parser and writer. The writer allows for readable and always correct xml in code, without using templates. The parser is a wrapper around both DOMDocument and SimpleXML.
The parser and writer also work on fragments of XML. The parser also makes sure that the output is identical to the input. When converting a node to a string, \arc\xml will return the full xml string, including tags. If you don't want that, you can always access the 'nodeValue' property to get the original SimpleXMLElement.
Finally the parser also adds the ability to use basic CSS selectors to find elements in the XML.
Example code:
And parsing it:
Installation
This library requires PHP 7.1 or higher. It is installable and autoloadable via Composer as arc/xml.
Parsing XML
Examples
For these examples we'll use the following XML
Getting the title
result:
The parser returns the full XML element by default. If you just want the contents, you must be explicit:
result:
Instead of the default in SimpleXML, arc\xml must be explicitly told to get the value of the node using the nodeValue
property.
Setting the title
As you can see, there is no need to mention the nodeValue here, the name 'title' is enough to select the correct element. It would not make sense to turn the title into another tag entirely here. You can still use the nodeValue
if you prefer though.
Getting attributes
result
Just what you would expect, even though there is a namespace in there. When you use a namespace that the parser hasn't been told about before, it will simply look it up in the document and re-use it.
Since attributes aren't XML nodes, there is no nodeValue. Attributes are always returned as just a string.
Setting attributes
This adds the title-attribute
if it wasn't there before, or updates it if it was.
Removing attributes
Searching the document
result:
Again, you get the full XML of the result and it is just an array. (Its been joined here using implode
for clarity).
The find()
method accepts most CSS2.0 selectors. For now you can't enter more than one selector, so you can't select 'item, channel' for instance.
Either use the SimpleXML xpath()
method or run multiple queries.
Searching using namespaces
result:
Again, you get the full XML by default. But in addition, though you've used a namespace alias not known in the document ( dublincore
), find()
returns the <dc:date>
element for you. The alias is different, but the namespace is the same and that is what matters.
The find() method always returns an array, which may be empty. By using current() you get the first element found, or null if nothing was found.
Supported CSS Selectors
The following CSS selectors are supported:
tag1 tag2
This matchestag2
which is a descendant oftag1
.tag1 > tag2
This matchestag2
which is a direct child oftag1
.tag:first-child
This matchestag
only if its the first child.tag1 + tag2
This matchestag2
only if its immediately preceded bytag1
.tag1 ~ tag2
This matchestag2
only if it has a previous siblingtag1
.tag[attr]
This matchestag
if it has the attributeattr
.tag[attr="foo"]
This matchestag
if it has the attributeattr
with the valuefoo
in its value list.tag#id
This matches anytag
with idid
.#id
This matches any element with idid
.ns|tag
This matchesns:tag
or more generallytag
in the namespace indicated by the aliasns
SimpleXML
The parsed XML behaves almost identical to a SimpleXMLElement, with the exceptions noted above. So you can access attributes just like SimpleXMLElement allows:
You can walk through the node tree:
Any method or property available in SimpleXMLElement is included in \arc\xml parsed data.
DOMElement methods
In addition to SimpleXMLElement methods, you can also call any method that is available in DOMElement.
Parsing fragments
The arc\xml parser accepts partial XML content. It doesn't require a single root element.
result: