27.1 C
Pakistan
Saturday, July 27, 2024

PHP Makes It Easy to Read and Write XML

With developer experience in mind, XML Wrangler can read any kind of XML file, including those with intricate namespaces and enormous files. If the XML is erroneous, it will also raise exceptions!

A straightforward PHP library called XML Wrangler was created to make reading and writing XML simple. This package is framework agnostic and may be used in any PHP project (>=v8.1) using well-known Laravel-like techniques. With its abundance of functionality, XML Wrangler makes working with XML far more pleasurable than it is with PHP right out of the box.

  • Dot Notation: XML document querying with dot-notation
  • Low memory usage: Large XML files can be read by XML Wrangler.
  • Laravel-like: methods that you can use like sole, first, firstOrFail, collect, etc.
  • Lazy methods: Read large XML files and repeatedly run a loop over each node
  • XML Generation: Write XML using arrays
  • XML Parsing: Provides functionality to parse XML documents.
  • DTOs: Use the Element DTO element to define attributes and namespaces
  • Error Handling: It handles errors correctly, in contrast to SimpleXML.
  • XPath Queries: supports data extraction from XML using XPath queries.
  • Editing and Manipulation: permits XML data to be modified and handled.
  • Simple API: provides an easy-to-use API for handling XML
  • Namespace Support: Handles XML namespaces.

Here’s an example of reading XML, however you should read the readme to understand about all you can do with this package:

<?php
use Saloon\XmlWrangler\XmlReader;
$reader = XmlReader::fromString($xml);
// Retrieve all values as one simple array
$reader->values();
// [‘breakfast_menu’ => [[‘name’ => ‘…’], [‘name’ => ‘…’], …]
// Use dot-notation to find a specific element
$reader->value(‘food.0’)->sole();
// [‘name’ => ‘Belgian Waffles’, ‘price’ => ‘$5.95’, …]
// Use the element method to get a simple Element DTO containing attributes and content
$reader->element(‘food.0’)->sole(); // Element::class
// Use XPath to query the XML
$reader->xpathValue(‘//food[@bestSeller=”true”]/name’)->get();
// [‘Belgian Waffles’, ‘Berry-Berry Belgian Waffles’]

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles