简体   繁体   English

将php数组转换为xml

[英]convert a php array to xml

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.` 我有一个问题,这使我发疯了2天,我在php中有一个关联数组,其中包含多个子表,我想将其转换为xml尤其是simplexml,但是我认为所有带有特殊字符的问题都存在问题,等等。他们告诉我更改编码“ ISO-8859-1”,但是它不起作用,您能帮我吗。

<?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 警告:SimpleXMLElement :: __ construct()[simplexmlelement .-- construct]:实体:第1行:解析器错误:预期的开始标记,在C:\\ Program Files(x86)\\ EasyPHP-5.3.9 \\ www中找不到“ <” \\ fbcnx.php在第4行

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ?> in C:\\Program Files (x86)\\EasyPHP-5.3.9\\www\\fbcnx.php on line 4 警告:在第4行的C:\\ Program Files(x86)\\ EasyPHP-5.3.9 \\ www \\ fbcnx.php中,SimpleXMLElement :: __ construct()[simplexmlelement .-- construct]:?>

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in C:\\Program Files (x86)\\EasyPHP-5.3.9\\www\\fbcnx.php on line 4 警告:在第4行的C:\\ Program Files(x86)\\ EasyPHP-5.3.9 \\ www \\ fbcnx.php中,SimpleXMLElement :: __ construct()[simplexmlelement .-- construct]:^

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(' 致命错误:C:\\ Program Files(x86)\\ EasyPHP-5.3.9 \\ www \\ fbcnx.php:4中未捕获的异常'Exception',消息为'String无法解析为XML',堆栈跟踪:#0 C:\\程序文件(x86)\\ EasyPHP-5.3.9 \\ www \\ fbcnx.php(4):SimpleXMLElement-> __ construct('

the ?> in the SimpleXMLElement constructor is causing the problem. SimpleXMLElement构造函数中的?>引起了问题。 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 传递给类的构造函数的字符串必须是XML文档的根元素或xml文件的路径,请参见http://www.php.net/manual/zh/simplexmlelement.construct.php

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

SimpleXMLElement:__construct requires a well-formed XML string. SimpleXMLElement:__construct需要格式正确的XML字符串。 This should work: 这应该工作:

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

Note that SimpleXML always accepts UTF-8 encoded strings, not ISO-8859-1 encoded strings. 请注意,SimpleXML始终接受UTF-8编码的字符串,而不接受ISO-8859-1编码的字符串。 If necessary, use utf8_encode to convert an ISO-8859-1 string to UTF-8. 如有必要,请使用utf8_encode将ISO-8859-1字符串转换为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. 如果生成的XML使用ISO-8859-1编码而不是UTF-8编码很重要,请保留<?xml ... ?>声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM