ToonMariner Posted November 1, 2009 Share Posted November 1, 2009 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 More sharing options...
salathe Posted November 1, 2009 Share Posted November 1, 2009 Can you give us an example code snippet which produces this phantom line? Link to comment https://forums.phpfreaks.com/topic/179860-phantom-line-in-domdocument/#findComment-948866 Share on other sites More sharing options...
redarrow Posted November 1, 2009 Share Posted November 1, 2009 can we see a example please.... off topic how are you mate , well hopes all ok mate. Link to comment https://forums.phpfreaks.com/topic/179860-phantom-line-in-domdocument/#findComment-948869 Share on other sites More sharing options...
ToonMariner Posted November 1, 2009 Author Share Posted November 1, 2009 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.... Link to comment https://forums.phpfreaks.com/topic/179860-phantom-line-in-domdocument/#findComment-948958 Share on other sites More sharing options...
salathe Posted November 2, 2009 Share Posted November 2, 2009 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? Link to comment https://forums.phpfreaks.com/topic/179860-phantom-line-in-domdocument/#findComment-948964 Share on other sites More sharing options...
ToonMariner Posted November 2, 2009 Author Share Posted November 2, 2009 yeah couple of typos on copying (removed bits and bobs etc etc) found the issue... guess who's php file containing the class had an empty line at the beginning? - that's right MINE... fookin dime bar... Link to comment https://forums.phpfreaks.com/topic/179860-phantom-line-in-domdocument/#findComment-949042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.