Best way to create XML stream from assoc array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Purple
    Recognized Expert Contributor
    • May 2007
    • 404

    Best way to create XML stream from assoc array

    Hi All,

    What is the best way to create an XML stream from an assoc array in php.

    Regards Purple
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Purple.

    You mean something like this:
    [code=php]
    array
    (
    'name' => 'John',
    'age' => '38',
    'orientation' => 'up'
    );
    [/code]

    into this:
    [code=xml]
    <name>John</name>
    <age>38</age>
    <orientation>up </orientation>
    [/code]
    ?

    Comment

    • Purple
      Recognized Expert Contributor
      • May 2007
      • 404

      #3
      Hi pbmods,

      yes, thats it - I am working on a generalised function which creates datastreams for extjs. The JSON was simple, the XML doesn't appear so - my assumption is there must be an easier way..

      Regards Purple

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Purple.

        The fastest way I can think of to accomplish this would be something like this:
        [code=php]
        $xml = '<?xml version="1.0" encoding="utf-8"?><introta gs ... >';
        foreach( $data as $_key => $_value )
        {
        $xml .= "<{$_key}>{$_va lue}</{$_key}>";
        }
        $xml .= '<closingtags ... >';
        [/code]

        It's only sightly more complicated if you want to make it pretty to look at :P

        Comment

        • Purple
          Recognized Expert Contributor
          • May 2007
          • 404

          #5
          Thanks for that pbmods,

          after working with the json_encode function:

          [PHP]$this->json_data = '{"'.$header.'" :'.json_encode( $this->queue).'}';[/PHP]

          where $this->queue is an assoc array,

          I think I was expecting something a little more funky :)

          Thanks for your help !

          Purple

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Purple.

            Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

            Comment

            • gregerly
              Recognized Expert New Member
              • Sep 2006
              • 192

              #7
              Originally posted by pbmods
              Heya, Purple.

              The fastest way I can think of to accomplish this would be something like this:
              [code=php]
              $xml = '<?xml version="1.0" encoding="utf-8"?><introta gs ... >';
              foreach( $data as $_key => $_value )
              {
              $xml .= "<{$_key}>{$_va lue}</{$_key}>";
              }
              $xml .= '<closingtags ... >';
              [/code]

              It's only sightly more complicated if you want to make it pretty to look at :P
              Hey Pbmods,

              I had a question about the syntax you are using within your foreach loop, specifially the "<{$_key}>" . I'm curious about the use of brackets. I've seen it before, but I'm not sure that I know what it means. What does using the { }'s do?

              Thanks,

              Greg

              Comment

              • ak1dnar
                Recognized Expert Top Contributor
                • Jan 2007
                • 1584

                #8
                Originally posted by gregerly
                Hey Pbmods,

                I had a question about the syntax you are using within your foreach loop, specifially the "<{$_key}>" . I'm curious about the use of brackets. I've seen it before, but I'm not sure that I know what it means. What does using the { }'s do?

                Thanks,

                Greg
                It's just another way to put variables inside of a string in php, when there we have double quotes.

                Comment

                • gregerly
                  Recognized Expert New Member
                  • Sep 2006
                  • 192

                  #9
                  Originally posted by ajaxrand
                  It's just another way to put variables inside of a string in php, when there we have double quotes.
                  But variables (excluding array's) can be placed within a string without the use of { }'s. Does it have something to do with the way the variables are declared in the foreach loop? IE $_key => $_value?

                  Thanks

                  Greg

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Heya, Greg.

                    It's really just a style issue for me. When there's curly braces around a variable, it helps make it more obvious to me that there's a variable there. Sometimes, especially when the string is long and distinguished, the '$' gets lost. Adding a curly brace around the variable name just makes it stand out a bit more.

                    Complex (or "Curly") syntax, as it is called, is also useful if you need to put a variable next to an alphanumeric character in a string:
                    [code=php]
                    $str = $flower . 's are red';
                    $str = "{$flower}s are blue;";
                    [/code]

                    Or if you want to use a multidimensiona l array:
                    [code=php]
                    $str = $ingredients['baking'][$i] . ' is sweet.';
                    $str = "And so are {$pronouns['personal'][$j]}";
                    [/code]

                    Or if you need to use variable variables:
                    [code=php]
                    $str = "I lost my {$$obj}; can I have yours?";
                    [/code]

                    For more information, check out this document.

                    PS. How's that article coming?

                    Comment

                    • ak1dnar
                      Recognized Expert Top Contributor
                      • Jan 2007
                      • 1584

                      #11
                      Originally posted by gregerly
                      But variables (excluding array's) can be placed within a string without the use of { }'s.
                      excluding arrays?
                      Originally posted by gregerly
                      Does it have something to do with the way the variables are declared in the foreach loop? IE $_key => $_value?
                      [CODE=php]<?Php
                      $myArray = array('ABC','DE F','GHI');

                      echo "{$myArray[0]}<br>";
                      echo "$myArray[0]<br>";

                      foreach($myArra y as $key => $value){
                      echo "$key >> $value<br>";
                      }
                      foreach($myArra y as $key => $value){
                      echo "{$key} >> {$value}<br>";
                      }
                      ?>[/CODE]

                      So now what? Its only another way only. but there might be some..(need to read the doc)

                      Comment

                      • ak1dnar
                        Recognized Expert Top Contributor
                        • Jan 2007
                        • 1584

                        #12
                        Sorry about your thread Purple. It's greg and pb. They gonna hijack this.

                        Comment

                        • gregerly
                          Recognized Expert New Member
                          • Sep 2006
                          • 192

                          #13
                          Yeah, sorry to hijack the post. I'll make sure it doesn't happen again. I appreciate the info on the curly syntax. In all my books and tutorials I've ever read, I haven't come across the curly syntax often.

                          Again, thanks for the info.

                          Greg

                          Comment

                          • pbmods
                            Recognized Expert Expert
                            • Apr 2007
                            • 5821

                            #14
                            Originally posted by ajaxrand
                            Sorry about your thread Purple. It's greg and pb. They gonna hijack this.
                            Hmph. [WHINE]Greg started it![/WHINE] >P

                            Comment

                            • gregerly
                              Recognized Expert New Member
                              • Sep 2006
                              • 192

                              #15
                              Originally posted by pbmods
                              Hmph. [WHINE]Greg started it![/WHINE] >P
                              [PHP]<?
                              for($i=0; $i<10000;$i++){
                              echo "it's not my fault! It was all Pbmods's fault, having interesting content to his posts!<br />";
                              }
                              ?>[/PHP]

                              Greg said emphatically... i'm such a nerd.

                              Comment

                              Working...