bizzare array structure from xml_parse_into_struct()

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

    #1

    bizzare array structure from xml_parse_into_struct()

    I wanted to test xml_parse_into_ struct() so I took the example off of
    www.php.net and put this code up on a site:



    <?php


    $simple = <<<END
    <item>
    <cbId>345</cbId>
    <cbHeadline>H ey there</cbHeadline>
    <cbMainContent> God is not dead, he is merely
    sleeping.</cbMainContent>
    </item>
    <item>
    <cbId>432</cbId>
    <cbHeadline>Wha t a dog, what a dog</cbHeadline>
    <cbMainContent> In the manger.</cbMainContent>
    </item>
    END;

    $p = xml_parser_crea te();
    xml_parse_into_ struct($p, $simple, $vals, $index);
    xml_parser_free ($p);
    echo "Index array\n";
    print_r($index) ;
    echo "\nVals array\n";
    print_r($vals);


    ?>





    However, what I got back made no sense to me. Only the first ITEM made
    it into the arrays, and all the other tags somehow got counted as
    ITEMS. Why?

    If I simply want my XML code turned into a two dimensional array, what
    is the simplest way to do it? Is xml_parse_into_ struct working
    correctly, and I simply misunderstand it, or is there another way for
    me to do this?





    Index array
    Array
    (
    [ITEM] => Array
    (
    [0] => 0
    [1] => 2
    [2] => 3
    [3] => 5
    [4] => 6
    [5] => 8
    [6] => 9
    )

    [CBID] => Array
    (
    [0] => 1
    )

    [CBHEADLINE] => Array
    (
    [0] => 4
    )

    [CBMAINCONTENT] => Array
    (
    [0] => 7
    )

    )

    Vals array
    Array
    (
    [0] => Array
    (
    [tag] => ITEM
    [type] => open
    [level] => 1
    [value] =>

    )

    [1] => Array
    (
    [tag] => CBID
    [type] => complete
    [level] => 2
    [value] => 345
    )

    [2] => Array
    (
    [tag] => ITEM
    [value] =>

    [type] => cdata
    [level] => 1
    )

    [3] => Array
    (
    [tag] => ITEM
    [value] =>
    [type] => cdata
    [level] => 1
    )

    [4] => Array
    (
    [tag] => CBHEADLINE
    [type] => complete
    [level] => 2
    [value] => Hey there
    )

    [5] => Array
    (
    [tag] => ITEM
    [value] =>

    [type] => cdata
    [level] => 1
    )

    [6] => Array
    (
    [tag] => ITEM
    [value] =>
    [type] => cdata
    [level] => 1
    )

    [7] => Array
    (
    [tag] => CBMAINCONTENT
    [type] => complete
    [level] => 2
    [value] => God is not dead, he is merely sleeping.
    )

    [8] => Array
    (
    [tag] => ITEM
    [value] =>

    [type] => cdata
    [level] => 1
    )

    [9] => Array
    (
    [tag] => ITEM
    [type] => close
    [level] => 1
    )

    )
  • Zurab Davitiani

    #2
    Re: bizzare array structure from xml_parse_into_ struct()

    lawrence wrote:
    [color=blue]
    > $simple = <<<END
    > <item>
    > <cbId>345</cbId>
    > <cbHeadline>H ey there</cbHeadline>
    > <cbMainContent> God is not dead, he is merely
    > sleeping.</cbMainContent>
    > </item>
    > <item>
    > <cbId>432</cbId>
    > <cbHeadline>Wha t a dog, what a dog</cbHeadline>
    > <cbMainContent> In the manger.</cbMainContent>
    > </item>
    > END;[/color]

    This is not well-formed XML. You need to enclose the item tags inside a root
    element; you can only have 1 root element:

    <itemlist>
    <item>blahbla h</item>
    <item>blah2blah 2</item>
    </itemlist>

    If you are looking for a simple way to handle XML content without having to
    use a DOM parser, have a look at ActiveLink PHP XML Package:

    http://www.active-link.com/software/
    Download ActiveLink PHP XML Package for free. ActiveLink PHP XML Package provides means to parse, read, modify and output XML and XML documents without using any PHP XML libraries. Included classes are: XML, XMLDocument, XMLBranch, XMLLeaf, RSS, Tag, Tree, Branch, Leaf, File, Socket, HTTPClient.


    Comes with its own parser; read/modify/delete/update XML content with an
    easy to use interface; purely written in PHP, does NOT depend on or use any
    other XML extension/module. Disclaimer: I am the author of the package.

    Comment

    • lawrence

      #3
      Re: bizzare array structure from xml_parse_into_ struct()

      Zurab Davitiani <agt@mindless.c om> wrote in message news:<xQ2Mc.954 18$kI4.83760@ne wssvr29.news.pr odigy.com>...[color=blue]
      > This is not well-formed XML. You need to enclose the item tags inside a root
      > element; you can only have 1 root element:
      >
      > <itemlist>
      > <item>blahbla h</item>
      > <item>blah2blah 2</item>
      > </itemlist>[/color]

      Thanks much. I did as you suggested and wrapped everything above in a
      <channel> </channel> tag. Now what I get is workable, though I there
      is much about it that I still don't understand. Why does the CHANNEL
      array have 4 mentions of CHANNEL, when it only shows up twice, the
      opening tag <channgel> and the closing tag </channel>? Why does the
      ITEM array have 14 entries, when there are only 4 ITEM tags, two
      opening and two closing? Why after cbId is there another ITEM array,
      when there is no such tag there, and why is it given the type 'cdata'?

      This is the current output:





      Index array
      Array
      (
      [CHANNEL] => Array
      (
      [0] => 0
      [1] => 11
      [2] => 22
      [3] => 23
      )

      [ITEM] => Array
      (
      [0] => 1
      [1] => 3
      [2] => 4
      [3] => 6
      [4] => 7
      [5] => 9
      [6] => 10
      [7] => 12
      [8] => 14
      [9] => 15
      [10] => 17
      [11] => 18
      [12] => 20
      [13] => 21
      )

      [CBID] => Array
      (
      [0] => 2
      [1] => 13
      )

      [CBHEADLINE] => Array
      (
      [0] => 5
      [1] => 16
      )

      [CBMAINCONTENT] => Array
      (
      [0] => 8
      [1] => 19
      )

      )

      Vals array
      Array
      (
      [0] => Array
      (
      [tag] => CHANNEL
      [type] => open
      [level] => 1
      [value] =>

      )

      [1] => Array
      (
      [tag] => ITEM
      [type] => open
      [level] => 2
      [value] =>

      )

      [2] => Array
      (
      [tag] => CBID
      [type] => complete
      [level] => 3
      [value] => 345
      )

      [3] => Array
      (
      [tag] => ITEM
      [value] =>

      [type] => cdata
      [level] => 2
      )

      [4] => Array
      (
      [tag] => ITEM
      [value] =>
      [type] => cdata
      [level] => 2
      )

      [5] => Array
      (
      [tag] => CBHEADLINE
      [type] => complete
      [level] => 3
      [value] => Hey there, handsome
      )

      [6] => Array
      (
      [tag] => ITEM
      [value] =>

      [type] => cdata
      [level] => 2
      )

      [7] => Array
      (
      [tag] => ITEM
      [value] =>
      [type] => cdata
      [level] => 2
      )

      [8] => Array
      (
      [tag] => CBMAINCONTENT
      [type] => complete
      [level] => 3
      [value] => God is not dead, he is merely sleeping.
      )

      [9] => Array
      (
      [tag] => ITEM
      [value] =>

      [type] => cdata
      [level] => 2
      )

      [10] => Array
      (
      [tag] => ITEM
      [type] => close
      [level] => 2
      )

      [11] => Array
      (
      [tag] => CHANNEL
      [value] =>

      [type] => cdata
      [level] => 1
      )

      [12] => Array
      (
      [tag] => ITEM
      [type] => open
      [level] => 2
      [value] =>

      )

      [13] => Array
      (
      [tag] => CBID
      [type] => complete
      [level] => 3
      [value] => 432
      )

      [14] => Array
      (
      [tag] => ITEM
      [value] =>

      [type] => cdata
      [level] => 2
      )

      [15] => Array
      (
      [tag] => ITEM
      [value] =>
      [type] => cdata
      [level] => 2
      )

      [16] => Array
      (
      [tag] => CBHEADLINE
      [type] => complete
      [level] => 3
      [value] => What a dog, yes, what a dog
      )

      [17] => Array
      (
      [tag] => ITEM
      [value] =>

      [type] => cdata
      [level] => 2
      )

      [18] => Array
      (
      [tag] => ITEM
      [value] =>
      [type] => cdata
      [level] => 2
      )

      [19] => Array
      (
      [tag] => CBMAINCONTENT
      [type] => complete
      [level] => 3
      [value] => In the manger, was the cow.
      )

      [20] => Array
      (
      [tag] => ITEM
      [value] =>

      [type] => cdata
      [level] => 2
      )

      [21] => Array
      (
      [tag] => ITEM
      [type] => close
      [level] => 2
      )

      [22] => Array
      (
      [tag] => CHANNEL
      [value] =>

      [type] => cdata
      [level] => 1
      )

      [23] => Array
      (
      [tag] => CHANNEL
      [type] => close
      [level] => 1
      )

      )

      Comment

      • Zurab Davitiani

        #4
        Re: bizzare array structure from xml_parse_into_ struct()

        lawrence wrote:
        [color=blue]
        > Thanks much. I did as you suggested and wrapped everything above in a
        > <channel> </channel> tag. Now what I get is workable, though I there
        > is much about it that I still don't understand. Why does the CHANNEL
        > array have 4 mentions of CHANNEL, when it only shows up twice, the
        > opening tag <channgel> and the closing tag </channel>? Why does the
        > ITEM array have 14 entries, when there are only 4 ITEM tags, two
        > opening and two closing? Why after cbId is there another ITEM array,
        > when there is no such tag there, and why is it given the type 'cdata'?[/color]

        Because this is the way SAX parsers work - they don't parse the document in
        its entirety into a single structure (like DOM), they give you bits and
        pieces of information and it's up to you to determine how to use them.
        Those "type" values are for you to know whether the tag was opened, its
        contents parsed, closed, etc. The xml_parse_into_ struct tries to give SAX
        events a structural image, but I personally don't think it's very efficient
        or user-friendly.

        If you don't like SAX, or it's not useful for whatever you are doing -
        either use a DOM parser or follow the link to the XML package I gave you in
        my previous reply.

        Comment

        • lawrence

          #5
          Re: bizzare array structure from xml_parse_into_ struct()

          Zurab Davitiani <agt@mindless.c om> wrote in message news:<avBNc.331 4[color=blue]
          > Because this is the way SAX parsers work - they don't parse the document in
          > its entirety into a single structure (like DOM), they give you bits and
          > pieces of information and it's up to you to determine how to use them.
          > Those "type" values are for you to know whether the tag was opened, its
          > contents parsed, closed, etc. The xml_parse_into_ struct tries to give SAX
          > events a structural image, but I personally don't think it's very efficient
          > or user-friendly.
          >
          > If you don't like SAX, or it's not useful for whatever you are doing -
          > either use a DOM parser or follow the link to the XML package I gave you in
          > my previous reply.[/color]

          That's interesting about SAX parsers. They don't sound very useful.

          I'll look into your package, but let me ask a question about it. What
          I really want is simply to grab this XML and get back a 2 dimensional
          array, then loop through and get out each ITEM, then loop through each
          ITEM and the value of each element. Does your package make it easy for
          me to grab a 2 dimensional array out of the above 2 dimensional XML?
          If not, should I use a DOM parser for that?

          Comment

          • lawrence

            #6
            Re: bizzare array structure from xml_parse_into_ struct()

            Zurab Davitiani <agt@mindless.c om> wrote in message news:<avBNc.331 4[color=blue]
            > Because this is the way SAX parsers work - they don't parse the document in
            > its entirety into a single structure (like DOM), they give you bits and
            > pieces of information and it's up to you to determine how to use them.
            > Those "type" values are for you to know whether the tag was opened, its
            > contents parsed, closed, etc. The xml_parse_into_ struct tries to give SAX
            > events a structural image, but I personally don't think it's very efficient
            > or user-friendly.
            >
            > If you don't like SAX, or it's not useful for whatever you are doing -
            > either use a DOM parser or follow the link to the XML package I gave you in
            > my previous reply.[/color]

            Okay, I went and read the online tutorials for the ActiveLink PHP XML
            Package. The organization and cleaness of the documentation is very
            impressive. However, my first reaction is that it does way too much
            what I need, and would be way too hard to integrate into my existing
            code. I've already a lot of code that writes XML, so including this
            package would give me a lot of redundant code, I think.

            I admire the clean and intelligent simplicity of the ActiveLink site.

            Comment

            • Zurab Davitiani

              #7
              Re: bizzare array structure from xml_parse_into_ struct()

              lawrence wrote:
              [color=blue]
              > That's interesting about SAX parsers. They don't sound very useful.[/color]

              SAX parsers are good for parsing large XML documents for specific
              information. They are quicker and consumer less memory than DOM; but again,
              they are not as useful when you want a tree-like structure.
              [color=blue]
              > I'll look into your package, but let me ask a question about it. What
              > I really want is simply to grab this XML and get back a 2 dimensional
              > array, then loop through and get out each ITEM, then loop through each
              > ITEM and the value of each element. Does your package make it easy for
              > me to grab a 2 dimensional array out of the above 2 dimensional XML?
              > If not, should I use a DOM parser for that?[/color]

              If you want to create a 2-dimensional array you could, but you would not
              need an array at all. If I took your example, it would be:

              $myXML = new XML($simple);
              $itemsXMLArray = $myXML->getBranches("c hannel", "item");
              foreach($itemsX MLArray as $itemXML) {
              print("cbId: " . $itemXML->getTagContent( "item/cbId");
              //print other fields here
              }

              Add some error checking and it's good to go.

              Comment

              • Zurab Davitiani

                #8
                Re: bizzare array structure from xml_parse_into_ struct()

                Zurab Davitiani wrote:
                [color=blue]
                > $myXML = new XML($simple);
                > $itemsXMLArray = $myXML->getBranches("c hannel", "item");
                > foreach($itemsX MLArray as $itemXML) {
                > print("cbId: " . $itemXML->getTagContent( "item/cbId");
                > //print other fields here
                > }
                >
                > Add some error checking and it's good to go.[/color]

                Damn, missing closing parethesis at the end of print line!

                Comment

                • lawrence

                  #9
                  Re: bizzare array structure from xml_parse_into_ struct()

                  Zurab Davitiani <agt@mindless.c om> wrote in message news:<JcYNc.369 5$AY5.2118@news svr21.news.prod igy.com>...[color=blue]
                  > lawrence wrote:
                  >[color=green]
                  > > That's interesting about SAX parsers. They don't sound very useful.[/color]
                  >
                  > SAX parsers are good for parsing large XML documents for specific
                  > information. They are quicker and consumer less memory than DOM; but again,
                  > they are not as useful when you want a tree-like structure.
                  >[color=green]
                  > > I'll look into your package, but let me ask a question about it. What
                  > > I really want is simply to grab this XML and get back a 2 dimensional
                  > > array, then loop through and get out each ITEM, then loop through each
                  > > ITEM and the value of each element. Does your package make it easy for
                  > > me to grab a 2 dimensional array out of the above 2 dimensional XML?
                  > > If not, should I use a DOM parser for that?[/color]
                  >
                  > If you want to create a 2-dimensional array you could, but you would not
                  > need an array at all. If I took your example, it would be:
                  >
                  > $myXML = new XML($simple);
                  > $itemsXMLArray = $myXML->getBranches("c hannel", "item");
                  > foreach($itemsX MLArray as $itemXML) {
                  > print("cbId: " . $itemXML->getTagContent( "item/cbId");
                  > //print other fields here
                  > }
                  >
                  > Add some error checking and it's good to go.[/color]

                  That's very impressive. What is the license on this software? I see on
                  the website it is described as "free". Does that mean GPL, public
                  domain, or rights reserved but the cost is $0?

                  Comment

                  • Zurab Davitiani

                    #10
                    Re: bizzare array structure from xml_parse_into_ struct()

                    lawrence wrote:
                    [color=blue]
                    > That's very impressive. What is the license on this software? I see on
                    > the website it is described as "free". Does that mean GPL, public
                    > domain, or rights reserved but the cost is $0?[/color]

                    Thanks for the compliment. The license is included with the download archive
                    - it's LGPL. Remember to read tutorial and documentation (all class methods
                    are documented), and, if you end up liking it, enjoy!

                    Comment

                    • lawrence

                      #11
                      Re: bizzare array structure from xml_parse_into_ struct()

                      Could this package be corrupted? I've downloaded the XML package from
                      this site twice now:

                      http://www.active-link.com/software/

                      I keep trying to open it with WinZip, but I always get an error. Is
                      the double tar normal?



                      lkrubner@geocit ies.com (lawrence) wrote in message news:<da7e68e8. 0407281338.a0c9 80c@posting.goo gle.com>...[color=blue]
                      > Zurab Davitiani <agt@mindless.c om> wrote in message news:<avBNc.331 4[color=green]
                      > > Because this is the way SAX parsers work - they don't parse the document in
                      > > its entirety into a single structure (like DOM), they give you bits and
                      > > pieces of information and it's up to you to determine how to use them.
                      > > Those "type" values are for you to know whether the tag was opened, its
                      > > contents parsed, closed, etc. The xml_parse_into_ struct tries to give SAX
                      > > events a structural image, but I personally don't think it's very efficient
                      > > or user-friendly.
                      > >
                      > > If you don't like SAX, or it's not useful for whatever you are doing -
                      > > either use a DOM parser or follow the link to the XML package I gave you in
                      > > my previous reply.[/color]
                      >
                      > Okay, I went and read the online tutorials for the ActiveLink PHP XML
                      > Package. The organization and cleaness of the documentation is very
                      > impressive. However, my first reaction is that it does way too much
                      > what I need, and would be way too hard to integrate into my existing
                      > code. I've already a lot of code that writes XML, so including this
                      > package would give me a lot of redundant code, I think.
                      >
                      > I admire the clean and intelligent simplicity of the ActiveLink site.[/color]

                      Comment

                      • Michael Fesser

                        #12
                        Re: bizzare array structure from xml_parse_into_ struct()

                        .oO(lawrence)
                        [color=blue]
                        >Could this package be corrupted? I've downloaded the XML package from
                        >this site twice now:
                        >
                        >http://www.active-link.com/software/[/color]

                        Works here (WinZip 9).
                        [color=blue]
                        >I keep trying to open it with WinZip, but I always get an error. Is
                        >the double tar normal?[/color]

                        It's a gzipped tarball (tar archives, gzip compresses), quite common.
                        What version of WinZip do you use?

                        Try this re-packed version (lower compression rate):

                        <http://www.mfesser.de/temp/ActiveLink-PHP-XML-Package-0.3.4.zip> [58KB]

                        Micha

                        Comment

                        • lawrence

                          #13
                          Re: bizzare array structure from xml_parse_into_ struct()

                          Michael Fesser <netizen@gmx.ne t> wrote in message[color=blue][color=green]
                          > >I keep trying to open it with WinZip, but I always get an error. Is
                          > >the double tar normal?[/color]
                          >
                          > It's a gzipped tarball (tar archives, gzip compresses), quite common.
                          > What version of WinZip do you use?
                          >
                          > Try this re-packed version (lower compression rate):
                          >
                          > <http://www.mfesser.de/temp/ActiveLink-PHP-XML-Package-0.3.4.zip> [58KB]
                          >
                          > Micha[/color]

                          Thanks, I've a working version of the package now. I like the fact
                          that each class is in its own file, that is how my own code works as
                          well. That makes it somewhat easy to import. It will, I think, only
                          take a day or two to get the thing working in my own software. They
                          copyright notice will of course stay where it is in each file. But I
                          need to have all the files in one directory, so I'm going to have to
                          completely change the way the files get imported. Thta will take a bit
                          of reworking. I'm not sure I understand the import() function yet, but
                          I assume I'll get rid of it, as my own software has its own, very
                          particular import method.

                          I may write some additional comments as I study the code. I'll post so
                          others learning the code can perhaps benefit from whatever I learn.
                          The code, on brief review, looks like a model of clarity.

                          Comment

                          • lawrence

                            #14
                            Re: bizzare array structure from xml_parse_into_ struct()

                            From: Zurab Davitiani (agt@mindless.c om)[color=blue]
                            > I'll look into your package, but let me ask a question about it. What
                            > I really want is simply to grab this XML and get back a 2 dimensional
                            > array, then loop through and get out each ITEM, then loop through each
                            > ITEM and the value of each element. Does your package make it easy for
                            > me to grab a 2 dimensional array out of the above 2 dimensional XML?
                            > If not, should I use a DOM parser for that?[/color]

                            If you want to create a 2-dimensional array you could, but you would
                            not
                            need an array at all. If I took your example, it would be:

                            $myXML = new XML($simple);
                            $itemsXMLArray = $myXML->getBranches("c hannel", "item");
                            foreach($itemsX MLArray as $itemXML) {
                            print("cbId: " . $itemXML->getTagContent( "item/cbId");
                            //print other fields here
                            }[color=blue][color=green][color=darkred]
                            >>>>>>>>>>>>>>> >>>>>>>
                            >>>>>>>>>>>>>>> >>>>>>>
                            >>>>>>>>>>>>>>> >>>>>>>[/color][/color][/color]

                            That is very clean. I'm just getting to use it now. Your package is
                            one of the few that I did not write that I've been able to use. With
                            most classes available on the web the programmer makes too many
                            assumptions and the code doesn't port well.

                            Comment

                            • Charles Stricklin

                              #15
                              Q/ How do I parse values from a RSS feed?

                              If I have an RSS newsfeed like this:

                              <?xml version="1.0" encoding="utf-8"?><!-- generator="whoc ares" -->
                              <rss version="0.92">
                              <channel>
                              <title>Websit e Name</title>
                              <link>http://www.websiteurl. com</link>
                              <description>De scription of website</description>
                              <lastbuilddate> Sun, 19 Sep 2004 04:34:52 +0000</lastbuilddate>
                              <docs>http://backend.userlan d.com/rss092</docs>

                              <item>
                              <title>1st Title</title>
                              <description>1s t Description</description>
                              <link>http://www.websiteurl. com/1stlink/</link>
                              </item>

                              <item>
                              <title>2nd Title</title>
                              <description>2n d Description</description>
                              <link>http://www.websiteurl. com/2ndlink/</link>
                              </item>
                              </channel>
                              </rss>

                              And the following code is used to parse that file/feed:

                              <?php
                              $feed = 'http://www.example.com/feed/rss/';

                              /* ...create and XML parser... */
                              $xml_parser = xml_parser_crea te();

                              /* ...open the feed and parse it... */
                              $fp = @fopen($feed, 'rb');
                              if (is_resource($f p)) {
                              xml_parse_into_ struct( $xml_parser, $fp, $vals, $index );
                              }
                              @fclose($fp);

                              /* ...free parser */
                              xml_parser_free ( $xml_parser );
                              ?>

                              How do I extract the values from $xml_parser?


                              Comment

                              Working...