In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to convert the xml of php into json". In the daily operation, I believe many people have doubts about how to convert the xml of php into json. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to convert the xml of php into json". Next, please follow the editor to study!
Php xml into json methods: 1, to create a PHP sample file; 2, to find the need to convert the xml file; 3, through the "function xmlToArray ($xml, $options = array ()) {.}" method to convert it to json.
Operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer
How to convert php xml into json?
XML to JSON
The following code demonstrates how to convert data from an xml file to data in Json format:
Function xmlToArray ($xml, $options = array ()) {$defaults = array ('namespaceSeparator' = >':', / / you may want this to be something other than a colon 'attributePrefix' = >' @', / / to distinguish between attributes and nodes with the same name 'alwaysArray' = > array (), / / array of xml tag names which should always become arrays' autoArray' = > true / / only create arrays for tags which appear more than once 'textContent' = >' $', / / key used for the text content of elements' autoText' = > true, / / skip textContent key if node has no attributes or child nodes' keySearch' = > false, / / optional search and replace on tag and attribute names' keyReplace' = > false / / replace values for above search values (as passed to str_replace () $options = array_merge ($defaults, $options); $namespaces = $xml- > getDocNamespaces (); $namespaces ['] = null; / / add base (empty) namespace / / get attributes from all namespaces $attributesArray = array () Foreach ($namespaces as $prefix = > $namespace) {foreach ($xml- > attributes ($namespace) as $attributeName = > $attribute) {/ / replace characters in attribute name if ($options ['keySearch']) $attributeName = str_replace ($options [' keySearch'], $options ['keyReplace'], $attributeName); $attributeKey = $options [' attributePrefix']. ($prefix? $prefix. $options ['namespaceSeparator']:'). $attributeName; $attributesArray [$attributeKey] = (string) $attribute;}} / / get child nodes from all namespaces $tagsArray = array (); foreach ($namespaces as $prefix = > $namespace) {foreach ($xml- > children ($namespace) as $childXml) {/ / recurse into child nodes $childArray = xmlToArray ($childXml, $options); list ($childTagName, $childProperties) = each ($childArray) / / replace characters in tag name if ($options ['keySearch']) $childTagName = str_replace ($options [' keySearch'], $options ['keyReplace'], $childTagName); / / add namespace prefix, if any if ($prefix) $childTagName = $prefix. $options ['namespaceSeparator']. $childTagName; if (! isset ($tagsArray [$childTagName]) {/ / only entry with this key / / test if tags of this type should always be arrays, no matter the element count $tagsArray [$childTagName] = in_array ($childTagName, $options ['alwaysArray']) | |! $options [' autoArray']? Array ($childProperties): $childProperties;} elseif (is_array ($tagsArray [$childTagName]) & & array_keys ($tagsArray [$childTagName]) = range (0, count ($tagsArray [$childTagName])-1) {/ / key already exists and is integer indexed array $tagsArray [$childTagName] [] = $childProperties } else {/ / key exists so convert to integer indexed array with previous value in position 0$ tagsArray [$childTagName] = array ($tagsArray [$childTagName], $childProperties);} / / get text content of node $textContentArray = array (); $plainText = trim ((string) $xml); if ($plainText! = =') $textContentArray [$options ['textContent']] = $plainText / / stick it all together $propertiesArray =! $options ['autoText'] | | $attributesArray | | $tagsArray | | ($plainText = ='')? Array_merge ($attributesArray, $tagsArray, $textContentArray): $plainText; / / return node as array return array ($xml- > getName () = > $propertiesArray);}
Use an example
$xmlNode = simplexml_load_file ('example.xml'); $arrayData = xmlToArray ($xmlNode); echo json_encode ($arrayData)
At this point, the study on "how to transform the xml of php into json" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.