PHPackages                             thenextcoder/xml-flow - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. thenextcoder/xml-flow

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

thenextcoder/xml-flow
=====================

A simple, fast, lightweight, and easy-to-use PHP library for building, parsing, and validating XML documents.

v1.0.2(2y ago)0340↑81.8%MITPHPPHP ^8.2

Since Mar 21Pushed 2y ago1 watchersCompare

[ Source](https://github.com/thenextcoder/xml-flow)[ Packagist](https://packagist.org/packages/thenextcoder/xml-flow)[ Docs](https://github.com/thenextcoder/xml-flow)[ RSS](/packages/thenextcoder-xml-flow/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

XMLFlow
=======

[](#xmlflow)

[![License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

XMLFlow is a simple, fast, lightweight, and easy-to-use PHP library for building, parsing, and validating XML documents. It can also be used to build highly structured prompts for LLM.

Features
--------

[](#features)

1. Build XML documents programmatically with `XmlBuilder`
2. Parse any XML data into PHP arrays or objects
3. Validate the syntax and structure of your XML data

Installation
------------

[](#installation)

You can install XMLFlow via [Composer](https://getcomposer.org/):

```
composer require thenextcoder/xml-flow
```

You will then be able to import XMLFlow in your PHP scripts like this:

```
use TheNextCoder\XmlFlow\Builder\XmlBuilder;
```

Usage XmlBuilder
----------------

[](#usage-xmlbuilder)

### Example 1: Creating a Simple Document

[](#example-1-creating-a-simple-document)

This example demonstrates how to create a simple XML document with a custom root element and a few child elements.

```
use TheNextCoder\XmlFlow\Builder\XmlBuilder;

$xmlBuilder = new XmlBuilder('greeting');
$xmlBuilder->addElement('hello', 'World');
$xmlBuilder->addElement('goodbye', 'See you later');

echo $xmlBuilder->getFormattedXml();
```

**Output:**

```

  World
  See you later

```

### Example 2: Nested Elements with Attributes

[](#example-2-nested-elements-with-attributes)

This example shows how to create an XML document with nested elements and attributes, illustrating the use of XPath to specify the parent element.

```
$xmlBuilder = new XmlBuilder('book', ['isbn' => '000-0-00-000000-0']);
$chapter = $xmlBuilder->addElement('chapter', 'Introduction to XML', null, ['number' => '1']);
$xmlBuilder->addElement('section', 'Basics of XML', $chapter, ['id' => 'section-1']);

echo $xmlBuilder->getFormattedXml();
```

**Output:**

```

  Introduction to XML
    Basics of XML

```

### Example 3: Using XPath to Add Elements

[](#example-3-using-xpath-to-add-elements)

Illustrates adding elements to a specified parent using XPath, useful for more complex document structures.

```
$xmlBuilder = new XmlBuilder('library');
$xmlBuilder->addElement('shelf', null, null, ['id' => 'shelf-1']);
$xmlBuilder->addElement('book', 'XML for Dummies', '//shelf[@id="shelf-1"]', ['author' => 'John Doe']);

echo $xmlBuilder->getFormattedXml();
```

**Output:**

```

    XML for Dummies

```

### Example 4: Complex Document Creation

[](#example-4-complex-document-creation)

This example creates a more complex XML document, demonstrating the class's flexibility.

```
$xmlBuilder = new XmlBuilder('catalog');
$products = $xmlBuilder->addElement('products');
for ($i = 1; $i addElement('product', "Product $i", $products);
    $xmlBuilder->addElement('price', '$' . (10 * $i), $product, ['currency' => 'USD']);
}

echo $xmlBuilder->getFormattedXml();
```

**Output:**

```

      $10Product 1

      $20Product 2

      $30Product 3

```

Usage XmlParser
---------------

[](#usage-xmlparser)

### Example 1: Parsing XML Data

[](#example-1-parsing-xml-data)

This example demonstrates how to parse an XML string into a PHP array.

```
use TheNextCoder\XmlFlow\Parser\XmlParser;

$xmlString = title . "\n";
    echo "Priority: " . $xml->priority . "\n\n";

    // Extracting and listing subtasks
    echo "Subtasks:\n";
    foreach ($xml->subtasks->subtask as $subtask) {
        echo "- " . $subtask . "\n";
    }

    // Optional: Converting to an associative array
    $arrayRepresentation = $parser->toArray($xml);
    echo "\nArray Representation:\n";
    print_r($arrayRepresentation);

} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
```

Usage XmlValidator
------------------

[](#usage-xmlvalidator)

### Example 1: Validating XML Data

[](#example-1-validating-xml-data)

This example demonstrates how to validate the syntax and structure of an XML string.

```
use TheNextCoder\XmlFlow\Validator\XmlValidator;

$xmlContent = 'Example'; // Your XML content here

try {
    XmlValidator::validate($xmlContent);
    echo "The XML is well-formed.";
} catch (Exception $e) {
    echo "The XML is not well-formed. Errors: " . $e->getMessage();
}
```

Contributing
------------

[](#contributing)

Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for more details.

License
-------

[](#license)

XMLFlow is open-source software licensed under the [MIT license](LICENSE).

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

779d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/760738d85b12cd4b3b553aaba63bbaa7024884efb33671ad224d406d39c4fa70?d=identicon)[thenextcoder](/maintainers/thenextcoder)

---

Top Contributors

[![thenextcoder](https://avatars.githubusercontent.com/u/111305442?v=4)](https://github.com/thenextcoder "thenextcoder (3 commits)")

---

Tags

xmlXML Validatorxml-parserxml-builderllm xml prompt

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/thenextcoder-xml-flow/health.svg)

```
[![Health](https://phpackages.com/badges/thenextcoder-xml-flow/health.svg)](https://phpackages.com/packages/thenextcoder-xml-flow)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M226](/packages/masterminds-html5)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M849](/packages/jms-serializer)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M622](/packages/jms-serializer-bundle)[sabre/xml

sabre/xml is an XML library that you may not hate.

52832.2M131](/packages/sabre-xml)[goetas-webservices/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

4910.9M34](/packages/goetas-webservices-xsd2php-runtime)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
