Remove namespace from XML using PHP

前端 未结 4 1423
你的背包
你的背包 2020-12-05 19:15

I have an XML document that looks like this:



        
4条回答
  •  春和景丽
    2020-12-05 19:29

    To remove the namespace completely, you'll need to use Regular Expressions (RegEx). For example:

    $feed = file_get_contents("http://www.sitepoint.com/recent.rdf");
    $feed = preg_replace("/<.*(xmlns *= *[\"'].[^\"']*[\"']).[^>]*>/i", "", $feed); // This removes ALL default namespaces.
    $xml_feed = simplexml_load_string($feed);
    

    Then you've stripped any xml namespaces before you load the XML (be careful with the regex through, because if you have any fields with something like:

    cool. ]]>
    

    Then it will strip the xmlns from inside the CDATA which may lead to unexpected results.

提交回复
热议问题