Jump to content

Phantom line in DOMDocument


ToonMariner

Recommended Posts

Hi Guys,

 

should be a quick 'un...

 

Not found anything on this and looked a fair bit.

 

Creating an xml file with DOMDocument and saving to sting using DOMDocument::saveXML();

 

the result is passed back to an ajax call but fails as the xml has an empty line prior to the <?xml ...?> declaration.

 

Anyone encountered this and how to combat it? (must be a better way than just loading string into array and shifting first element.)

 

cheers peeps.

Link to comment
https://forums.phpfreaks.com/topic/179860-phantom-line-in-domdocument/
Share on other sites

Hi redarrow...

 

All pretty good - got forced into working for myself back in May - quite liking it ;)

 

OK code

 

class Array2XML
{
private $XMLArray;

private $doc;

public function __construct ()
{
	$this->doc = new DOMDocument();
	$this->doc->strictErrorChecking	=	true;
	$this->doc->formatOutPut	=	true;
	$this->doc->ignoreWhiteSpace	=	false;
}

private function createXML ( $arr , $node = NULL , $key = NULL)
{
	if	(
		is_null ( $node )
		)
	{
		$node	=	$this->doc->createElement('root');
		$this->doc->appendChild ( $node );
	}
	foreach ($arr as $k => $v)
	{
		if	(
			is_array ( $v )
			)
		{
			$item	=	$this->doc->createElement($k);
			foreach ($v as $k2 => $v2)
			{
				$this->createXML($v2 , $item , $k2);
			}
			$node->appendChild($item);
		}
		else
		{
			if	(
				is_string  ( $k )
				)
			{
				$item	=	$this->doc->createElement($k);
				$text	=	$this->doc->createTextNode ($v);
				$item->appendChild($text);
				$node->appendChild( $item );
			}
			else
			{
				$item	=	$this->doc->createElement($key);
				$text	=	$this->doc->createTextNode ($v);
				$item->appendChild($text);
				$node->appendChild( $item );
			}
		}
	}
}

public function retrunAsString ()
{
	$this->createXML($this->XMLArray);
	return $this->doc->saveXML ();
}

public function setArray($XMLArray){
	if	(
		is_array($XMLArray)
		&&
		count($XMLArray) != 0
		)
	{
		$this->XMLArray = $XMLArray;
	}
	return false;
}

}

?>

 

(its a work in progress!!!)

 

here is the array serialized:

 

a:2:{s:5:"field";s:9:"regregion";s:4:"data";a:2:{s:5:"value";a:4:{i:0;s:4:"5392";i:1;s:4:"5391";i:2;s:4:"5390";i:3;s:4:"5389";}s:7:"display";a:4:{i:0;s:7:"England";i:1;s:16:"Northern Ireland";i:2;s:8:"Scotland";i:3;s:5:"Wales";}}}

 

just call with

 

$obj = new ArrayToXML();

$obj->setArray($arr)' // that serialized effort...

echo $obj->returnAsString();

 

strangely trying to view in browser issues error stating there is whitespace before xml declaration!!!

 

Bit flumoxed on that one....

 

There are a few problems (different class names, mistyped method names, etc.) with the code that you posted. After fixing what appear to be typos, your code runs for me without producing whitespace before the XML declaration: see http://codepad.org/kHPecgHn

 

Does that code in the link above, still have the problem when you try it?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.