imen Posted June 11, 2013 Share Posted June 11, 2013 (edited) 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_ratio=1&t_story=FriendInterviewStory in C:\Program Files (x86)\EasyPHP-5.3.9\www\fbcnx.php on line 1 Edited June 11, 2013 by imen Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/ Share on other sites More sharing options...
boompa Posted June 11, 2013 Share Posted June 11, 2013 All of the & characters in your strings must be converted to entities. For example, & becomes & You can do this with htmlentities() Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435390 Share on other sites More sharing options...
requinix Posted June 11, 2013 Share Posted June 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435402 Share on other sites More sharing options...
cpd Posted June 11, 2013 Share Posted June 11, 2013 And you're not ever returning anything from arrayToXML so calling asXML() on it will just raise an error. Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435411 Share on other sites More sharing options...
imen Posted June 11, 2013 Author Share Posted June 11, 2013 (edited) 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 June 11, 2013 by imen Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435421 Share on other sites More sharing options...
requinix Posted June 12, 2013 Share Posted June 12, 2013 hi still alivei thank you for your answer, I change the code with htmlspecialchars() , but it shows this errorThat is the error cpd was talking about. Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435579 Share on other sites More sharing options...
imen Posted June 12, 2013 Author Share Posted June 12, 2013 (edited) That is the error cpd was talking about. there is no solution to resolve this problem???? Edited June 12, 2013 by imen Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435580 Share on other sites More sharing options...
requinix Posted June 12, 2013 Share Posted June 12, 2013 Read cpd's reply. He told you exactly what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435582 Share on other sites More sharing options...
imen Posted June 12, 2013 Author Share Posted June 12, 2013 Read cpd's reply. He told you exactly what the problem is. sorry,but I did not understand what that means the cpd's??? Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435586 Share on other sites More sharing options...
requinix Posted June 12, 2013 Share Posted June 12, 2013 (edited) cpd is a member here. He posted in this thread. Didn't you see it? Scroll up. Edited June 12, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435587 Share on other sites More sharing options...
imen Posted June 12, 2013 Author Share Posted June 12, 2013 cpd is a member here. He posted in this thread. Didn't you see it? Scroll up. ah oki,thanks Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435588 Share on other sites More sharing options...
cpd Posted June 12, 2013 Share Posted June 12, 2013 Its very simple to fix. Return the SimpleXMLElement and then perform the ->asXML() method on it... Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435617 Share on other sites More sharing options...
imen Posted June 12, 2013 Author Share Posted June 12, 2013 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( ) ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435621 Share on other sites More sharing options...
cpd Posted June 12, 2013 Share Posted June 12, 2013 Dude, seriously... read the god damn answers people are giving you! Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435630 Share on other sites More sharing options...
imen Posted June 13, 2013 Author Share Posted June 13, 2013 sorry,but I did not understand Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435632 Share on other sites More sharing options...
Zane Posted June 13, 2013 Share Posted June 13, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435648 Share on other sites More sharing options...
imen Posted June 13, 2013 Author Share Posted June 13, 2013 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 inWarning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: unterminated entity reference since=1356700238&limit=25&__previous=1 Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1435707 Share on other sites More sharing options...
Zane Posted June 17, 2013 Share Posted June 17, 2013 (edited) 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 June 17, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/279040-convert-a-php-array-to-xml/#findComment-1436316 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.