如何在 PHP 中包含外部 XML 文件?
我需要一种在 PHP 中包含外部 XML 文件的方法,该文件不使用 simplexml 标签。此外,我还需要它与其他导入的 XML 集成,因此将文件头删除为
基本上有一个 PHP 类,其中包含动态创建 XML 元素的方法基于用户输入。例如,我可以创建一个名为“test”的节点,将“id=1”设置为属性并向其添加子节点。我基本上需要的是一种从其他文件中提取更多 XML 内容并让我的 PHP 脚本识别它的方法,从而能够在此导入的代码上调用方法。我尝试使用 php 的 fopen() 函数,但是,尽管它会将导入的 XML 打印到屏幕上,但一旦导入的代码开始,它就不会验证并发出错误信号。我无法使用 simpleXML 扩展有两个主要原因。首先,整个类是使用 PHP5 之前的 XML 处理编写的,我无法从头开始重写整个类,因为它是团队项目的一部分,其次,此类类具有无法使用 simpleXML 扩展复制的方法。
这是我生成的 XML:
返回:非法内容,第 3 行第 1 列,突出显示“<” “gg”标签的...(顺便说一下,这是从外部文件导入的部分。)
这是用于打印导入的 XML 的代码片段:
$file = simplexml_load_file($url);
foreach($file as $key => $value) {
echo "<" . $key . ">" . $value . "</" . $key . ">\n";
}
如何做到这一点?
附加说明:是的,服务器支持 PHP 5 (5.2.6),但代码是在 php5 之前编写的。
I need a way to include an external XML file in PHP which does not use simplexml tags. Furthermore, I'd also need it to integrate with other imported XMLs, hence removing file headers as <?XML version...>
Basically have a PHP class which includes methods to dynamically create XML elements based on user-input. For example, I could create a node called "test", set "id=1" as attribute and add child nodes to it. What I basically need, is a way to extract further XML content from other files and have my PHP script recognize it, hence being able to call methods on this imported code. I tried using php's fopen() function but, although it would print the imported XML to the screen, it would not validate and signal an error as soon as the imported code began. I cannot use simpleXML extension for two main reasons. Firstly, the entire class is written using Pre-PHP5 XML handling, and I cannot re-write the whole thing from scratch as it is part of a team-project, secondly, such class features methods which could not be replicated with simpleXML extension.
This is the XML I generate: <?xml version="1.0"?> <ga><dsa>hea</dsa><sda>eh</sda></ga> <gg><ds>he</ds><sd>eh</sd></gg>
And it returns: Illegal Content, Line 3 Column 1, highliting the "<" of the "gg" tag... (Which, by the way, is the part imported from the external file.)
This is a snippet of the code used to print imported XML:
$file = simplexml_load_file($url);
foreach($file as $key => $value) {
echo "<" . $key . ">" . $value . "</" . $key . ">\n";
}
How can this be done?
Additional note: Yes, the server suppors PHP 5 (5.2.6), but the code was written in pre-php5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的评论来看,我想说您会收到错误,因为有效的 XML 文档需要根元素。您的 XML 有两个:
和
,这意味着 XML 无效且无法解析。您应该通过添加根元素来修复 XML。然后解析错误就会消失。
另一种选择是将代码片段加载为 带有 DOM 的文档片段:
输出:
但请注意,这仍然不是完整的 XML 文档,因为它缺少根元素。
如果要将 DOM 树导入到另一个 DOM 树中,可以使用
DOMDocument::importNode
。要将其与上面的片段一起使用,您将执行此操作,这将导致
如果您有要导入的现有文档,您只需执行
此操作即可将片段附加为根节点的最后一个子节点。如果您想将其放在 DOM 树上的其他位置,则必须使用 Xpath 或
getElementsByTagName
或getElementsById
或childNodes
各个节点上的属性,然后附加到该节点。Judging from your comments I'd say you get an error because a valid XML document needs a root element. You XML has two:
<ga>
and<gg>
, which means the XML is invalid and cannot be parsed.You should fix your XML by adding a root element. Then the parsing errors will go away.
Another option would be to load the snippet as a document fragment with DOM:
Output:
But note that this is still not a complete XML document because it misses a root element.
If you want to import a DOM Tree into another, you can use
DOMDocument::importNode
. To use that with the fragment above, you would doThat would result in
If you have an existing document you want to import to, you would simply do
This would append the fragment as the last child of the root node. If you want to have it somewhere else on the DOM tree, you would have to traverse the DOM tree with Xpath or
getElementsByTagName
orgetElementsById
or thechildNodes
property on the various nodes and then append to that node instead.