SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simplexml_load_file and json

    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:
    Code:
    <?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:

    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?

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Have you done a print_r on $simple to see how it is being stored in that object?

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Have you done a print_r on $simple to see how it is being stored in that object?
    Sure, this was the result:

    Code:
    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....... ) ) ) )

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    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)
    Code:
    [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 Code:
    <?php 
    $xml 
    simplexml_load_file($file_xml'SimpleXMLElement',LIBXML_NOCDATA); 
        
        
    print_r($xml); 
    ?>

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect, thank you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •