用PHP的SimpleXML访问处理指令 [英] Accessing processing instructions with PHP's SimpleXML

查看:0
本文介绍了用PHP的SimpleXML访问处理指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单--有没有办法使用SimpleXML访问处理指令节点的数据?我知道SimpleXML很简单;因此,它有许多限制,主要是处理混合内容节点。

示例:

Test.xml

<test>
    <node>
        <?php /* processing instructions */ ?>
    </node>
</test>

Parse.php

$test = simplexml_load_file('Test.xml');
var_dump($test->node->php); // dumps as a SimpleXMLElement, so it's sorta found,
                            // however string casting and explicitly calling
                            // __toString() yields an empty string
那么,这仅仅是SimpleXML的简单性造成的技术限制,还是有办法呢?如有必要,我将转换为SAX或DOM,但SimpleXML会更好。

推荐答案

问题在于?Php?>被认为是一个标签...因此,它被解析为一个大的标记元素。您需要做的是:

$xml = file_get_contents('myxmlfile.xml');
$xml = str_replace('<?php', '<![CDATA[ <?php', $xml);
$xml = str_replace('?>', '?> ]]>', $xml);
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);

我不完全确定这是否会奏效,但我认为它会奏效。测试一下...

这篇关于用PHP的SimpleXML访问处理指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆