Remove Xml Version Tag When a Xml Is Created in PHP

remove xml version tag when a xml is created in php

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);

How to remove XML version from php://input

You could just remove it with str_replace():

$xmlString = file_get_contents('php://input');
$xmlString = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $xmlString);
file_put_contents("broadbeantesttest.xml", $xmlString, FILE_APPEND);

How to create XML file without XML opening tags

You could replace data in the string before to save it:

$str = $xmldata->asXML(); // get as string instead of saving file
$str = str_replace(['<?xml version="1.0"?>','<items>','</items>'],'',$str); // remove tags you don't want.
file_put_contents('xml/playlistbdays.xml', trim($str)) ; // save file

How to remove XML tag based on child attribute using php?

You need to use DOMDocument class to parse string to XML document. Then use DOMXpath class to find target element in document and use DOMNode::removeChild() to remove selected element from document.

$doc = new DOMDocument(); 
$doc->loadXML($xml);
$xpath = new DOMXpath($doc);
// select target entry tag
$entry = $xpath->query("//entry[title[@lang='fr']]")->item(0);
// remove selected element
$entry->parentNode->removeChild($entry);
$xml = $doc->savexml();

You can check result in demo



Related Topics



Leave a reply



Submit