0

I have a web app (in PHP) which selects the data of an XML element depending on the attribute's value.

However, I'm having trouble trying to actually do this. Here is the XML which I'm using:

    <meta> <!-- Data about data -->

        <genre genre_name="edm">
            <amount_of_bands>3</amount_of_bands>
        /genre>

        <genre genre_name="pop">
            <amount_of_bands>3</amount_of_bands>
        </genre>

        <genre genre_name="hip-hop">
            <amount_of_bands>1</amount_of_bands>
        </genre>

    </meta>

I'm trying to access the numbers in the tags. I've tried using $xml_root->meta->genre['genre_name = edm']->amount_of_bands; (with SimpleXML)), and that yields nothing. I've tried researching, but I still get nothing.

EDIT, YO - I fixed my problem by replacing <genre genre_name="hip-hop"> with an extra <genre_name>hip-hop</genre_name>element inside the <genre>...</genre> element. Much easier to read, maintain and code with.

Thanks for the answers, regardless.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Joshua Merriman
  • 925
  • 1
  • 8
  • 13

1 Answers1

0

Check the Example #1 in: http://php.net/manual/en/simplexmlelement.attributes.php

Instead of echoing the data, push the attribute list into an associative array, which you will be able to reference later on.

Similar question: How to get the value of an attribute from XML file in PHP?

Community
  • 1
  • 1
Maciej Gurban
  • 5,615
  • 4
  • 40
  • 55