Page 1 of 1

PHP and XML

Posted: Wed Jan 14, 2009 12:22 pm
by bdee1
i am relatively new to PHP and i am trying to figure out how to parse and modify an xml file using php.

i am starting by just trying to read the contents of a sample xml file and i am not having much luck.

my php code looks like this:

Code: Select all

 
<?php
#
#CREATING THE HANDLER
#
$xml = new SimpleXMlElement('file.xml', 0, true);
 
#
#GETTING AN ATTRIBUTE
#
foreach($xml->item as $item){
    $item_URL = $item->attributes()->url;
}
?>
 
<strong><?=$item_URL;?></strong><br>
 
and my xml file looks like this:

Code: Select all

 
<?xml version="1.0" encoding="utf-8"?>
<link>
    <title>Link 1</title>
    <description>This is the first link</description>
    <url>www.blairdee.info</url>
</link>
<link>
    <title>Link 2</title>
    <description>This is the SECOND link</description>
    <url>www.google.com</url>
</link>
<link>
    <title>Link 3</title>
    <description>This is the third link</description>
    <url>www.microsoft.com/windows7</url>
</link>
 
and when i run the php page it gives me the following errors:
HTTP/1.1 500 Internal Server Error Connection: close Date: Wed, 14 Jan 2009 18:13:41 GMT Content-Type: text/html Server: Microsoft-IIS/6.0 X-Powered-By: PHP/5.2.8 X-Powered-By: ASP.NET PHP Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: file.xml:7: parser error : Extra content at the end of the document in C:\domains\blairdee.info\wwwroot\PHP\index.php on line 5 PHP Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <link> in C:\domains\blairdee.info\wwwroot\PHP\index.php on line 5 PHP Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in C:\domains\blairdee.info\wwwroot\PHP\index.php on line 5 PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\domains\blairdee.info\wwwroot\PHP\index.php:5 Stack trace: #0 C:\domains\blairdee.info\wwwroot\PHP\index.php(5): SimpleXMLElement->__construct('file.xml', 0, true) #1 {main} thrown in C:\domains\blairdee.info\wwwroot\PHP\index.php on line 5

what am i doing wrong here?

Re: PHP and XML

Posted: Wed Jan 14, 2009 2:21 pm
by kendall
seems to me there is either an illegal character in the xml...xml has some foriegn characters that dont really play nice and u usually have to use the HTML equivalent... take a look at the file "&" are usually a problem