Jump to content

XML PHP error


clarky08

Recommended Posts

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:

<?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:

<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

Link to comment
https://forums.phpfreaks.com/topic/102129-xml-php-error/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.