loadXML throws "Only one top level element is allowed .

Alles, was PHP betrifft, kann hier besprochen werden.

loadXML throws "Only one top level element is allowed .

Postby ying_zhao » 05. April 2006 16:58

Hi all,

I have just started experimenting with php & xml.

Followed the samples came with the DOM API section on this site. I was able to load the book.xml from the file system with the code like this.

Code: Select all
<?php
$doc = DOMDocument::load('book.xml');
echo $doc->saveXML();

$doc = new DOMDocument();
$doc->load('book.xml');
echo $doc->saveXML();
?>

However, when I want to load from a string, like below.

Code: Select all
<?php
$doc = DOMDocument::loadXML('<root><node/></root>');
echo $doc->saveXML();

$doc = new DOMDocument();
$doc->loadXML('<root><node/></root>');
echo $doc->saveXML();
?>

I got this error.

Only one top level element is allowed in an XML document. Error processing resource 'http://localhost/contest/loadXML.php'. Line 4, Position 2

<root><node/></root>
-^

I am using xampp for Windows 1.5.1. Anyone knows why? Please help. Many thanks.
ying_zhao
 
Posts: 1
Joined: 05. April 2006 16:48

Postby KingCrunch » 06. April 2006 08:00

The examples are independent. Youre tryin to return two documents, but two documents causes two Root-Elements, which is not allowed in xml.
Nicht jeder Fehler ist ein Bug ...
KingCrunch
 
Posts: 1724
Joined: 26. November 2005 19:25


Return to PHP

Who is online

Users browsing this forum: No registered users and 16 guests