Jump to content

Recommended Posts

I have a Simple php newbie question about targeting arrays with simpleXML. I have an xml file with nodes that have a whole bunch of attributes attached to them. I know how to target and echo a node but I can't seem to target and echo the the attributes on those nodes. For example, I would like to echo the attribute 'name' on the list node.

 

 

My xml file looks like this:

 

<?xml version="1.0" encoding="utf-16"?>
<!-- http://www.designvillain.com/vp/list1.xml -->
<item>
    <list name="My name" videotitle="My title">
        <thumb>1.jpg</thumb>      
    </list> 
</item>

 

My php looks like this:

<?php
    if( ! $xml = simplexml_load_file('file.xml') )
    {
        echo 'unable to load XML file';
    }
    
    else
    
    {
        foreach( $xml->list[0]->list as $list )
    
        {
            echo $list->attributes('name').'<br/>';
        }
    } 
?>

 

Any suggestions?

 

Link to comment
https://forums.phpfreaks.com/topic/165444-solved-targeting-arrays-with-simplexml/
Share on other sites

Hmm, still not working. I looked this up in the php reference and it is definitely supposed to work:

 


<?php

    if( ! $xml = simplexml_load_file('file.xml') )
    {
        echo 'unable to load XML file';
    }
    
    else
    
    {
    
foreach ($xml->list as $list) {

	echo $list['name'];

}

?>

 

Could I still be missing something here?

It works for me.

 

<?php
$xml = <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<!-- http://www.designvillain.com/vp/list1.xml -->
<item>
    <list name="My name" videotitle="My title">
        <thumb>1.jpg</thumb>     
    </list>
</item>
EOF;

$xml = simplexml_load_string($xml);

foreach ($xml->list as $list) {
echo $list['name'];
}

 

Outputs:

My name

 

Is that not the output you're looking for?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.