Jump to content

convert a php array to xml


imen

Recommended Posts

Hi,I have a problem, I have an associative array in php that contains multiple sub tables, I want to convert it into xml and especially simplexml, but I think I have a problem with everything accent special character, etc. they told me to change the encoding "ISO-8859-1", but it does not work, can you help me please??

 

function arrayToXML( SimpleXMLElement &$xml, $array ) {

foreach( $array as $name => $value ) {
if( !is_array($value) )
//$xml->addChild( $name, $value );
$xml ->$name = $value;
else {
$child = $xml->addChild( $name );   
arrayToXML( $child, $value );
}
}
}
echo arrayToXML($xml_page_info,$wall)->asXML();
exit( ) ;
?>

Here is the array

 

 

Array
(
[data] => Array
(
[0] => Array
(
[id] => 1509985731_10200242899264388
[created_time] => 2012-12-28T13:10:38+0000
)

[1] => Array
(
[id] => 1509985731_314820988634241
[name] => Timeline Photos
[link] => http://www.facebook.com/photo.php?fbid=435081696563337&set=a.153490351389141.38937.153383058066537&type=1
[created_time] => 2012-12-26T17:29:39+0000
)

[2] => Array
(
[id] => 1509985731_10200228490104168
[name] => Friend Interview
[link] => http://apps.facebook.com/friendinterview/y/?ctx=sa&s_app=326914550570&s_uid=1509985731&said=1022&spid=1457&src=101&t_link=app&t_ratio=1&t_story=FriendInterviewStory
[created_time] => 2012-12-26T02:44:15+0000
)
 

 

 

 

Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: unterminated entity reference s_app=326914550570&s_uid=1509985731&said=1022&spid=1457&src=101&t_link=app&t_rat‌​io=1&t_story=FriendInterviewStory in C:\Program Files (x86)\EasyPHP-5.3.9\www\fbcnx.php on line 1
Edited by imen
Link to comment
Share on other sites

All of the & characters in your strings must be converted to entities. For example, & becomes &

 

You can do this with htmlentities()

But only the &s. If you use htmlentities() or htmlspecialchars() then you'll be double-encoding some characters.

 

http://stackoverflow.com/questions/552957/rationale-behind-simplexmlelements-handling-of-text-values-in-addchild-and-adda

http://www.php.net/manual/en/simplexmlelement.addchild.php#112204

 

imen, are you sure you posted the right code? The error message talks about the value given to addChild() but the only time you call that you don't provide the value.

Link to comment
Share on other sites

hi still alivei thank you for your answer, I change the code with htmlspecialchars() , but it shows this error

 

 

Fatal error : Call to une fonction asXML membre () sur un non-objet dans C: \ Program Files (x86) \ EasyPHP-5.3.9 \ www \ fbcnx.php on line 14

 

<?php

$xml = new SimpleXMLElement( '<pageinfo/>' );

// function call to convert array to xml
function arrayToXML( SimpleXMLElement &$xml, $array ) {
foreach( $array as $name => $value ) {
if( !is_array($value) )
$xml->addChild( $name, htmlspecialchars($value ));
 
else {
$child = $xml->addChild( $name );   
arrayToXML( $child, $value );
}
}
}
echo (arrayToXML($xml,$wall)->asXML());
exit( ) ;
?>
Edited by imen
Link to comment
Share on other sites

I modified my code but it shows me this error

 

 

Fatal error : Call to une fonction asXML membre () sur un non-objet dans C: \ Program Files (x86) \ EasyPHP-5.3.9 \ www \ fbcnx.php on line 14 

 

my code is:

 

<?php

$xml_page_info = new SimpleXMLElement( '<pageinfo/>' );

// function defination to convert array to xml
 function array_to_xml($student_info, &$xml_page_info)
{
    foreach($student_info as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_page_info->addChild("$key");
                array_to_xml($value, $subnode);
            }
            else{
                array_to_xml($value, $xml_page_info);
            }
        }
        else {
            $xml_page_info->addChild($key,$value);
        }
    }
return $xml_page_info;
echo array_to_xml($xml_page_info,$wall)->asXML();
exit( ) ;
 
?>
Link to comment
Share on other sites

You are getting the non-object error because of this line

echo array_to_xml($xml_page_info,$wall)->asXML();

You have your parameters backwards... According to this line

 function array_to_xml($student_info, &$xml_page_info)

$xml_page_info goes at the end.

Link to comment
Share on other sites

You are getting the non-object error because of this line

echo array_to_xml($xml_page_info,$wall)->asXML();

You have your parameters backwards... According to this line

 function array_to_xml($student_info, &$xml_page_info)

$xml_page_info goes at the end.

I modified my code by

 

echo array_to_xml($wall,$xml_page_info)->asXML();

 but it shows me this error

 

 

Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: unterminated entity reference s_app=326914550570&s_uid=1509985731&said=1022&spid=1457&src=101&t_link=app&t_ratio=1&t_story=FriendInterviewStory in

Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: unterminated entity reference since=1356700238&limit=25&__previous=1

Link to comment
Share on other sites

I went to google and searched for "undetermined entity reference" and the first result, which was the PHP manual,  was this


6 years ago
To avoid warning message "unterminated entity reference" you may use htmlentities() for escaping supplied value:
<?php
//...
$dom->createElement('name'htmlentities($text))
//...
?>

 

Omit the fact that it shows up at the very bottom of the page.  Ctrl+F is your best friend in searching for information.

Edited by Zane
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.