简体   繁体   English

Pear XML序列化器和属性

[英]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? 有没有办法“告诉” PEAR XML_Serializer,它应该将哪些属性序列化为属性,将哪些序列化为子元素?

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. 我曾考虑过使用“ XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES”选项,但不幸的是,我需要一些标量作为属性,而另一些则作为子元素。

Is there a way to tell the XML_Serializer how he should serialize the properties of the source class? 有没有办法告诉XML_Serializer他应该如何序列化源类的属性?

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 有用吗...所有内容都将序列化为XML-Element,但是User元素的“ id”属性将序列化为Attribute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM