Getting SimpleXMLElement to include the encoding in output

前端 未结 5 991
梦如初夏
梦如初夏 2020-12-09 10:26

This:

$XML = new SimpleXMLElement(\"\");
echo($XML->asXML());

...outputs this:



        
5条回答
  •  难免孤独
    2020-12-09 11:04

    You can try this, but you must use simplexml_load_string for $xml

    $xml // Your main SimpleXMLElement
    $xml->addAttribute('encoding', 'UTF-8');
    

    Or you can still use other means to add the encoding to your output.

    Simple Replacement

    $outputXML=str_replace('', '', $outputXML);
    

    Regular Expressions

    $outputXML=preg_replace('/<\?\s*xml([^\s]*)\?>/' '', $outputXML);
    

    DOMDocument - I know you said you don't want to use DOMDocument, but here is an example

    $xml=dom_import_simplexml($simpleXML);
    $xml->xmlEndoding='UTF-8';
    $outputXML=$xml->saveXML();
    

    You can wrap this code into a function that receives a parameter $encoding and adds it to the

提交回复
热议问题