类将PHP对象转换为XML

类将PHP对象转换为XML,php,xml,oop,Php,Xml,Oop,我写了下面这节课。对于只有一个级别的简单对象似乎可以正常工作,但是当对象有多个级别(多个数组)并且XML都乱七八糟时,就不能正常工作。有谁能帮我改进一下,这样它就可以处理任何对象了吗 class XMLGenerator { function __construct($obj,$root, $element, $fullXML = true) { $array = $this->object_2_array($obj);

我写了下面这节课。对于只有一个级别的简单对象似乎可以正常工作,但是当对象有多个级别(多个数组)并且XML都乱七八糟时,就不能正常工作。有谁能帮我改进一下,这样它就可以处理任何对象了吗

class XMLGenerator
{ 
       function __construct($obj,$root, $element, $fullXML = true) {     

          $array = $this->object_2_array($obj);
          $this->output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
          $this->output .= $this->make($array, $root, $element, $fullXML);

       }

        //convert objects into arrays
        function object_2_array($result){

            $array = array();

            foreach ($result as $key => $value){

                if (is_object($value)){
                    $array[$key]=XMLGenerator::object_2_array($value);
                } elseif (is_array($value)){
                    $array[$key]=XMLGenerator::object_2_array($value);
                } else {
                    $array[$key]=$value;
                }
            }
            return $array;
        }   

       //make XML
       function make($array, $root, $element, $fullXML) {
          if (is_numeric($root)) {
             $xml = "<{$element}>\n";
          } else {
              $xml = "<{$root}>\n";
          }

          foreach ($array as $key => $value) {
             if (is_array($value)) {
                 if ($element == 'options'){  //workaround for orders 3 level problem, need to rethink this - LT
                    $xml .= $this->make($value, $key, $key, $fullXML); 
                 } else {
                    $xml .= $this->make($value, $element, $key, $fullXML);  
                 }              
             } else {

                 //any fields with HTML need wrapping in CDATA
                 if (($key === 'Description')||( $key === 'productDescription' )){
                    $value = '<![CDATA['. $value .']]>';
                 //remove any chars XML doesnt like  
                 } else {
                    $value = htmlentities($value,ENT_QUOTES, 'UTF-8');
                    $value = functions::xml_entities($value);    
                 }
                 //decide on node name

                if (is_numeric($key)) {
                   $xml .= "<{$root}>{$value}</{$root}>\n";
                } else {
                   $xml .= "<{$key}>{$value}</{$key}>\n";
                }           
             }
          }

          if (is_numeric($root)) {
             $xml .= "</{$element}>\n";
          } else {
              $xml .= "</{$root}>\n";
          } 

          return $xml;
       } 


       //save XML to file
       function saveFile($file){
            //create DOM to ensure all XML is valid before writing to file
            $doc = new DOMDocument();
            $doc->loadXML($this->output);

            if ($doc->save("$file")){
                return TRUE;
            } else {
                return FALSE;
            }
       }

}
        [productDescription] => Test
        [productName] => Test
        [UPC] => 123
    )
[skID:protected] => 89137
[orderID:protected] => 482325
[order] => Array
    (
        [id] => 482325
        [customer] => 491936
        [net] => 1565.98
        [vat] => 274.05
        [billing_address] => Address Object
            (
                [db_connection:protected] => msSqlConnect Object
                    (
                        [con] => 
                        [dbName] => 
                    )

                [custID:protected] => 491936
                [addID:protected] => 156928
                [fields] => Array
                    (
                        [id] => 156928
                        [surname] => test
                        [forename] => test
                        [add1] => 89 testRoad
                        [add2] => 
                        [city] => City
                        [country] => GB
                        [postcode] => POSTCODE
                    )

            )
        [items] => Array
            (
                [0] => Array
                    (
                        [id] => 716549
                        [headerID] => 482325
                        [productID] => 4084620
                        [net] => 22.99
                        [vat] => 4.0233
                        [qty] => 1
                        [options] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763217
                                        [optionCost] => 100
                                        [optionVAT] => 17.5
                                    )

                                [1] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763241
                                        [optionCost] => 10
                                        [optionVAT] => 1.75
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 716551
                        [headerID] => 482325
                        [productID] => 3779074
                        [net] => 1400
                        [vat] => 245
                        [qty] => 1
                        [options] => 
                    )
            )
    ) )
)

        [productDescription] => Test
        [productName] => Test
        [UPC] => 123
    )
[skID:protected] => 89137
[orderID:protected] => 482325
[order] => Array
    (
        [id] => 482325
        [customer] => 491936
        [net] => 1565.98
        [vat] => 274.05
        [billing_address] => Address Object
            (
                [db_connection:protected] => msSqlConnect Object
                    (
                        [con] => 
                        [dbName] => 
                    )

                [custID:protected] => 491936
                [addID:protected] => 156928
                [fields] => Array
                    (
                        [id] => 156928
                        [surname] => test
                        [forename] => test
                        [add1] => 89 testRoad
                        [add2] => 
                        [city] => City
                        [country] => GB
                        [postcode] => POSTCODE
                    )

            )
        [items] => Array
            (
                [0] => Array
                    (
                        [id] => 716549
                        [headerID] => 482325
                        [productID] => 4084620
                        [net] => 22.99
                        [vat] => 4.0233
                        [qty] => 1
                        [options] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763217
                                        [optionCost] => 100
                                        [optionVAT] => 17.5
                                    )

                                [1] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763241
                                        [optionCost] => 10
                                        [optionVAT] => 1.75
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 716551
                        [headerID] => 482325
                        [productID] => 3779074
                        [net] => 1400
                        [vat] => 245
                        [qty] => 1
                        [options] => 
                    )
            )
    ) )
下面的方法根本不起作用

        [productDescription] => Test
        [productName] => Test
        [UPC] => 123
    )
[skID:protected] => 89137
[orderID:protected] => 482325
[order] => Array
    (
        [id] => 482325
        [customer] => 491936
        [net] => 1565.98
        [vat] => 274.05
        [billing_address] => Address Object
            (
                [db_connection:protected] => msSqlConnect Object
                    (
                        [con] => 
                        [dbName] => 
                    )

                [custID:protected] => 491936
                [addID:protected] => 156928
                [fields] => Array
                    (
                        [id] => 156928
                        [surname] => test
                        [forename] => test
                        [add1] => 89 testRoad
                        [add2] => 
                        [city] => City
                        [country] => GB
                        [postcode] => POSTCODE
                    )

            )
        [items] => Array
            (
                [0] => Array
                    (
                        [id] => 716549
                        [headerID] => 482325
                        [productID] => 4084620
                        [net] => 22.99
                        [vat] => 4.0233
                        [qty] => 1
                        [options] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763217
                                        [optionCost] => 100
                                        [optionVAT] => 17.5
                                    )

                                [1] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763241
                                        [optionCost] => 10
                                        [optionVAT] => 1.75
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 716551
                        [headerID] => 482325
                        [productID] => 3779074
                        [net] => 1400
                        [vat] => 245
                        [qty] => 1
                        [options] => 
                    )
            )
    ) )
订单对象( [db_连接:受保护]=>msSqlConnect对象 ( [con]=> [dbName]=> )

        [productDescription] => Test
        [productName] => Test
        [UPC] => 123
    )
[skID:protected] => 89137
[orderID:protected] => 482325
[order] => Array
    (
        [id] => 482325
        [customer] => 491936
        [net] => 1565.98
        [vat] => 274.05
        [billing_address] => Address Object
            (
                [db_connection:protected] => msSqlConnect Object
                    (
                        [con] => 
                        [dbName] => 
                    )

                [custID:protected] => 491936
                [addID:protected] => 156928
                [fields] => Array
                    (
                        [id] => 156928
                        [surname] => test
                        [forename] => test
                        [add1] => 89 testRoad
                        [add2] => 
                        [city] => City
                        [country] => GB
                        [postcode] => POSTCODE
                    )

            )
        [items] => Array
            (
                [0] => Array
                    (
                        [id] => 716549
                        [headerID] => 482325
                        [productID] => 4084620
                        [net] => 22.99
                        [vat] => 4.0233
                        [qty] => 1
                        [options] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763217
                                        [optionCost] => 100
                                        [optionVAT] => 17.5
                                    )

                                [1] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763241
                                        [optionCost] => 10
                                        [optionVAT] => 1.75
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 716551
                        [headerID] => 482325
                        [productID] => 3779074
                        [net] => 1400
                        [vat] => 245
                        [qty] => 1
                        [options] => 
                    )
            )
    ) )

非常感谢您的帮助。

多个级别需要递归处理—因为您不知道前面的级别数。在执行递归时,您还需要注意打开了哪些XML元素等等

        [productDescription] => Test
        [productName] => Test
        [UPC] => 123
    )
[skID:protected] => 89137
[orderID:protected] => 482325
[order] => Array
    (
        [id] => 482325
        [customer] => 491936
        [net] => 1565.98
        [vat] => 274.05
        [billing_address] => Address Object
            (
                [db_connection:protected] => msSqlConnect Object
                    (
                        [con] => 
                        [dbName] => 
                    )

                [custID:protected] => 491936
                [addID:protected] => 156928
                [fields] => Array
                    (
                        [id] => 156928
                        [surname] => test
                        [forename] => test
                        [add1] => 89 testRoad
                        [add2] => 
                        [city] => City
                        [country] => GB
                        [postcode] => POSTCODE
                    )

            )
        [items] => Array
            (
                [0] => Array
                    (
                        [id] => 716549
                        [headerID] => 482325
                        [productID] => 4084620
                        [net] => 22.99
                        [vat] => 4.0233
                        [qty] => 1
                        [options] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763217
                                        [optionCost] => 100
                                        [optionVAT] => 17.5
                                    )

                                [1] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763241
                                        [optionCost] => 10
                                        [optionVAT] => 1.75
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 716551
                        [headerID] => 482325
                        [productID] => 3779074
                        [net] => 1400
                        [vat] => 245
                        [qty] => 1
                        [options] => 
                    )
            )
    ) )
您要做的是将一个PHP对象序列化为XML。您不是第一个需要它的人,PHP附带了一个遵循WDDX规范的XML序列化程序,例如函数:

        [productDescription] => Test
        [productName] => Test
        [UPC] => 123
    )
[skID:protected] => 89137
[orderID:protected] => 482325
[order] => Array
    (
        [id] => 482325
        [customer] => 491936
        [net] => 1565.98
        [vat] => 274.05
        [billing_address] => Address Object
            (
                [db_connection:protected] => msSqlConnect Object
                    (
                        [con] => 
                        [dbName] => 
                    )

                [custID:protected] => 491936
                [addID:protected] => 156928
                [fields] => Array
                    (
                        [id] => 156928
                        [surname] => test
                        [forename] => test
                        [add1] => 89 testRoad
                        [add2] => 
                        [city] => City
                        [country] => GB
                        [postcode] => POSTCODE
                    )

            )
        [items] => Array
            (
                [0] => Array
                    (
                        [id] => 716549
                        [headerID] => 482325
                        [productID] => 4084620
                        [net] => 22.99
                        [vat] => 4.0233
                        [qty] => 1
                        [options] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763217
                                        [optionCost] => 100
                                        [optionVAT] => 17.5
                                    )

                                [1] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763241
                                        [optionCost] => 10
                                        [optionVAT] => 1.75
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 716551
                        [headerID] => 482325
                        [productID] => 3779074
                        [net] => 1400
                        [vat] => 245
                        [qty] => 1
                        [options] => 
                    )
            )
    ) )
$object = (object) array('hello' => (object) array('value' => 'world') );

echo wddx_serialize_value($object);
这将给出以下XML():

        [productDescription] => Test
        [productName] => Test
        [UPC] => 123
    )
[skID:protected] => 89137
[orderID:protected] => 482325
[order] => Array
    (
        [id] => 482325
        [customer] => 491936
        [net] => 1565.98
        [vat] => 274.05
        [billing_address] => Address Object
            (
                [db_connection:protected] => msSqlConnect Object
                    (
                        [con] => 
                        [dbName] => 
                    )

                [custID:protected] => 491936
                [addID:protected] => 156928
                [fields] => Array
                    (
                        [id] => 156928
                        [surname] => test
                        [forename] => test
                        [add1] => 89 testRoad
                        [add2] => 
                        [city] => City
                        [country] => GB
                        [postcode] => POSTCODE
                    )

            )
        [items] => Array
            (
                [0] => Array
                    (
                        [id] => 716549
                        [headerID] => 482325
                        [productID] => 4084620
                        [net] => 22.99
                        [vat] => 4.0233
                        [qty] => 1
                        [options] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763217
                                        [optionCost] => 100
                                        [optionVAT] => 17.5
                                    )

                                [1] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763241
                                        [optionCost] => 10
                                        [optionVAT] => 1.75
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 716551
                        [headerID] => 482325
                        [productID] => 3779074
                        [net] => 1400
                        [vat] => 245
                        [qty] => 1
                        [options] => 
                    )
            )
    ) )

标准类
标准类
世界

如果需要不同的输出,则需要自己编写序列化您可以找到对XML输出进行序列化的现有PHP代码。

这意味着什么?SOAP通常是将对象转换为XML的最佳方式。通常是为了将其发送到Web服务,但没有什么可以阻止您仅使用呈现的XML。或者使用XSLT更改WDDX数据包XMLInterest,从未听说过关于WDDX。它与SOAP有什么不同?SOAP:“在计算机网络中实现Web服务时交换结构化信息的规范”-WDDX:“通过Web轻松地彼此交换数据。”.SOAP更为复杂,并且使用RPC,而WDDX只涉及序列化,我认为它更为古老,实际上并不复杂。@MikeB:“WDDX和XML-RPC都创建于1998年,是SOAP和Web服务的前身。SOAP借用了WDDX的信封/头/体结构和传输+交互中立性,以及XML-RPC的HTTP和RPC绑定。“-@hakre非常酷。SOAP有时会很麻烦,所以很高兴知道有一个不太复杂的解决方案可以序列化xml中的对象:)
        [productDescription] => Test
        [productName] => Test
        [UPC] => 123
    )
[skID:protected] => 89137
[orderID:protected] => 482325
[order] => Array
    (
        [id] => 482325
        [customer] => 491936
        [net] => 1565.98
        [vat] => 274.05
        [billing_address] => Address Object
            (
                [db_connection:protected] => msSqlConnect Object
                    (
                        [con] => 
                        [dbName] => 
                    )

                [custID:protected] => 491936
                [addID:protected] => 156928
                [fields] => Array
                    (
                        [id] => 156928
                        [surname] => test
                        [forename] => test
                        [add1] => 89 testRoad
                        [add2] => 
                        [city] => City
                        [country] => GB
                        [postcode] => POSTCODE
                    )

            )
        [items] => Array
            (
                [0] => Array
                    (
                        [id] => 716549
                        [headerID] => 482325
                        [productID] => 4084620
                        [net] => 22.99
                        [vat] => 4.0233
                        [qty] => 1
                        [options] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763217
                                        [optionCost] => 100
                                        [optionVAT] => 17.5
                                    )

                                [1] => Array
                                    (
                                        [id] => 
                                        [orderDetailsID] => 716549
                                        [optionid] => 763241
                                        [optionCost] => 10
                                        [optionVAT] => 1.75
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 716551
                        [headerID] => 482325
                        [productID] => 3779074
                        [net] => 1400
                        [vat] => 245
                        [qty] => 1
                        [options] => 
                    )
            )
    ) )