├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Exception ├── ExceptionInterface.php ├── RecursionException.php └── RuntimeException.php └── Xml2Json.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file, in reverse chronological order by release. 4 | 5 | ## 3.1.3 - TBD 6 | 7 | ### Added 8 | 9 | - Nothing. 10 | 11 | ### Changed 12 | 13 | - Nothing. 14 | 15 | ### Deprecated 16 | 17 | - Nothing. 18 | 19 | ### Removed 20 | 21 | - Nothing. 22 | 23 | ### Fixed 24 | 25 | - Nothing. 26 | 27 | ## 3.1.2 - 2019-10-16 28 | 29 | ### Added 30 | 31 | - [#10](https://github.com/zendframework/zend-xml2json/pull/10) adds support for PHP 7.3. 32 | 33 | ### Changed 34 | 35 | - Nothing. 36 | 37 | ### Deprecated 38 | 39 | - Nothing. 40 | 41 | ### Removed 42 | 43 | - Nothing. 44 | 45 | ### Fixed 46 | 47 | - Nothing. 48 | 49 | ## 3.1.1 - 2018-04-30 50 | 51 | ### Added 52 | 53 | - Nothing. 54 | 55 | ### Changed 56 | 57 | - [#8](https://github.com/zendframework/zend-xml2json/pull/8) ensures that tests against PHP 7.2 are not allowed to fail. 58 | 59 | ### Deprecated 60 | 61 | - Nothing. 62 | 63 | ### Removed 64 | 65 | - Nothing. 66 | 67 | ### Fixed 68 | 69 | - Nothing. 70 | 71 | ## 3.1.0 - 2017-10-16 72 | 73 | ### Added 74 | 75 | - [#5](https://github.com/zendframework/zend-xml2json/pull/5) adds support for 76 | PHP 7.1 and 7.2. 77 | 78 | ### Changed 79 | 80 | - Nothing. 81 | 82 | ### Deprecated 83 | 84 | - Nothing. 85 | 86 | ### Removed 87 | 88 | - [#1](https://github.com/zendframework/zend-xml2json/pull/1) removes an 89 | obsolete `@deprecated` annotation. 90 | 91 | - [#5](https://github.com/zendframework/zend-xml2json/pull/5) removes support 92 | for PHP 5.5. The code likely still runs on that version, but we no longer 93 | support it. 94 | 95 | - [#5](https://github.com/zendframework/zend-xml2json/pull/5) removes support 96 | for HHVM. 97 | 98 | ### Fixed 99 | 100 | - Nothing. 101 | 102 | ## 3.0.0 - 2016-03-31 103 | 104 | First release as a standalone component. Previous versions were shipped as 105 | `Zend\Json\Json::toXml()` within the [zend-json](https://github.com/zendframework/zend-json) 106 | package. 107 | 108 | ### Added 109 | 110 | - Nothing. 111 | 112 | ### Deprecated 113 | 114 | - Nothing. 115 | 116 | ### Removed 117 | 118 | - Nothing. 119 | 120 | ### Fixed 121 | 122 | - Nothing. 123 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2005-2019, Zend Technologies USA, Inc. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | - Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | - Redistributions in binary form must reproduce the above copyright notice, this 11 | list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | - Neither the name of Zend Technologies USA, Inc. nor the names of its 15 | contributors may be used to endorse or promote products derived from this 16 | software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zend-xml2json 2 | 3 | > ## Repository abandoned 2019-12-31 4 | > 5 | > This repository has moved to [laminas/laminas-xml2json](https://github.com/laminas/laminas-xml2json). 6 | 7 | [![Build Status](https://secure.travis-ci.org/zendframework/zend-xml2json.svg?branch=master)](https://secure.travis-ci.org/zendframework/zend-xml2json) 8 | [![Coverage Status](https://coveralls.io/repos/github/zendframework/zend-xml2json/badge.svg?branch=master)](https://coveralls.io/github/zendframework/zend-xml2json?branch=master) 9 | 10 | zend-xml2json provides functionality for converting XML structures to JSON. 11 | 12 | ## Installation 13 | 14 | Run the following to install this library: 15 | 16 | ```bash 17 | $ composer require zendframework/zend-xml2json 18 | ``` 19 | 20 | ## Documentation 21 | 22 | Browse the documentation online at https://docs.zendframework.com/zend-xml2json/ 23 | 24 | ## Support 25 | 26 | * [Issues](https://github.com/zendframework/zend-xml2json/issues/) 27 | * [Chat](https://zendframework-slack.herokuapp.com/) 28 | * [Forum](https://discourse.zendframework.com/) 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zendframework/zend-xml2json", 3 | "description": "Provides functionality for converting XML to JSON, optionally including XML attributes", 4 | "license": "BSD-3-Clause", 5 | "keywords": [ 6 | "zf", 7 | "zendframework", 8 | "json", 9 | "xml" 10 | ], 11 | "support": { 12 | "docs": "https://docs.zendframework.com/zend-xml2json/", 13 | "issues": "https://github.com/zendframework/zend-xml2json/issues", 14 | "source": "https://github.com/zendframework/zend-xml2json", 15 | "rss": "https://github.com/zendframework/zend-xml2json/releases.atom", 16 | "chat": "https://zendframework-slack.herokuapp.com", 17 | "forum": "https://discourse.zendframework.com/c/questions/components" 18 | }, 19 | "require": { 20 | "php": "^5.6 || ^7.0", 21 | "zendframework/zend-json": "^2.6.1 || ^3.0", 22 | "zendframework/zendxml": "^1.0.2" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "^5.7.27 || ^6.5.8 || ^7.1.4", 26 | "zendframework/zend-coding-standard": "~1.0.0" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Zend\\Xml2Json\\": "src/" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "ZendTest\\Xml2Json\\": "test/" 36 | } 37 | }, 38 | "config": { 39 | "sort-packages": true 40 | }, 41 | "extra": { 42 | "branch-alias": { 43 | "dev-master": "3.1.x-dev", 44 | "dev-develop": "3.2.x-dev" 45 | } 46 | }, 47 | "scripts": { 48 | "check": [ 49 | "@cs-check", 50 | "@test" 51 | ], 52 | "cs-check": "phpcs", 53 | "cs-fix": "phpcbf", 54 | "test": "phpunit --colors=always", 55 | "test-coverage": "phpunit --colors=always --coverage-clover clover.xml" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Exception/ExceptionInterface.php: -------------------------------------------------------------------------------- 1 | static::$maxRecursionDepthAllowed) { 118 | // XML tree is too deep. Exit now by throwing an exception. 119 | throw new Exception\RecursionException(sprintf( 120 | 'Function processXml exceeded the allowed recursion depth of %d', 121 | static::$maxRecursionDepthAllowed 122 | )); 123 | } 124 | 125 | $children = $simpleXmlElementObject->children(); 126 | $name = $simpleXmlElementObject->getName(); 127 | $value = static::getXmlValue($simpleXmlElementObject); 128 | $attributes = (array) $simpleXmlElementObject->attributes(); 129 | 130 | if (! $children) { 131 | if (! empty($attributes) && ! $ignoreXmlAttributes) { 132 | foreach ($attributes['@attributes'] as $k => $v) { 133 | $attributes['@attributes'][$k] = static::getXmlValue($v); 134 | } 135 | 136 | if (! empty($value)) { 137 | $attributes['@text'] = $value; 138 | } 139 | 140 | return [$name => $attributes]; 141 | } 142 | 143 | return [$name => $value]; 144 | } 145 | 146 | $childArray = []; 147 | foreach ($children as $child) { 148 | $childname = $child->getName(); 149 | $element = static::processXml($child, $ignoreXmlAttributes, $recursionDepth + 1); 150 | 151 | if (! array_key_exists($childname, $childArray)) { 152 | $childArray[$childname] = $element[$childname]; 153 | continue; 154 | } 155 | 156 | if (empty($subChild[$childname])) { 157 | $childArray[$childname] = [$childArray[$childname]]; 158 | $subChild[$childname] = true; 159 | } 160 | 161 | $childArray[$childname][] = $element[$childname]; 162 | } 163 | 164 | if (! empty($attributes) && ! $ignoreXmlAttributes) { 165 | foreach ($attributes['@attributes'] as $k => $v) { 166 | $attributes['@attributes'][$k] = static::getXmlValue($v); 167 | } 168 | $childArray['@attributes'] = $attributes['@attributes']; 169 | } 170 | 171 | if (! empty($value)) { 172 | $childArray['@text'] = $value; 173 | } 174 | 175 | return [$name => $childArray]; 176 | } 177 | } 178 | --------------------------------------------------------------------------------