Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleXmlElement->addAttribute() doesn't allow empty strings

Tags:

php

xml

simplexml

Description:

A call like addAttribute("attrname", "") results in "PHP Warning: SimpleXMLElement::addAttribute(): Attribute name and value are required". In addition to the warning, the attribute is discarded.

Reproduce code:

<?php
$xml = new SimpleXmlElement("<img></img>");
$xml->addAttribute("src", "foo");
$xml->addAttribute("alt", "");
echo $xml->asXML()."\n";
?>

Expected result:

<?xml version="1.0"?>
<img src="foo" alt=""/>

Actual result:

PHP Warning:  SimpleXMLElement::addAttribute(): Attribute name and value are required in [...]/test.php on line 4
<?xml version="1.0"?>
<img src="foo"/>

this problem exists in PHP 5.2.1, but in PHP5.3.5 it works as I expect, but I can't change my php version(for some reason). Is there any way to solve this?

like image 360
missingcat92 Avatar asked Nov 04 '22 08:11

missingcat92


1 Answers

can you try by changing below line

$xml = new SimpleXmlElement("<img>");

and also you can try with below also

    $xml = new DOMDocument('1.0', 'iso-8859-1');

    $doc = $xml->createElement('document');
    $doc = $xml->appendChild($doc);
like image 108
Robin Michael Poothurai Avatar answered Nov 09 '22 15:11

Robin Michael Poothurai