解析嵌入在 XML 中的 PHP
我读过这个问题/答案,但是没有提到这是一个“坏主意” ”,不幸的是,任何包含 parse、xml 和 php 词的搜索查询通常都会生成有关使用 PHP 解析 XML 的信息(通过<一href="http://ca.php.net/simplexml" rel="nofollow noreferrer">SimpleXML 或其他)
简短问题 -- 将 PHP 嵌入 XML 中,通过在 XML 解析期间定位处理指令节点来有条件地提取(并执行) PHP 是否是一种可行的解决方案,或者我是不是在向一棵充满问题的树吠叫?
扩展——也许问题与 XML 关系不大,而与选择适当的“元语言”有关,我可以根据它执行查询以满足条件,并执行包含 PHP 片段。由于其结构、可移植性和简单性(尽管冗长),XML 似乎是一个很好的候选者
我考虑过回到普通 PHP,使用数组作为数据结构,但任何结构化元语言都足以作为包装器。我们非常欢迎您针对此类任务提出您的首选语言建议。
不管怎样,我一直在开发一个接受带有嵌入式 PHP 的 XML 文件的引擎。我解析 XML 数据(在我的例子中使用 SAX 回调),并根据一些输入来协助“查询”XML 数据,使用 eval() 提取并运行适当的嵌入式 PHP
。
(我知道;“如果 eval()
是答案,那么你几乎肯定问错了问题。”,但我目前不关心这个 )
所以我最终得到这样的结果:
<root>
<node>
<parameters>
<!-- some stuff -->
</parameters>
<callback>
<?php
function(){
// do some stuff
};
?>
</callback>
</node>
<node>
<parameters>
<!-- some other stuff -->
</parameters>
<callback>
<?php
function(){
// do some other stuff
};
?>
</callback>
</node>
</root>
我可以通过使用 xml_set_processing_instruction_handler()
设置回调来解析 PHP,最终会这样做:(
xml_set_processing_instruction_handler($parser, function($parser, $target, $func)
{
// obtain some parameters into $data
call_user_func(eval("return {$func};"), $data);
});
代码只是一个示例,还有很多事情要做这里的当然)
是否尝试过这种方法,但由于一些不可预见的原因而导致失败 边缘情况?我不想在这样的解析器上投入大量时间,却发现它在某些情况下会严重失败。我很高兴从自己的错误中学习,但我更愿意从别人的错误中学习。
I read this question/answer however there wasn't any mention of it being a "bad idea", and unfortunately any search query with the words parse, xml, and php typically yield information about parsing XML with PHP (via SimpleXML or whatever)
Short question -- Is it a viable solution to embed PHP in XML, extracting (and executing) it conditionally, by targeting processing instruction nodes during XML parsing, or am I barking up a tree fraught with issues?
Expanding -- Perhaps the issue is less to do with XML, and more to do with selecting an appropriate "meta-language", against which I can perform queries to satisfy conditions, and execute the contained PHP snippet. XML seemed like a good candidate for it's structure, portability, and simplicity (despite verbosity)
I've considered just falling back to vanilla PHP, using arrays as a data structure, but any structured meta-language would suffice as a wrapper. Suggestions of your preferred language for such a task is more than welcome.
Anyway, I've been working on an engine that accepts XML files with embedded PHP. I parse the XML data (in my case using SAX callbacks) and depending on some input to assist with "querying" the XML data, the appropriate embedded PHP is pulled and ran with eval()
.
(I know; "If eval()
is the answer, you're almost certainly asking the wrong question.", but I'm not concerned with that at the moment)
So I end up with something like:
<root>
<node>
<parameters>
<!-- some stuff -->
</parameters>
<callback>
<?php
function(){
// do some stuff
};
?>
</callback>
</node>
<node>
<parameters>
<!-- some other stuff -->
</parameters>
<callback>
<?php
function(){
// do some other stuff
};
?>
</callback>
</node>
</root>
I can parse the PHP out by setting a callback with xml_set_processing_instruction_handler()
which ultimately does:
xml_set_processing_instruction_handler($parser, function($parser, $target, $func)
{
// obtain some parameters into $data
call_user_func(eval("return {$func};"), $data);
});
(the code is only an example, there's alot more going on here of course)
Has this approach been attempted resulting in failure due to some unforeseen
edge case? I don't want to invest a large about of time into such a parser only to find out that it'll fail spectacularly under some circumstances. I'm happy to learn from my own mistakes, but I'd much rather learn from someone else's.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是比 Phing 声明 AdhocTask 作为 CDATA 并执行其中的代码。请注意,在源代码中,它们只是简单地评估元素的内容。
我可以看到的一个警告在 xml_set_processing_instruction_handler( ) 文档:“无法引用 PI 结束标记 (?>)。”这意味着以下内容将出现问题:
这是很容易避免的:
除此之外,只要您采取 通常的 eval() 预防措施,你应该没问题!
This seems like a better approach than Phing's custom approach of declaring an AdhocTask as CDATA and executing the code within. Note that in the source code, they simply eval the contents of the element.
One caveat that I can see is mentioned on the xml_set_processing_instruction_handler() documentation: "the PI end tag (?>) can not be quoted." This means the following will present a problem:
This is quite easily avoided:
Other than that, as long as you take the usual eval() precautions, you should be fine!
要了解“解析 XSLT 中嵌入的 PHP”的用法,请参阅此registerPHPFunctions教程。
要了解“PHP 转换为 XML”的用法(与通常使用 PHP“转换为 HTML”的方法一样),请参阅http://code.google.com/p/smallest-php-xml-xsl-framework/
它是一个以XML+PHP(PHP生成XML)和XSLT作为可选模板系统的应用程序。在 MVC 架构中,XML+PHP 执行“MVC 模型处理”,XSLT 执行 MVC 视图(或者可选地直接使用 PHP 代码作为视图)。
For understand the use of "parsing PHP embedded in XSLT" see this tutorial of registerPHPFunctions.
For understand the use of "PHP into XML" (as usual use of PHP "into HTML") see http://code.google.com/p/smallest-php-xml-xsl-framework/
It is an application with XML+PHP (PHP generating XML) and XSLT as optional template system. In a MVC architecture the XML+PHP do the "MVC-Model processing" and XSLT the MVC-View (or optional direct use of a PHP code as View).