Zaggs
1
Hi Guys!
I’m converting an XML feed into a multi-level array, but i’m not sure that json reads anything inside CDATA tags.
Here’s my XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<request>
<jobs>
<job>
<consultant><![CDATA[test@test.com]]></consultant>
<job_title>Job title #2</job_title>
<job_url>http://www.google.com</job_url>
<locations>
<location>Michigan</location>
</locations>
<license_types>
<license_type>Temporary</license_type>
</license_types>
<categories>
<category>Mining</category>
</categories>
<job_salary></job_salary>
<job_ote></job_ote>
<job_perks></job_perks>
<job_description>Test job do NOT apply.......</job_description>
</job>
<job>
<consultant><![CDATA[test@test.com]]></consultant>
<job_title>Job title #3</job_title>
<locations>
<location>Iowa</location>
</locations>
<license_types>
<license_type>Permanent</license_type>
<license_type>Temporary</license_type>
</license_types>
<categories>
<category>Mining</category>
<category>Hazmat</category>
</categories>
<job_salary>50000</job_salary>
<job_ote></job_ote>
<job_perks></job_perks>
<job_description>Test job do NOT apply.......</job_description>
</job>
</jobs>
</request>
PHP Code:
$simple = simplexml_load_file($_FILES['userfile']['tmp_name']);
$xml_array = json_decode(json_encode($simple),1);
print_r($xml_array); exit;
The array always has an empty value for consultant. How can I make json read XML with CDATA tags?
Have you done a print_r on $simple to see how it is being stored in that object?
Zaggs
3
Sure, this was the result:
SimpleXMLElement Object ( [jobs] => SimpleXMLElement Object ( [job] => Array ( [0] => SimpleXMLElement Object ( [consultant] => SimpleXMLElement Object ( ) [job_title] => Job title #2 [job_url] => http://www.google.com [locations] => SimpleXMLElement Object ( [location] => Michigan ) [license_types] => SimpleXMLElement Object ( [license_type] => Temporary ) [categories] => SimpleXMLElement Object ( [category] => Mining ) [job_salary] => SimpleXMLElement Object ( ) [job_ote] => SimpleXMLElement Object ( ) [job_perks] => SimpleXMLElement Object ( ) [job_description] => Test job do NOT apply....... ) [1] => SimpleXMLElement Object ( [consultant] => test@test.com [job_title] => Job title #3 [locations] => SimpleXMLElement Object ( [location] => Iowa ) [license_types] => SimpleXMLElement Object ( [license_type] => Array ( [0] => Permanent [1] => Temporary ) ) [categories] => SimpleXMLElement Object ( [category] => Array ( [0] => Mining [1] => Hazmat ) ) [job_salary] => 50000 [job_ote] => SimpleXMLElement Object ( ) [job_perks] => SimpleXMLElement Object ( ) [job_description] => Test job do NOT apply....... ) ) ) )
Seems your issue is loading the XML, not the json_encoding.
Your print_r for the $simple object has the following for the Consultant (an empty object)
[consultant] => SimpleXMLElement Object ( )
I found this in the comments on php.net
If you want CDATA in your object you should use LIBXML_NOCDATA
<?php
$xml = simplexml_load_file($file_xml, 'SimpleXMLElement',LIBXML_NOCDATA);
print_r($xml);
?>