SimpleXML Bug?

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

    SimpleXML Bug?

    So I'm trying to use SimpleXML to get some attribute information about
    some nodes in my XML document, but it seems like SimpleXML ignores
    attributes for elements with no children,

    For instance:

    <xmlfile>
    <object foo="bar">
    <color mode="rgb">ffdd ee</color>
    </object>
    </xmlfile>

    I can get the attribute foo from the object element, but there does
    not seem to be any way to retrieve the mode attrbitue from the color
    element. print_r() of this data shows that mode="rgb" is totally
    lost.

    If this is a bug, I'll go report this to php.net, but if it's not,
    could someone explain either the reasoning behind it or how to get
    this information?
  • Michael Fesser

    #2
    Re: SimpleXML Bug?

    ..oO(dimo414)
    >So I'm trying to use SimpleXML to get some attribute information about
    >some nodes in my XML document, but it seems like SimpleXML ignores
    >attributes for elements with no children,
    >
    >For instance:
    >
    ><xmlfile>
    <object foo="bar">
    <color mode="rgb">ffdd ee</color>
    </object>
    ></xmlfile>
    >
    >I can get the attribute foo from the object element, but there does
    >not seem to be any way to retrieve the mode attrbitue from the color
    >element. print_r() of this data shows that mode="rgb" is totally
    >lost.
    Pleast post your PHP code.

    Micha

    Comment

    • dimo414

      #3
      Re: SimpleXML Bug?

      On May 23, 2:07 am, Michael Fesser <neti...@gmx.de wrote:
      .oO(dimo414)
      >
      >
      >
      So I'm trying to use SimpleXML to get some attribute information about
      some nodes in my XML document, but it seems like SimpleXML ignores
      attributes for elements with no children,
      >
      For instance:
      >
      <xmlfile>
      <object foo="bar">
      <color mode="rgb">ffdd ee</color>
      </object>
      </xmlfile>
      >
      I can get the attribute foo from the object element, but there does
      not seem to be any way to retrieve the mode attrbitue from the color
      element. print_r() of this data shows that mode="rgb" is totally
      lost.
      >
      Pleast post your PHP code.
      >
      Micha
      Well, if I print_r the simplexml object generated by my example xml, I
      get the following:
      SimpleXMLElemen t Object
      (
      [object] =SimpleXMLEleme nt Object
      (
      [@attributes] =Array
      (
      [foo] =bar
      )
      [color] =ffddee
      )
      )
      Note that the mode attribute is completely lost.

      If you really need to see the php code that generated this, here it
      is:
      <?php

      $xml = '<xmlfile>
      <object foo="bar">
      <color mode="rgb">ffdd ee</color>
      </object>
      </xmlfile>';

      $simple = simplexml_load_ string($xml);

      print_r($simple );

      ?>

      Comment

      • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

        #4
        Re: SimpleXML Bug?

        dimo414 escribió:
        <?php
        >
        $xml = '<xmlfile>
        <object foo="bar">
        <color mode="rgb">ffdd ee</color>
        </object>
        </xmlfile>';
        >
        $simple = simplexml_load_ string($xml);
        >
        print_r($simple );
        >
        ?>
        The issue is probably related to print_r():

        <?php

        $xml = '<xmlfile>
        <object foo="bar">
        <color mode="rgb">ffdd ee</color>
        </object>
        </xmlfile>';

        $simple = simplexml_load_ string($xml);

        // Applied here, it does print the right value:
        print_r($simple->object[0]->color[0]['mode']);

        ?>



        --
        -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
        -- Mi sitio sobre programación web: http://bits.demogracia.com
        -- Mi web de humor al baño María: http://www.demogracia.com
        --

        Comment

        • Michael Fesser

          #5
          Re: SimpleXML Bug?

          ..oO(dimo414)
          >Well, if I print_r the simplexml object generated by my example xml, I
          >get the following:
          >SimpleXMLEleme nt Object
          >(
          [object] =SimpleXMLEleme nt Object
          (
          [@attributes] =Array
          (
          [foo] =bar
          )
          [color] =ffddee
          )
          >)
          >Note that the mode attribute is completely lost.
          >
          >If you really need to see the php code that generated this, here it
          >is:
          ><?php
          >
          >$xml = '<xmlfile>
          <object foo="bar">
          <color mode="rgb">ffdd ee</color>
          </object>
          ></xmlfile>';
          >
          >$simple = simplexml_load_ string($xml);
          >
          >print_r($simpl e);
          >
          >?>
          Ah, I missed the print_r() from your first posting. Well, just printing
          out a SimpleXML object usually doesn't work as expected. It's simply
          because of how these objects are handled and created internally. You
          would run into the same problems if you want to print out an instance of
          a class which makes heavy use of the __set() and __get() interceptor
          methods to access their properties - a simple print_r() won't work.

          Anyway, you can still use XPath or the SimpleXML syntax as described in
          the manual to access all the nodes and attributes:

          print_r($simple );
          print_r($simple->object);
          print_r($simple->object->color['mode']);

          Sometimes it might also be necessary to explicitly cast a node to string
          if you want to work with it.

          Micha

          Comment

          • Chuck Anderson

            #6
            Re: SimpleXML Bug?

            Michael Fesser wrote:
            .oO(dimo414)
            >
            >
            >Well, if I print_r the simplexml object generated by my example xml, I
            >get the following:
            >SimpleXMLEleme nt Object
            >(
            > [object] =SimpleXMLEleme nt Object
            > (
            > [@attributes] =Array
            > (
            > [foo] =bar
            > )
            > [color] =ffddee
            > )
            >)
            >>
            >>
            Maybe I shouldn't use this thread to ask this question , but ......

            I'm confused by the '@' notation on @attributes in a SimpleXMLElemen t
            Object.

            I can find no documentation explaining what it means or how it got there.

            Can anyone explain? And how would I create a member like this in an
            object I create?

            --
            *************** **************
            Chuck Anderson • Boulder, CO

            Nothing he's got he really needs
            Twenty first century schizoid man.
            *************** *************** *****

            Comment

            • Michael Fesser

              #7
              Re: SimpleXML Bug?

              ..oO(Chuck Anderson)
              >Maybe I shouldn't use this thread to ask this question , but ......
              >
              >I'm confused by the '@' notation on @attributes in a SimpleXMLElemen t
              >Object.
              >
              >I can find no documentation explaining what it means or how it got there.
              I may be wrong, but I don't think it has any special meaning. Remember
              that SimpleXML is written in C and not limited to what the PHP parser
              accepts. Since '@attributes' only seem to appear once in the SimpleXML
              source code, I think it's just a naming convention, perhaps following
              the XPath syntax for accessing attributes.
              >Can anyone explain? And how would I create a member like this in an
              >object I create?
              Not directly, because the parser wouldn't allow it. But there are other
              ways to define and access class members of almost arbitrary names if you
              have to, e.g.

              <?php
              $foo = new StdClass();
              $bar = '@attributes';
              $foo->$bar = 'something';
              var_dump($foo);
              ?>

              Micha

              Comment

              • Chuck Anderson

                #8
                Re: SimpleXML Bug?

                Michael Fesser wrote:
                .oO(Chuck Anderson)
                >
                >
                >Maybe I shouldn't use this thread to ask this question , but ......
                >>
                >I'm confused by the '@' notation on @attributes in a SimpleXMLElemen t
                >Object.
                >>
                >I can find no documentation explaining what it means or how it got there.
                >>
                >
                I may be wrong, but I don't think it has any special meaning. Remember
                that SimpleXML is written in C and not limited to what the PHP parser
                accepts. Since '@attributes' only seem to appear once in the SimpleXML
                source code, I think it's just a naming convention, perhaps following
                the XPath syntax for accessing attributes.
                >
                >
                >Can anyone explain? And how would I create a member like this in an
                >object I create?
                >>
                >
                Not directly, because the parser wouldn't allow it. But there are other
                ways to define and access class members of almost arbitrary names if you
                have to, e.g.
                >
                <?php
                $foo = new StdClass();
                $bar = '@attributes';
                $foo->$bar = 'something';
                var_dump($foo);
                ?>
                >
                Micha
                >
                Okay, thanks for the explanation. What confused me is when using
                SimpleXML, I can not access "@attribute s" like the other object members.

                When I do a print_r of a SimpleXMLElemen t Object, attributes are listed
                (along with children) this way:

                [node1] SimpleXMLElemen t Object
                (
                [@attributes] =array
                (
                ['attribname'] =attribvalue
                )
                [child1] =child1value
                [child2] =child2value
                }

                I only can access the value of the attribute "attribname " by either:
                $value = node1['attribname']
                .... which bypasses the array it appears to be in (@attributes). ??

                or

                $attribs = node1->attributes() ;
                $value = $attribs['attribname']
                .... using the object method, attributes().

                --
                *************** **************
                Chuck Anderson • Boulder, CO

                Nothing he's got he really needs
                Twenty first century schizoid man.
                *************** *************** *****

                Comment

                Working...