├── .gitignore ├── composer.json ├── tests ├── example2.php └── example1.php ├── LICENSE.md ├── README.md └── lib └── XMLParser └── XMLParser.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | tmp/ -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jtrumbull/xml-parser", 3 | "description": "PHP Class that parses XML objects", 4 | "require": { 5 | "php": ">=5.3.0", 6 | "ext-simplexml": "*" 7 | }, 8 | "license": "MIT", 9 | "autoload": { 10 | "psr-4": { 11 | "XMLParser\\": "lib/XMLParser" 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/example2.php: -------------------------------------------------------------------------------- 1 | login_attempts = [ 14 | '2015/01/01 07:21:00', 15 | '2015/01/01 07:21:20', 16 | '2015/01/01 07:22:15' 17 | ]; 18 | $xml = XMLParser::encode($data); 19 | echo $xml->asXML(); 20 | -------------------------------------------------------------------------------- /tests/example1.php: -------------------------------------------------------------------------------- 1 | 'success', 7 | 'Person'=>array( 8 | 'attr:id'=>987654321, 9 | 'First Name'=>'John', 10 | 'Last Name'=>'Smith' 11 | ), 12 | 'Address'=>array( 13 | 'attr:'=>array( 14 | 'geo-coded'=>TRUE, 15 | 'lat'=>'0.0000', 16 | 'lon'=>'-0.0000' 17 | ), 18 | 'Street'=>'123 Main Street', 19 | 'City'=>'Somewhere', 20 | 'State'=>'DE', 21 | 'Zip'=>'12345' 22 | ), 23 | 'other'=>array( 24 | 'key'=>'value', 25 | array('value1','value2','value3') 26 | ) 27 | ),'response'); 28 | echo $xml->asXML(); 29 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2012-2014 Joseph Trumbull 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XMLParser 2 | I needed a quick way of outputting XML from an associative array on a few projects -this was the end result. I created 3 | this repository for later reference and fellow devs. At the time, It served it's purpose, however I am not actively 4 | developing this project -see the [Contributing](#contributing) section. 5 | 6 | ## Table of contents 7 | * [Installation](#installation) 8 | * [Usage](#usage) 9 | * [Documentation](#documentation) 10 | * [Contributing](#contributing) 11 | * [Community](#community) 12 | * [Copyright and license](#copyright-and-license) 13 | 14 | ## Installation 15 | * composer require jtrumbull/xml-parser 16 | 17 | ##Usage 18 | 19 | ```PHP 20 | header('Content-type: text/xml'); 21 | 22 | use XMLParser\XMLParser; 23 | 24 | $data = array( 25 | 'attr:status'=>'success', 26 | 'Person'=>array( 27 | 'attr:id'=>987654321, 28 | 'First Name'=>'John', 29 | 'Last Name'=>'Smith' 30 | ), 31 | 'Address'=>array( 32 | 'attr:'=>array( 33 | 'geo-coded'=>TRUE, 34 | 'lat'=>'0.0000', 35 | 'lon'=>'-0.0000' 36 | ), 37 | 'Street'=>'123 Main Street', 38 | 'City'=>'Somewhere', 39 | 'State'=>'DE', 40 | 'Zip'=>'12345' 41 | ), 42 | 'other'=>array( 43 | 'key'=>'value', 44 | array('value1','value2','value3') 45 | ) 46 | ); 47 | 48 | $xml = XMLParser::encode( $data , 'response' ); 49 | echo $xml->asXML(); 50 | ``` 51 | 52 | Will output: 53 | 54 | ```XML 55 | 56 | 57 | John 58 | Smith 59 | 60 |
61 | 123 Main Street 62 | Somewhere 63 | DE 64 | 12345 65 |
66 | 67 | value 68 | 69 | value1 70 | value2 71 | value3 72 | 73 | 74 |
75 | ``` 76 | 77 | ## Documentation 78 | 79 | ### XMLParser\XMLParser::encode() 80 | 81 | **Syntax:** 82 | ```PHP 83 | SimpleXMLElement XMLParser::encode( mixed $data [, string $root] ) 84 | ``` 85 | **Returns:** SimpleXMLElement 86 | 87 | ### XMLParser\XMLParser::decode() 88 | **Syntax:** 89 | ```PHP 90 | json XMLParser::decode( mixed $string ) 91 | ``` 92 | **Returns:** json 93 | 94 | ## Contributing 95 | 96 | Check out the [issue](https://github.com/jtrumbull/XMLParser/issues) tracker, if 97 | your issue or feature request has not addressed open a new issue. All pull requests are welcome. 98 | 99 | ## Community 100 | 101 | ## Copyright and license 102 | 103 | XML parser is distributed under the MIT License -see [LICENSE.md](https://github.com/jtrumbull/XMLParser/blob/master/LICENSE.md) 104 | -------------------------------------------------------------------------------- /lib/XMLParser/XMLParser.php: -------------------------------------------------------------------------------- 1 | "; 33 | $node = self::_formatName( is_null($root) ? 34 | self::$_defaultRootNode : 35 | $root); 36 | $value = ($is_array = is_array($data)) ? NULL : self::_formatValue($data); 37 | $xml_string .= "<$node>$value"; 38 | $xml = new SimpleXMLElement($xml_string); 39 | if ($is_array) { 40 | $xml = self::_addChildren($xml,$data); 41 | } 42 | } 43 | catch (Exception $e) { 44 | trigger_error($e->getMessage(), E_USER_ERROR); 45 | } 46 | return isset($xml) ? $xml : null; // isset() essentially to make editor happy. 47 | } 48 | 49 | public static function objectToArray($std) { 50 | if (is_object($std)) { 51 | $std = get_object_vars($std); 52 | } 53 | if (is_array($std)) { 54 | return array_map(['self','objectToArray'], $std); 55 | } 56 | else { 57 | return $std; 58 | } 59 | } 60 | 61 | private static function _validateEncodeData ($data) 62 | { 63 | if(is_object($data)){ // Try conversion 64 | $data = self::objectToArray($data); 65 | } 66 | if (is_object($data)) { // If it's still an object throw exception 67 | throw new InvalidArgumentException( 68 | "Invalid data type supplied for XMLParser::encode" 69 | ); 70 | } 71 | return $data; 72 | } 73 | 74 | private static function _addChildren (SimpleXMLElement $element, $data) 75 | { 76 | foreach ($data as $key => $value) { 77 | $regex = '/^'.self::$_defaultAttrTag.'([a-z0-9\._-]*)/'; 78 | $is_attr = preg_match($regex, $key, $attr); 79 | if ($is_attr) { 80 | if (is_array($value)) { 81 | foreach( $value as $k=>$v ) { 82 | $element->addAttribute( 83 | self::_formatName($k), 84 | self::_formatValue($v)); 85 | } 86 | } else { 87 | $element->addAttribute( 88 | self::_formatName($attr[1]), 89 | self::_formatValue($value) 90 | ); 91 | } 92 | continue; 93 | } 94 | $node = self::_formatName( 95 | is_numeric($key) ? 96 | (is_array($value) ? 97 | self::$_defaultListNode : 98 | self::$_defaultItemNode) : $key 99 | ); 100 | if (is_array($value)) { 101 | $child = $element->addChild($node); 102 | self::_addChildren($child,$value); 103 | continue; 104 | } 105 | $element->addChild($node, $value); 106 | } 107 | return $element; 108 | } 109 | 110 | private static function _formatName ($string) 111 | { 112 | $p = [ 113 | '/[^a-z0-9\._ -]/i' => '', 114 | '/(?=^[[0-9]\.\-\:^xml])/i' => self::$_defaultItemNode.'_', 115 | '/ /' => '_' 116 | ]; 117 | $string = preg_replace(array_keys($p), array_values($p), $string); 118 | if (self::$caseSensitive) { 119 | return $string; 120 | } 121 | return strtolower($string); 122 | } 123 | 124 | private static function _formatValue ($string) 125 | { 126 | $string = is_null($string) ? 'NULL' : $string; 127 | return is_bool($string) ? self::_bool($string) : $string; 128 | } 129 | 130 | private static function _bool ($bool) 131 | { 132 | return $bool ? 'TRUE' : 'FALSE'; 133 | } 134 | 135 | public static function decode ($xml) 136 | { 137 | if (!$xml instanceof SimpleXMLElement) 138 | { 139 | $xml = new SimpleXMLElement($xml); 140 | } 141 | return json_encode($xml); 142 | } 143 | 144 | } 145 | --------------------------------------------------------------------------------