0

I'm using the simplexml_load_string function to process an xml string.

The following is the xml string.

   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#"    
    xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
    <artist-list offset="0" count="1422">
       <artist ext:score="100" type="Person" id="72c536dc-7137-4477-a521-567eeb840fa8">
       <name>Bob Dylan</name>
       <sort-name>Dylan, Bob</sort-name>
       <gender>male</gender><country>US</country>
       <life-span><begin>1941-05-24</begin></life-span>
  </artist>
  </artist-list>
  </metadata>

When the function returns I get the following array. I want to read the artist ext:score="value" but it doesn't get returned, how would I get this attribute of the tag?

 SimpleXMLElement Object
 (
   [artist-list] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [offset] => 0
                [count] => 1422
            )

        [artist] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [type] => Person
                                [id] => 72c536dc-7137-4477-a521-567eeb840fa8
                            )

                        [name] => Bob Dylan
                        [sort-name] => Dylan, Bob
                        [gender] => male
                        [country] => US
                        [life-span] => SimpleXMLElement Object
                            (
                                [begin] => 1941-05-24
                            )
                     }
              }      
     }
 }      

1 Answer 1

1

It's a namespace thing. Register the two namespaces, and use them when you run an XPath query, or roll-through the attributes.

Here's some code, tested with your XML hope it's helpful

<?php

    try {
        $xml = simplexml_load_file( "meta.xml" );

        $xml->registerXPathNamespace('m', 'http://musicbrainz.org/ns/mmd-2.0#' );
        $xml->registerXPathNamespace('ext', 'http://musicbrainz.org/ns/ext#-2.0' );

        // Find the customer
        $result = $xml->xpath('//m:artist');

        while(list( , $node) = each($result)) {
            echo  $node."\r\n";

            echo  "Default Name Space Attributes: \r\n";
            foreach($node->attributes() as $a => $b) { 
                echo "\t".$a.":'".$b."'";
            }

            echo  "Name Space Attributes: \r\n";
            foreach($node->attributes( "ext", 1 ) as $a => $b) { 
                echo "\t".$a.":'".$b."'";
            }
        }
    } catch( Exception $e ) {
        echo "Exception on line ".$e->getLine()." of file ".$e->getFile()." : ".$e->getMessage()."<br/>";
    }


?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude, you hit the nail on the head.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.