remove xml version tag when a xml is created in php

前端 未结 10 1829
星月不相逢
星月不相逢 2020-11-28 10:28

I\'m creating a xml using this

$customXML = new SimpleXMLElement(\'\');

after adding some attributes onto this, when

相关标签:
10条回答
  • 2020-11-28 10:56

    In theory you can provide the LIBXML_NOXMLDECL option to drop the XML declaration when saving a document, but this is only available in Libxml >= 2.6.21 (and buggy). An alternative would be to use

    $customXML = new SimpleXMLElement('<abc></abc>');
    $dom = dom_import_simplexml($customXML);
    echo $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
    
    0 讨论(0)
  • 2020-11-28 11:01

    A practical solution: you know that the first occurrence of ?> in the result string is going to be then end of the xml version substring. So:

    $customXML = new SimpleXMLElement('<abc></abc>');
    $customXML = substr($customXML, strpos($customXML, '?'.'>') + 2);
    

    Note that ?> is split into two parts because otherwise some poor syntax highlighter may have problems parsing at this point.

    0 讨论(0)
  • 2020-11-28 11:02
    echo preg_replace("/<\\?xml.*\\?>/",'',$doc->saveXML(),1);
    
    0 讨论(0)
  • 2020-11-28 11:09
    $customXML = new SimpleXMLElement('<source><abc>hello</abc></source>');
    $result = $customXML->xpath('//abc');
    $result = $result[0];
    var_dump($result->asXML());
    
    0 讨论(0)
  • 2020-11-28 11:15

    If this is a problem, this should do it:

    $xml = str_replace(' version="1.0"', '', $xml);`
    
    0 讨论(0)
  • 2020-11-28 11:16

    As SimpleXMLElement always uses "\n" to separate the XML-Declaration from the rest of the document, it can be split at that position and the remainder taken:

    explode("\n", $customXML->asXML(), 2)[1];
    

    Example:

    <?php
    
    $customXML = new SimpleXMLElement('<!-- some comment -->
    <abc>
    </abc>');
    
    echo explode("\n", $customXML->asXML(), 2)[1];
    

    Output:

    <!-- some comment -->
    <abc>
    </abc>
    
    0 讨论(0)
提交回复
热议问题