PHP SimpleXML doesn't preserve line breaks in XML attributes

前端 未结 6 1356
自闭症患者
自闭症患者 2020-12-11 04:31

I have to parse externally provided XML that has attributes with line breaks in them. Using SimpleXML, the line breaks seem to be lost. According to another stackoverflow

6条回答
  •  -上瘾入骨i
    2020-12-11 04:59

    This is what worked for me:

    First, get the xml as a string:

        $xml = file_get_contents($urlXml);
    

    Then do the replacement:

        $xml = str_replace(".\xe2\x80\xa9",".\n\n",$xml);
    

    The "." and "< as:eol/ >" were there because I needed to add breaks in that case. The new lines "\n" can be replaced with whatever you like.

    After replacing, just load the xml-string as a SimpleXMLElement object:

        $xmlo = new SimpleXMLElement( $xml );
    

    Et Voilà

提交回复
热议问题