ASHXML

The WP eCommerce ASHXML class.

Defined (1)

The class is defined in the following location(s).

/wpsc-includes/shipping.helper.php  
  1. class ASHXML{ 
  2. /** 
  3. * This function iterates over the keys from an array, if there is any 
  4. * non-numeric keys, it is associative, else it is a "list" 
  5. * @since 0.0.1 
  6. * @param array $data 
  7. * @return boolean 
  8. */ 
  9. function _is_list($data) { 
  10. $is_num = TRUE; 
  11. if (!is_array($data)) { return FALSE; } 
  12. foreach((array)$data as $key=>$value) { 
  13. if (!is_numeric($key)) { 
  14. $is_num = FALSE; 
  15. return $is_num; 
  16.  
  17. /** 
  18. * Helper function that parses xml attributes from an array into a string 
  19. * @since 0.0.1 
  20. * @param array $attrs 
  21. * @return string 
  22. */ 
  23. function _parse_attrs($attrs) { 
  24. $attrString = ""; 
  25. foreach($attrs as $key=>$value) { 
  26. $attrString .= sprintf(" %s=\"%s\" ", $key, $value); 
  27. return $attrString; 
  28.  
  29. /** 
  30. * Accepts an associative array and produces an XML document 
  31. * Attributes are supported by this function, see example. 
  32. * @since 0.0.1 
  33. * @param array $data Associative array, can be multi-dimensional 
  34. * @return string The resulting XML document 
  35. * Example: (Not a valid USPS Request) 
  36. * $test = array("RateRequestV4"=>array("@attr"=>array("ID"=>"This is my ID", "PASSWORD"=>"this is my pass") 
  37. * "Package"=>array(array("Weight"=>1.0, "Service"=>"First Class"),  
  38. * array("Weight"=>1.0, "Service"=>"PRIORITY")) 
  39. * ) 
  40. * ); 
  41. * $xml_doc = $this->build_message($test); 
  42. * -- Result -- 
  43. * <RateRequestV4 ID="This is my ID" PASSWORD="this is my pass"> 
  44. * <Package> 
  45. * <Weight>1.0</Weight> 
  46. * <Service>First Class</Service> 
  47. * </Package> 
  48. * <Package> 
  49. * <Weight>1.0</Weight> 
  50. * <Service>PRIORITY</Service> 
  51. * </Package> 
  52. * </RateRequestV4> 
  53. */ 
  54. function build_message(array $data) { 
  55. $xmlString = ""; 
  56. foreach($data as $node=>$value) { 
  57. if ($node == "@attr") { continue; } 
  58. $value_is_list = $this->_is_list($value); 
  59. if (is_array($value) && !$value_is_list) { 
  60. $attrs = ""; 
  61. if (array_key_exists("@attr", $value)) { 
  62. $attrs = $this->_parse_attrs($value["@attr"]); 
  63. unset($value["@attr"]); 
  64. $xmlString .= "<".$node." ".$attrs.">\n"; 
  65. $xmlString .= $this->build_message($value); 
  66. $xmlString .= "</".$node.">\n"; 
  67.  
  68. }elseif(is_array($value) && $value_is_list) { 
  69. foreach($value as $iter_node) { 
  70. $temp = array($node=>$iter_node); 
  71. $xmlString .= $this->build_message($temp); 
  72. }else{ 
  73. if (trim($value) != "") { 
  74. $xmlString .= "<".$node.">".$value."</".$node.">\n"; 
  75. }else{ 
  76. $xmlString .= "<".$node."/>\n"; 
  77. return $xmlString; 
  78.  
  79. /** 
  80. * Sets the header content type to text/xml and displays a given XML doc 
  81. * @since 0.0.1 
  82. * @param string $xml_doc 
  83. */ 
  84. function show_xml($xml_doc) { 
  85. header("content-type: text/xml"); 
  86. print $xml_doc; 
  87.  
  88. /** 
  89. * This is a helper function that retrieves an XML element from the 
  90. * provided document. Since we are trying to keep PHP4 support 
  91. * I cannot use simpleXML 
  92. * @since 0.0.1 
  93. * @param string $element The element to find in document 
  94. * @param string $document The XML Document to search 
  95. */ 
  96. function get($element, $document) { 
  97. preg_match_all('/<'.$element.'.*?>(.*?)<\/'.$element.'>/', $document, $matches); 
  98.  
  99. if (count($matches) > 1) { 
  100. return $matches[1]; 
  101. return FALSE; 
  102.