0
votes

I have a problem, which makes me go crazy 2 days, I have an associative array in php that contains multiple sub tables, I want to turn 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.`

<?php
  header('Content-type: text/html');
  $xml_student_info = new SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-1" ?>');
// function defination to convert array to xml

  function array_to_xml($student_info, $xml_student_info) {
   foreach($student_info as $key => $value) {
    if(is_array($value)) {
        if(!is_numeric($key)){
            $subnode = $xml_student_info->addChild("$key");
            array_to_xml($value, $subnode);
        }
        else{
            array_to_xml($value, $xml_student_info);
        }
    }
    else {
        $xml_student_info->addChild($key,$value);
    }
}
return $xml_student_info; }
    //function call to convert array to xml
    echo array_to_xml($wall,$xml_student_info)->asXML();
     exit( ) ;

         ?>

Et voici la réponse:

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '<' not found in C:\Program Files (x86)\EasyPHP-5.3.9\www\fbcnx.php on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ?> in C:\Program Files (x86)\EasyPHP-5.3.9\www\fbcnx.php on line 4

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in C:\Program Files (x86)\EasyPHP-5.3.9\www\fbcnx.php on line 4

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\Program Files (x86)\EasyPHP-5.3.9\www\fbcnx.php:4 Stack trace: #0 C:\Program Files (x86)\EasyPHP-5.3.9\www\fbcnx.php(4): SimpleXMLElement->__construct('

2
The error is indicating that the opening PHP tag is not expected – is this code from a PHP file?zeantsoi

2 Answers

0
votes

the ?> in the SimpleXMLElement constructor is causing the problem. The string you pass to the constructor of the class needs to be the root element for the XML document or the path to a xml file see http://www.php.net/manual/en/simplexmlelement.construct.php

0
votes
$xml_student_info = new SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-1" ?>');

SimpleXMLElement:__construct requires a well-formed XML string. This should work:

$xml_student_info = new SimpleXMLElement('<studentinfo/>');

Note that SimpleXML always accepts UTF-8 encoded strings, not ISO-8859-1 encoded strings. If necessary, use utf8_encode to convert an ISO-8859-1 string to UTF-8.

If it is important that the generated XML uses ISO-8859-1 encoding rather than UTF-8 encoding, leave the <?xml ... ?> declaration in place.