我是靠谱客的博主 完美香烟,这篇文章主要介绍XML内容要有根标签:Extra content at the end of the document一、场景重现二、可能的问题,现在分享给大家,希望可以做个参考。
- 一、场景重现
- 二、可能的问题
- 1、xml 没有根节点
- 2、Linux下,无权限读取文件的xml内容
一、场景重现
- 代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public function test() { //该xml有2个根节点:user 和 other $xml = '<?xml version="1.0" encoding="utf-8"?><user><name>姓名</name><age>99</age><hobby>study</hobby><like>you guess</like></user><other>1</other>'; $array = self::xmlToArray($xml); var_export($array); die; } //将xml转为array public function xmlToArray($xml) { if (!$xml) { die('xml数据异常'); } //禁止引用外部xml实体 防XXE注入 libxml_disable_entity_loader(true);//禁止引用外部xml实体 //将XML转为array $arrayData = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $arrayData; }
- 打印
复制代码
1
2Entity: line 1: parser error : Extra content at the end of the document
二、可能的问题
1、xml 没有根节点
- xml根节点不存在,增加一个自定义根节点即可~(比如
root
) - 代码
复制代码
1
2
3
4
5
6//加一个根标签root,防止转换时由于根标签有多个报错:Extra content at the end of the document in file $xml = '<?xml version="1.0" encoding="utf-8"?><root><user><name>姓名</name><age>99</age><hobby>study</hobby><like>you guess</like></user><other>1</other></root>'; $array = self::xmlToArray($xml); var_export($array); die;
- 打印(注意:
root
并没有被转换出来~)
复制代码
1
2array ( 'user' => array ( 'name' => '姓名', 'age' => '99', 'hobby' => 'study', 'like' => 'you guess', ), 'other' => '1', )
2、Linux下,无权限读取文件的xml内容
如果是Linux环境,可能没有读取xml文件的权限,把文件夹权限设置为
0777
- 1、代码实现
复制代码
1
2
3mkdir('文件地址', 0777, true); //true:递归创建目录 chmod('文件地址', 0777);
- 2、Linux命令实现:递归给文件夹/子文件夹可读可选最高权限
0777
复制代码
1
2chmod -R 777 /var/www/web/public/2021.txt
最后
以上就是完美香烟最近收集整理的关于XML内容要有根标签:Extra content at the end of the document一、场景重现二、可能的问题的全部内容,更多相关XML内容要有根标签:Extra内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复