Page 1 of 1

PHP XML error

Posted: Mon Apr 21, 2008 9:09 am
by clarky08
Hi I am trying to access an XML file using PHP and i keep getting this error

Warning: DOMDocument::load() [function.DOMDocument-load]: attributes construct error in file:///C%3A/wamp/www/Website/xml/library.xml, line: 1 in C:\wamp\www\Website\home.php on line 5

Warning: DOMDocument::load() [function.DOMDocument-load]: Couldn't find end of Start Tag xml line 1 in file:///C%3A/wamp/www/Website/xml/library.xml, line: 1 in C:\wamp\www\Website\home.php on line 5

Warning: DOMDocument::load() [function.DOMDocument-load]: Extra content at the end of the document in file:///C%3A/wamp/www/Website/xml/library.xml, line: 1 in C:\wamp\www\Website\home.php on line 5

Fatal error: Call to a member function getElementsByTagName() on a non-object in C:\wamp\www\Website\home.php on line 11

I have no idea what is wrong with my code and I have checked it and can't seem to find anything wrong with it. Can anybody help me??

This is my PHP code for it:

Code: Select all

 
<?php 
    /* here we must specify the version of XML : i.e: 1.0 */ 
    $xml = new DomDocument('1.0'); 
    $xml->load('xml/library.xml'); 
 
    /* first to create a list of categories */ 
    $categories = array(); 
    $XMLCategories = $xml->getElementsByTagName('categories')->item(0); 
 
    foreach($XMLCategories->getElementsByTagName('category') as $categoryNode) { 
        /* notice how we get attributes */ 
        $cid = $categoryNode->getAttribute('cid'); 
        $categories[$cid] = $categoryNode->firstChild->nodeValue; 
    } 
?>
 
and this is my XML code:

Code: Select all

 
<xml version="1.0"?>  
<library>    
<categories>      
<category cid="1">Web Development</category>      
<category cid="2">Database Programming</category>      
<category cid="3">PHP</category>      
<category cid="4">Java</category>    
</categories>    
<books>     
<book>       
 <title>Apache 2</title>        
 <author>Peter Wainwright</author>        
 <publisher>Wrox</publisher>       
 <category>1</category>      
 </book>      
 <book>        
 <title>Advanced PHP Programming</title>        
 <author>George Schlossnagle</author>        
 <publisher>Developer Library</publisher>        
 <category>1</category>        
 <category>3</category>      
 </book>      
 <book>        
 <title>Visual FoxPro 6 - Programmers Guide</title>        
 <author>Eric Stroo</author>        
 <publisher>Microsoft Press</publisher>        
 <category>2</category>      
 </book>       
 <book>        
 <title>Mastering Java 2</title>        
 <author>John Zukowski</author>        
 <publisher>Sybex</publisher>        
 <category>4</category>      
 </book>    
 </books></library>
 
Any help would be much appreciated.

Thanks a million

Colm

Re: PHP XML error

Posted: Tue Apr 22, 2008 12:22 am
by Zoxive
You have an invalid xml document.

If you read your errors again, You should see your problem..
Couldn't find end of Start Tag xml line 1