Open jzfgo opened 4 years ago
To achieve this, you will also need a custom deserializer for Query
, or if you are willing to switch from arrays to classes for Query
, you could use the class mapper, which has a feature to deserialize properties to single properties, or properties for which there can be more than 1
Hi,
Thanks for your response.
I've tried making a custom deserializer for Query
but what I need is to create a new node (Contexts
) to wrap the Context
elements and I don't know how to do that.
I thought about using classes too, but it looks like overcomplicating things to just parse a simple request.
Best, Javier.
Hi @evert,
As per your suggestion, I've switched to classes and, since I also intend write XML responses, I've decided to go all in and make a full blown library (its purpose is to handle updating pricing and availability in the Google Hotel Ads service):
https://github.com/bahiazul/google-hotel-ads-xml
However, I'm in a similar situation as before:
I've added an examples/
folder in the repo that I'm currently using for debugging. If you run php examples/query.php
you can see the issues with the <Context>
element when parsing and the <Property>
and <Child>
elements when writing.
For more info, all element classes inherit from a Base
class that implements the xmlSerialize()
and xmlDeserialize()
methods.
There is also a Service
class that inherits from the Sabre one where I'm defining all the mappings (VOs for simple elements and additional mappings for elements with attributes).
What am I doing wrong? I've read the docs and the source code from top to bottom and I still can't figure it out. Any help would be appreciated.
Best, Javier.
I think in the case of $Context
, the issue is that that property on the Query class is initialized as null
. sabre/xml will try to figure out if something should be treated as an array by looking at it's value, and null
implies to sabre/xml that it isn't.
It looks like that's an issue at at least a couple of places, so start there
Thanks @evert , I've made the modifications that you suggested but unfortunately I haven't seen any changes.
However, I've observed that, when turning off mapping to Query
in Service
, saber/xml
does interpret multiple <Context>
elements (as it should), which makes me think that the problem is with my custom deserializer.
Sadly this puts me in the same situation as before switching to classes. On the bright side, I now know where the problems exactly are. I just need to find how to fix them 😅.
Best, Javier.
Hi,
I have this weird looking XML and I'm trying to convert it to a simple array.
So far I've managed to do most of it with
keyValue
andrepeatingElements
deserializers and a custom one for the<Child/>
element.My problem is with the
<Context/>
elements. Since they aren't wrapped in a parent element, I can't treat them like repeating elements and if I parse<Query/>
’s children like key-value elements, I only get the first<Context/>
element.Any suggestions?
This an example of the XML that I'm trying to parse:
And this is the result that I would like to achieve:
Best, Javier.