SimpleXmlElement-> addAttribute () does not allow blank lines

Description:

A call of type addAttribute ("attrname", ") leads to" PHP warning: SimpleXMLElement :: addAttribute (): The name and value of the attribute are required. "In addition to the warning, the attribute is discarded.

Play 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 cannot change my php version (for some reason). Is there any way to solve this?

+5
source share
3 answers

you can try changing the line below

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

and also you can try below.

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

    $doc = $xml->createElement('document');
    $doc = $xml->appendChild($doc);
+1
source

SimpleXMLElement::addAttribute , value , . php 5.2.1, 5.2.6 5.3 , , , value ( ).

+1

Does this work for you? For testing, I do not have 5.2.1. It only works on 5.3.6.

$xml = simplexml_load_string('<img src="images/bg.png" alt="" />');
echo $xml->asXML();
0
source

All Articles