Skip to content

Latest commit

 

History

History
179 lines (159 loc) · 4.99 KB

README.md

File metadata and controls

179 lines (159 loc) · 4.99 KB

XmlJson v1.0.0

Convert XML strings to array and JSON arrays to XML

It's realy easy to use!

Functions

function XmlJson(string $attributePrefix = "@", string $innerTextLable = "#text"); // Constructor

function toJson(SimpleXMLElement $xml): array;
function toXml(array $json, string $root = "root"): SimpleXMLElement;

Convert method's table

Pattern XML JSON
1 <e/> "e": null
2 <e>text</e> "e": "text"
3 <e name="value" /> "e":{"@name": "value"}
4 <e name="value">text</e> "e": { "@name": "value", "#text": "text" }
5 <e> <a>text</a> <b>text</b> </e> "e": { "a": "text", "b": "text" }
6 <e> <a>text</a> <a>text</a> </e> "e": { "a": ["text", "text"] }
7 <e> text <a>text</a> </e> "e": { "#text": "text", "a": "text" }

Sample

Make instance

$xmlJson = new XmlJson();

Sample XML

$xmlString = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="web" cover="paperback">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
XML;

Convert XML string to SimpleXMLElement

$xmlDoc = new SimpleXMLElement($xmlString)

Convert XML document to JSON array

$jsonArr = $xmlJson->toJson($xmlDoc);

//Result:
/*
{
    "book": [
        {
            "title": {
                "@lang": "en",
                "#text": "Everyday Italian"
            },
            "author": "Giada De Laurentiis",
            "year": "2005",
            "price": "30.00",
            "@category": "cooking"
        },
        {
            "title": {
                "@lang": "en",
                "#text": "Harry Potter"
            },
            "author": "J K. Rowling",
            "year": "2005",
            "price": "29.99",
            "@category": "children"
        },
        {
            "title": {
                "@lang": "en",
                "#text": "XQuery Kick Start"
            },
            "author": [
                "James McGovern",
                "Per Bothner",
                "Kurt Cagle",
                "James Linn",
                "Vaidyanathan Nagarajan"
            ],
            "year": "2003",
            "price": "49.99",
            "@category": "web"
        },
        {
            "title": {
                "@lang": "en",
                "#text": "Learning XML"
            },
            "author": "Erik T. Ray",
            "year": "2003",
            "price": "39.95",
            "@category": "web",
            "@cover": "paperback"
        }
    ]
}
*/

Convert JSON array to XML document (SimpleXMLElement)

$xmlDoc_revert = $xmlJson->toXml($jsonArr, "bookstore");

/*
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="web">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
        <year>2003</year>
        <price>49.99</price>
    </book>
    <book category="web" cover="paperback">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
</bookstore>
*/