2

I am currently trying to rebuilt some java code in PHP. The code builds XML from a given object and sends XML requests to a backend. The backend delivers the response. Now I want to convert the XML response in a php object of a certain complex class. I used SimpleXml to convert the string to the class XmlResponse but i can only access the getter and setter from the XmlResponse class and not the other class inside the XmlResponse Class. In Java I simply use JAXB but I am not sure if something like this is possible. The the problem is, that the data tag can change from request to request, delivering other objects, depending on the request!

I am using the following setup :

  • PHP 5.6 or 7

  • Zend 2 or 3

Greetings!

At the end I want something like this :

<?php
    $responseObject = convertStringToObject($responseString, XMLResponse::class);
    $result = $responseObject->getResult();
    $data = $responseObject->getResult()->getData();
    $status = $responseObject->getResult()->getStatus();
    $user = $responseObject->getResult()->getData()->getUser()

?>

The Response looks like this :

<response>
    <transaction_id>1234</transaction_id>
    <result>
        <status>
            <code>1</code>
            <msg>Success</msg>
        </status>
        <data>
            <user>
                <fname>Jon</lname>
                <lname>Doe</lname>
                <birthday>1986-08-01 00:00:00</birthday>
            </user>
        </data>
    </result>
</response>  

Now I have the following classes in PHP.

XmlResponse

class XmlResponse {

    public $result; #XmlResult Class
    public $transaction_id; #string

    public function getArrayCopy() {
      return get_object_vars($this);
    }

     //getter + setter

 }

XmlResult

class XmlResult {

    public $data; #XmlData Class
    public $status; #XmlStatus Class

    public function getArrayCopy() {
        return get_object_vars($this); 
    }

    //getter + setter

}

XmlStatus

class XmlStatus {

    public $code; #string
    public $msg; #string

    public function getArrayCopy() {
    return get_object_vars($this);
    }

    //getter + setter

}

XmlData

class XmlData {

    public $user; #User Class
    public $car; #Car Class
    public $member; #Member Class
    public $foo; #Foo Class

    public function getArrayCopy() {
        return get_object_vars($this);
    }

    //getter + setter

}

User

class User {

    public $fname; #string
    public $lname; #string
    public $birthday; #string

    public function getArrayCopy() {
        return get_object_vars($this);
    }

    //getter + setter

}
Ephenodrom
  • 1,797
  • 2
  • 16
  • 28
  • Have you considered using xpath to query your xml? --> http://php.net/manual/en/simplexmlelement.xpath.php – Crisp Aug 10 '16 at 18:37

2 Answers2

1

I found the best and easiest solution for me! There is a library called Sabre that supports converting objects to XML and XML to objects : http://sabre.io/xml/

It comes up with some nice interfaces that you can implement within your object class, that controls the XmlSerialization and XmlDeserialization. So you define the mapping for XML tags -> Object variables and Object variables -> XML tags within you class. After that you only have to define the mapping of the XML tag to the object class and the object class to XML tag.

use Sabre\Xml\Service;
$xml = "<xml></xml>" #your xml as string
// For XML to object
$service->elementMap = [
    'XMLTAGNAME' => 'MyObject::class',
    'XMLTAGNAME' => 'MyObject2::class',
];
$service->parse($xml);

// For object to XML
$service->classMap = [
    'XMLTAGNAME' => MyObject::class,
    'XMLTAGNAME' => MyObject2::class,
];
$service->write("PARENTXMLTAGNAME", $object);

Within the class it will look something like this :

class MyObject implements XmlDeserializable, XmlSerializable {

    public $variable;

    function xmlSerialize(Writer $writer) {
        $writer->write([
            'variable' => $this->variable,
        ]);
    }

    static function xmlDeserialize(Reader $reader) {
        $object = new MyObject();
        $keyValue = \Sabre\Xml\Deserializer\keyValue($reader);
        if (isset($keyValue['variable'])) {
            $object->variable = $keyValue['variable'];
        }
        return $object;
    }
    //Getter and Setter
}

Installation :

`composer require sabre/xml`

API Documentation :

XmlSerialization : http://sabre.io/xml/reading/

XmlDeserialization : http://sabre.io/xml/writing/

Ephenodrom
  • 1,797
  • 2
  • 16
  • 28
0

Is serialization something you are looking for. For example first match when I searched:

How-to Serialize/Unserialize a SimpleXML Object?

Community
  • 1
  • 1
japesu
  • 134
  • 1
  • 10