Download the PHP package saloonphp/xml-wrangler without Composer

On this page you can find all versions of the php package saloonphp/xml-wrangler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package xml-wrangler

## 🌵 XML Wrangler - Easily Read & Write XML in PHP

XML Wrangler is a simplistic PHP library designed to make reading and writing XML easy. XML Wrangler has been built with developer experience in mind - you can read any type of XML file, even with complex namespaces and even large XML files. It will also throw exceptions if the XML is invalid!

Installation

XML Wrangler is installed via Composer.

Requires PHP 8.1+

Reading XML

Reading XML can be done by passing the XML string or file into the XML reader and using one of the many methods to search and find a specific element or value. You can also convert every element into an easily traversable array if you prefer. If you need to access attributes on an element you can use the Element DTO which is a simple class to access the content and attributes. XML Wrangler provides methods to iterate through multiple elements while only keeping one element in memory at a time.

Writing XML

Writing XML is as simple as defining a PHP array and using keys and values to define elements. When you need to define elements with a few more characteristics like attributes or namespaces you can use the Element DTO to define more advanced elements.

The above code will create the following XML

Documentation

Reading XML

This section on the documentation is for using the XML reader.

Various Input Types Supported

The XML reader can accept a variety of input types. You can use an XML string, file, or provide a resource. You can also read the XML directly from a PSR response (like from Guzzle) or a Saloon response.

Warning Due to limitations of the underlying PHP XMLReader class, the fromStream, fromPsrResponse and fromSaloon methods will create a temporary file on your machine/server to read from which will be automatically removed when the reader is destructed. You will need to ensure that you have enough storage on your machine to use this method.

Converting Everything Into An Array

You can use the elements and values methods to convert the whole XML document into an array. If you would like an array of values, use the values method - but if you need to access attributes on the elements, the elements method will return an array of Element DTOs.

Note If you are reading a large XML file, you should use the element or value methods instead. These methods can iterate through large XML files without running out of memory.

Reading Specific Values

You can use the value method to get a specific element's value. You can use dot-notation to search for child elements. You can also use whole numbers to find specific positions of multiple elements. This method searches through the whole XML body in a memory-efficient way.

This method will return a LazyQuery class which has different methods on to retrieve the data.

Reading Specific Elements

You can use the element method to search for a specific element. This method will provide an Element class which contains the value and attributes. You can use dot-notation to search for child elements. You can also use whole numbers to find specific positions of multiple elements. This method searches through the whole XML body in a memory efficient way.

This method will return a LazyQuery class which has different methods to retrieve the data.

Removing Namespaces From XML

Sometimes it's easier to traverse an XML document when you don't have to worry about namespaces and prefixes to elements. If you would like to remove them you can use the removeNamespaces() method on the reader.

Lazily Iterating

When searching a large file, you can use the lazy or collectLazy methods which will return a generator of results only keeping one item in memory at a time.

Using Laravel Collections

If you are using Laravel, you can use the collect and collectLazy methods which will convert the elements into a Laravel Collection/Lazy Collection. If you are not using Laravel, you can install the illuminate/collections package via Composer to add this functionality.

Searching by specific attributes

Sometimes you might want to search for a specific element or value where the element contains a specific attribute. You can do this by providing a second argument to the value or element method. This will search the last element for the attributes and will return if they match.

Reading with XPath

XPath is a fantastic way to search through XML. With one string, you can search for a specific element, with specific attributes or indexes. If you are interested in learning XPath, you can click here for a useful cheatsheet.

Reading Specific Values via XPath

You can use the xpathValue method to find a specific element's value with an XPath query. This method will return a Query class which has different methods to retrieve the data.

Reading Specific Elements via XPath

You can use the xpathElement method to find a specific element with an XPath query. This method will return a Query class which has different methods to retrieve the data.

Warning Due to limitations with XPath - the above methods used to query with XPath are not memory safe and may not be suitable for large XML documents.

XPath and un-prefixed namespaces

You might found yourself with an XML document that contains an un-prefixed xmlns attribute - like this:

When this happens, XML Wrangler will automatically remove these un-prefixed namespaces to improve compatability. If you would like to keep these namespaces, you can use setXpathNamespaceMap to map each un-prefixed XML namespace.

Writing XML

This section on the documentation is for using the XML writer.

Basic Usage

The most basic usage of the reader is to use string keys for the element names and values for the values of the element. The writer accepts infinitely nested arrays for nested elements.

The above code will be converted into the following XML

Using the Element DTO

When writing XML, you will often need to define attributes and namespaces on your elements. You can use the Element class in the array of XML to add an element with an attribute or namespace. You can mix the Element class with other arrays and string values.

This will result in the following XML

Arrays Of Values

You will often need to define an array of elements. You can do this by simply providing an array of values or element classes.

This will result in the following XML

Customising the root element

Sometimes you may need to change the name of the root element. This can be customised as the first argument of the write method.

If you would like to add attributes and namespaces to the root element you can use a RootElement class here too.

CDATA Element

If you need to add a CDATA tag you can use the CDATA class.

This will result in the following XML

Composable Elements

Sometimes you might have a part of XML that you will reuse across multiple XML requests in your application. With XML Wrangler, you can create "composable" elements where you can define your XML content in a class which you can re-use across your application. Extend the Element class and use the protected static compose method.

This will result in XML like this:

Customising XML encoding, version, and standalone

The default XML encoding is utf-8, the default version of XML is 1.0, and the default standalone is null (XML parsers interpret no standalone attribute the same as false). If you would like to customise this you can with the setXmlEncoding, setXmlVersion, and setXmlStandalone methods on the writer.

Which results in the XML declaration <?xml version="2.0" encoding="ISO-8859-1" standalone="yes"?>.

Adding custom "Processing Instructions" to the XML

You can add a custom "Processing Instruction" to the XML by using the addProcessingInstruction method.

This will result in the following XML

Minification

By default the XML written is not minified. You can provide the third argument to the write method to minify the XML.

Credits

XML Wrangler is a simple wrapper around two really powerful libraries which do a lot of the legwork. Both of libraries are fantastic and deserve a star!

Other Credits


All versions of xml-wrangler with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
veewee/xml Version ^3.1.0
spatie/array-to-xml Version ^3.2
ext-dom Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package saloonphp/xml-wrangler contains the following files

Loading the files please wait ....