XML / PHP:使用PHP创建XML文本

I'm a beginner with XML on PHP. My page returns me the following PHP error: error on line 2 at column 1: Extra content at the end of the document

But, at line 2, there only is a "require_once", followed by 3 others "require_once", the path to the required file is ok, and it ends with a ';'. The line above is the "

I have this sample of code, which I think is the source of this error (in this order)

$xml = simplexml_load_string("<result/>");
$entitlements = $xml->addChild("entitlements");
$entitlements->addChild("productId", $productId);

then, below :

$fulfillmentXML = new SimpleXMLElement($result);
//some stuff with $fullfillmentXML 
echo $xml->asXML();

I do not understand exactly how the XML works in PHP, but I thought there was a problem creating a SimpleXMLElement() after the simplexml_load_string() call.

Your error doesn't look like a PHP error, but rather a browser error generated when it tried to parse the supplied XML contents. Therefore line 2 of the error does not refer to line 2 of your PHP file, but to line 2 of your generated output.

Check your output (source of your generated page), and see what line 2 is there. Make sure you only have 1 root element, and that nothing comes after it.