您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

正则表达式可在任何xml标记中添加属性

正则表达式可在任何xml标记中添加属性

不要在XML上使用正则表达式。Xml不是常规语言。请改用phpxml扩展名

$xml = new SimpleXml(file_get_contents($xmlFile));
function process_recursive($xmlNode) {
    $xmlNode->addAttribute('attr', 'myAttr');
    foreach ($xmlNode->children() as $childNode) {
        process_recursive($childNode);
    }
}
process_recursive($xml);
echo $xml->asXML();

所有包含正则表达式的答案都将破坏此有效xml,例如:

<?xml version="1.0" encoding='UTF-8'?>
<html>
    <head>
        <!-- <Meta> ... </Meta> -->
        <script>//<![CDATA[
            function load() {document.write('<tt>Test</tt>');}
        //]]></script>
        <title><![CDATA[Fancy <<SiteName>> [with Breadcrumbs] > in > title]]></title>
    </head>
    <body onload="load()">
        <input
            type="submit"
            value="multiline
                   button
                   text"
        />
    </body>
</html>
其他 2022/1/1 18:19:37 有404人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶