simplexml and attributes with a namespace in front. How do you getthe attribute?

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

    simplexml and attributes with a namespace in front. How do you getthe attribute?

    Here's a bit of xml code that works.

    <?php

    $string =
    "<?xml version=\"1.0\" standalone=\"ye s\"?>
    <world>
    <people xmlns:ss=\"http ://crap\">
    <person id=\"5\">John Doe</person>
    <person id=\"2\">Susie Q. Public</person>
    </people>
    </world>";

    $xml = simplexml_load_ string($string) ;
    print "<pre>\n";
    print_r($xml);
    print "</pre>\n";

    foreach($xml->people->person as $p)
    foreach($p->attributes() as $a =$b)
    {
    print "$a =$b<br />\n";
    }

    // prints out
    // id =5
    // id =2
    ?>

    Now what if I change the first person to this
    <person ss:id=\"5\">Joh n Doe</person>

    Anyone know how to get the id?

    This is a question that is pertinent to an excel .xml file as
    you'll get lines like this
    <Cell ss:Index="3">st uff</Cell>

    btw: there is a guide on ibm that tells you how to do this with php
    and dom. I'm just wondering if there's a way to do it with simplexml.

    thx.
  • fido

    #2
    Re: simplexml and attributes with a namespace in front. How do youget the attribute?

    figured it out
    When the attribute has a namespace eg.
    <person ss:id=\"5\">Joh n Doe</person>

    you provide the namespace to the attributes as an argument
    eg.

    foreach($xml->people->person as $p)
    foreach($p->attributes("ht tp://crap") as $a =$b)
    {
    print "$a =$b<br />\n";
    }

    // id =5 is now printed


    fido wrote:
    Here's a bit of xml code that works.
    >
    <?php
    >
    $string =
    "<?xml version=\"1.0\" standalone=\"ye s\"?>
    <world>
    <people xmlns:ss=\"http ://crap\">
    <person id=\"5\">John Doe</person>
    <person id=\"2\">Susie Q. Public</person>
    </people>
    </world>";
    >
    $xml = simplexml_load_ string($string) ;
    print "<pre>\n";
    print_r($xml);
    print "</pre>\n";
    >
    foreach($xml->people->person as $p)
    foreach($p->attributes() as $a =$b)
    {
    print "$a =$b<br />\n";
    }
    >
    // prints out
    // id =5
    // id =2
    ?>
    >
    Now what if I change the first person to this
    <person ss:id=\"5\">Joh n Doe</person>
    >
    Anyone know how to get the id?
    >
    This is a question that is pertinent to an excel .xml file as
    you'll get lines like this
    <Cell ss:Index="3">st uff</Cell>
    >
    btw: there is a guide on ibm that tells you how to do this with php and
    dom. I'm just wondering if there's a way to do it with simplexml.
    >
    thx.

    Comment

    Working...