PHPackages                             originphp/xml - 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. originphp/xml

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

originphp/xml
=============

OriginPHP XML

2.1.0(4y ago)113.3k↓68.8%6MITPHPPHP &gt;=7.3.0CI failing

Since Oct 12Pushed 4y ago1 watchersCompare

[ Source](https://github.com/originphp/xml)[ Packagist](https://packagist.org/packages/originphp/xml)[ Docs](https://www.originphp.com)[ RSS](/packages/originphp-xml/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (8)Used By (6)

Xml
===

[](#xml)

[![license](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)](https://camo.githubusercontent.com/6fdb99389fe9d9e8a5c197002a191ace7c8b12a2020c0fa5756cf17aa08a4966/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874477265656e2e737667)[![build](https://github.com/originphp/xml/workflows/CI/badge.svg)](https://github.com/originphp/xml/actions)[![coverage](https://camo.githubusercontent.com/a2449a2f675e40c833567d4495b5b7ddc8159229f26c3c8ed89eb6768584dab2/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6f726967696e7068702f786d6c2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/originphp/xml?branch=master)

XML class to make making reading and writing XML easy.

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

[](#installation)

To install this package

```
$ composer require originphp/xml

```

Create XML from an array
------------------------

[](#create-xml-from-an-array)

You always must pass an array with 1 root element.

To set attributes, prefix the key with @. You can also set the text value of an element using @.

```
use Origin\Xml\Xml;
$data = [
    'post' => [
        '@category' => 'how tos', // to set attribute use @
        'id' => 12345,
        'title' => 'How to create an XML block',
        'body' =>  Xml::cdata('A quick brown fox jumps of a lazy dog.'),
        'author' => [
            'name' => 'James'
            ]
        ]
];

$xml = Xml::fromArray($data);
```

This will return the following:

```

    12345
    How to create an XML block
    "A quick brown fox jumps of a lazy dog."]]>

        James

```

For data which needed to be wrapped in CDATA, pass the data through `Xml::cdata($string)`.

You can also pass options when creating XML from an array.

```
$xml = Xml::fromArray($data,[
        'version' => '1.0',
        'encoding' => 'UTF-8',
        'pretty' => true
        ]);
```

Sometimes you might need to repeat the tags in XML, so you can do so like this.

```
$data = [
'charges' => [
    'charge' => [
        [
            'amount' => 10,
            'description' => 'Shipping',
        ],
        [
            'amount' => 35,
            'description' => 'Tax',
        ],
        ]
    ]
];
```

Which will output this:

```

        10
        Shipping

        35
        Tax

```

Here is an example of setting attributes (prefix the key with @) and text values (set the key to @).

```
$data = [
    'task' => [
        '@id' => 128,
        'name' => 'Buy milk',
        '@' => 'some text'
    ]
];
```

Which gives this:

```

some textBuy milk
```

Create an Array from XML
------------------------

[](#create-an-array-from-xml)

You can also create an array from the XML using the `toArray` method.

```
$xml = 'YouMeReminder  Buy milk';
$array = Xml::toArray($xml);
```

Namespaces
==========

[](#namespaces)

The xml utility also works with namespaces.

To set a generic namespace set the key `xmlns:`.

```
$data = [
'book' => [
    'xmlns:' => 'http://www.w3.org/1999/xhtml',
    'title' => 'Its a Wonderful Day'
    ]
];
$xml = Xml::fromArray($data);
```

This will output this:

```

Its a Wonderful Day

```

You can setup custom namespaces like this:

```
$data = [
    'student:record' => [
        'xmlns:student' => 'https://www.originphp.com/student',
        'student:name' => 'James',
        'student:phone' => '07986 123 4567'
    ]
];
```

Which will give you this

```

    James
    07986 123 4567

```

Lets take an example from [w3.org](https://www.w3.org/TR/xml-names/) and re-create this using an array.

So this is what we want to produce:

```

    Cheaper by the Dozen
    1568491379

```

To do this in an array (you could use the toArray method if you have the existing XML) set it up as follows.

```
$data = [
    'book' => [
        'xmlns:' => 'urn:loc.gov:books',
        'xmlns:isbn' => 'urn:ISBN:0-395-36341-6',
        'title' => 'Cheaper by the Dozen',
        'isbn:number' => '1568491379'
    ]
];
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity57

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

Every ~99 days

Recently: every ~77 days

Total

7

Last Release

1804d ago

Major Versions

1.0.4 → 2.0.02021-01-04

PHP version history (3 changes)1.0.0PHP ^7.2.0

1.0.4PHP &gt;=7.2.0

2.0.0PHP &gt;=7.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e8a821333d9c7b7bc2ad3d164d142f65cd3912dea78033d31f76b0f19ba8a0c?d=identicon)[originphp](/maintainers/originphp)

---

Top Contributors

[![jamielsharief](https://avatars.githubusercontent.com/u/20553479?v=4)](https://github.com/jamielsharief "jamielsharief (20 commits)")

---

Tags

xmlparseroriginPHP

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[imangazaliev/didom

Simple and fast HTML parser

2.2k2.3M64](/packages/imangazaliev-didom)[orchestra/parser

XML Document Parser for Laravel and PHP

4581.7M5](/packages/orchestra-parser)[laravie/parser

XML Document Parser for PHP

2342.1M8](/packages/laravie-parser)[goetas-webservices/xsd-reader

Read any XML Schema (XSD) programmatically with PHP

624.7M15](/packages/goetas-webservices-xsd-reader)[vipnytt/sitemapparser

XML Sitemap parser class compliant with the Sitemaps.org protocol.

772.2M10](/packages/vipnytt-sitemapparser)

PHPackages © 2026

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