PHP - XML parse error: Extra content at the end of the document
2012-11-25 16:40 youxin 阅读(3050) 评论(0) 编辑 收藏 举报php xml simplexml_load_string simplexml_load_file parse error: extra content at the end of the document
Having not worked with parsing xml in a VERY long time (and the last time i did it, it was much harder) I ran into a problem problem when trying to load in some dynamic XML snippets that are being created by an api I'm writing.
The scenario -
Long story short, I created XML modules that each represent different tasks. When the application picks and chooses which of the modules needs to be compiled together, it dynamically creates one final XML document and spits that back as the response to making a certain call into the API.
Example modules:
1) XML_HEADER:
<?xml version="1.0" encoding="utf-8"?>
2) XML_CAR:
<car>
<door></door>
<tires></tires>
<tires></tires>
</car>
3) XML_BIKE:
<bike>
<frame></frame>
<spokes></spokes>
<tires></tires>
<spokes></spokes>
<tires></tires>
</bike>
4) XML_APPLE:
<apple>
<color></color>
<type></type>
<type></type>
</apple>
Alright, so you get the point... Let's say the app, put together modules 1, 2, and 4 to create the following xml:
<?xml version="1.0" encoding="utf-8"?>
<car>
<door></door>
<tires></tires>
<tires></tires>
</car>
<apple>
<color></color>
<type></type>
<type></type>
</apple>
And I loaded this xml into a variable like so:
$xml = simplexml_load_string($the_xml_above);
The following error message would occur: Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 3: parser error : Extra content at the end of the document in the_file_name on line 3
Hopefully you are smarter than I was and see the problem right away.
Unfortunately for me, I worked on each module separately (and they are much more complex than the examples above) and I got lost in each one, not thinking of what the final compiled XML would look like.
So I searched teh google machine and came up completely empty. It wasn't until 20 minutes after searching that I output the xml on screen, stared at it for another 5 minutes that I came up with the problem.
THE SOLUTION?... I never specified a root element (like HTML's open <body> and close</body> tags). The the example XML above worked just fine like so:
<?xml version="1.0" encoding="utf-8"?>
<document>
<document>
<car>
<door></door>
<tires></tires>
<tires></tires>
</car>
<apple>
<color><color>
<type><type>
<type><type>
</apple>
</document>
Be sure to always specify your root element in an XML file, there are a couple ways of doing this, those ways you can google. q确保在你的xml文件里有一股根元素root element.
enjoy
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2011-11-25 win7下录制视频
2011-11-25 新建窗口代码
2011-11-25 浏览器新开一个窗口
2011-11-25 Gallery上传视频