You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
4.5 KiB

4 years ago
  1. <?php
  2. $xmlin = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
  3. <document><service>PROGRAM</service><name>BuscaProductos</name>
  4. <program>/u/general/dionisio/consulxml/xbtotpil.sh</program>
  5. <info>ESTA ES INFORMACION RETORNADA POR serverDMUG</info>
  6. <pileta><id>40</id><desc>VINO-TtaA-Aspiran</desc><cap>2700.00</cap><saldo>2700.00</saldo><alcohol>12.70</alcohol><azucar>0.000</azucar><acvol>0.51</acvol> </pileta><STATUS>OK</STATUS></document>";
  7. $objxml = simplexml_load_string($xmlin);
  8. $arrayData = xmlToArray($objxml, array(
  9. 'alwaysArray' => array('pileta'),
  10. 'keySearch' => '.', //MongoDB doesn't allow dots in keys
  11. 'keyReplace' => '_' //so replace with underscores
  12. ));
  13. echo json_encode($arrayData);
  14. function xmlToArray($xml, $options = array()) {
  15. $defaults = array(
  16. 'namespaceSeparator' => ':',//you may want this to be something other than a colon
  17. 'attributePrefix' => '@', //to distinguish between attributes and nodes with the same name
  18. 'alwaysArray' => array(), //array of xml tag names which should always become arrays
  19. 'autoArray' => true, //only create arrays for tags which appear more than once
  20. 'textContent' => '$', //key used for the text content of elements
  21. 'autoText' => true, //skip textContent key if node has no attributes or child nodes
  22. 'keySearch' => false, //optional search and replace on tag and attribute names
  23. 'keyReplace' => false //replace values for above search values (as passed to str_replace())
  24. );
  25. $options = array_merge($defaults, $options);
  26. $namespaces = $xml->getDocNamespaces();
  27. $namespaces[''] = null; //add base (empty) namespace
  28. //get attributes from all namespaces
  29. $attributesArray = array();
  30. foreach ($namespaces as $prefix => $namespace) {
  31. foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
  32. //replace characters in attribute name
  33. if ($options['keySearch']) $attributeName =
  34. str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
  35. $attributeKey = $options['attributePrefix']
  36. . ($prefix ? $prefix . $options['namespaceSeparator'] : '')
  37. . $attributeName;
  38. $attributesArray[$attributeKey] = (string)$attribute;
  39. }
  40. }
  41. //get child nodes from all namespaces
  42. $tagsArray = array();
  43. foreach ($namespaces as $prefix => $namespace) {
  44. foreach ($xml->children($namespace) as $childXml) {
  45. //recurse into child nodes
  46. $childArray = xmlToArray($childXml, $options);
  47. list($childTagName, $childProperties) = each($childArray);
  48. //replace characters in tag name
  49. if ($options['keySearch']) $childTagName =
  50. str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
  51. //add namespace prefix, if any
  52. if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;
  53. if (!isset($tagsArray[$childTagName])) {
  54. //only entry with this key
  55. //test if tags of this type should always be arrays, no matter the element count
  56. $tagsArray[$childTagName] =
  57. in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']
  58. ? array($childProperties) : $childProperties;
  59. } elseif (
  60. is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])
  61. === range(0, count($tagsArray[$childTagName]) - 1)
  62. ) {
  63. //key already exists and is integer indexed array
  64. $tagsArray[$childTagName][] = $childProperties;
  65. } else {
  66. //key exists so convert to integer indexed array with previous value in position 0
  67. $tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);
  68. }
  69. }
  70. }
  71. //get text content of node
  72. $textContentArray = array();
  73. $plainText = trim((string)$xml);
  74. if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;
  75. //stick it all together
  76. $propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')
  77. ? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;
  78. //return node as array
  79. return array(
  80. $xml->getName() => $propertiesArray
  81. );
  82. }
  83. ?>