Search code examples
phpxmlserializationxml-serializationpear

Pear XML Serializer and Attributes


is there a way, to "tell" the PEAR XML_Serializer, which properties it should serialize as attribute and which as sub element?

For example:

class User {
  public $id;
  public $name;
  public $address;
}

Should be serialized like this:

<User id="0">
  <name>John Doe</name>
  <address></address>
</User>

I thought about using the "XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES" Option, but unfortunately, I need some scalars as attribute and some as sub element.

Is there a way to tell the XML_Serializer how he should serialize the properties of the source class?


Solution

  • Done some code review and got the solution:

    $serializer->setOption(
      XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => array(
        "User" => array("id")
      )
    );
    

    Does the trick ... everything will be serialized as XML-Element but the "id" property of the User Element will be serialized as Attribute