Simplexml + Namespace +attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • root

    #1

    Simplexml + Namespace +attributes

    Hi folks - hope someone can help me.

    Firstly, my apologies for crossposting this to alt.php (and
    not properly crossposting either).

    I have an XML file with some elements like those below

    <core:Distric t Id="AB01">
    <core:Name>NORT H EAST</core:Name>
    <core:Associati on Type="type1">VA LUE 1a</core:Associatio n>
    <core:Associati on Type="type2">VA LUE 1b</core:Associatio n>
    <core:Associati on Type="type3">VA LUE 1c</core:Associatio n>
    </core:District>
    <core:Distric t Id="AB02">
    <core:Name>NORT H WEST</core:Name>
    <core:Associati on Type="type1">VA LUE 2a</core:Associatio n>
    <core:Associati on Type="type2">VA LUE 2b</core:Associatio n>
    <core:Associati on Type="type3">VA LUE 2c</core:Associatio n>
    </core:District>

    I'm trying to parse it using simplexml

    I have:

    $districts = $xmlfile->children($core );

    foreach ($districts as $key => $value)
    {
    echo "Name = " . $value->Name . "\n";
    echo "Region1 = " . $value->Association[0] . "\n";
    echo "Region2 = " . $value->Association[1] . "\n";
    echo "Region3 = " . $value->Association[2] . "\n";

    }

    This works fine, but I am unable to access the Id attribute in the
    opening District element, i.e. AB01 or AB02.

    Would anyone be able to suggest how I might get that value with
    simplexml?

    Many thanks,

    Russell
  • Darkstar 3D

    #2
    Re: Simplexml + Namespace +attributes

    District ID is an attribute of an element, not a child. Therefore to
    read it you must use syntax simular to

    $xml->District['ID']

    Comment

    Working...