Deserializing or parse XML response in Symfony2

5.9k views Asked by At

I am calling a API method through cURL and I got this response:

<?xml version="1.0" encoding="UTF-8"?>
<jobInfo
    xmlns="http://www.force.com/2009/06/asyncapi/dataload">
    <id>75080000002s5siAAA</id>
    <operation>query</operation>
    <object>User</object>
    <createdById>00580000008ReolAAC</createdById>
    <createdDate>2015-06-23T13:03:01.000Z</createdDate>
    <systemModstamp>2015-06-23T13:03:01.000Z</systemModstamp>
    <state>Open</state>
    <concurrencyMode>Parallel</concurrencyMode>
    <contentType>CSV</contentType>
    <numberBatchesQueued>0</numberBatchesQueued>
    <numberBatchesInProgress>0</numberBatchesInProgress>
    <numberBatchesCompleted>0</numberBatchesCompleted>
    <numberBatchesFailed>0</numberBatchesFailed>
    <numberBatchesTotal>0</numberBatchesTotal>
    <numberRecordsProcessed>0</numberRecordsProcessed>
    <numberRetries>0</numberRetries>
    <apiVersion>34.0</apiVersion>
    <numberRecordsFailed>0</numberRecordsFailed>
    <totalProcessingTime>0</totalProcessingTime>
    <apiActiveProcessingTime>0</apiActiveProcessingTime>
    <apexProcessingTime>0</apexProcessingTime>
</jobInfo>

I want to access|parse that result in a easy way and I don't know if I should deserializing the XML or just try to read it using some PHP native XML function. So ideas on this first doubt?

If it is better to deserialize the XML then I have read this post "Deserializing XML with JMSSerializerBundle in Symfony2" and is not clear at all for me if I will need an entity to achieve that. Also this other topic and still confuse to me. Any advice on that? Experiences? Suggestions?

2

There are 2 answers

0
Maltronic On BEST ANSWER

It depends on your intention. If you want to directly push part or all of the XML to an entity/document object for saving to a database then the JMSSerializerBundle can do this very smartly and is definitely the best way to do it.

If however you just want to extract one or two fields from the xml and use them in other business logic then simply loading the xml into a SimpleXML object is often simpler.

0
Pi Wi On

You can use any object (not only an entity) to deserialize the XML file to. It is recommended to deserialize to a object because you probably want to use it in an OOP way.

This is a well explained blog about JMS serializer (bundle) including a XML deserialization example in a user object: http://johnkary.net/blog/deserializing-xml-with-jms-serializer-bundle/