473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Best way to create XML stream from assoc array

Purple
404 Expert 256MB
Hi All,

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

Regards Purple
Aug 31 '07 #1
19 2663
pbmods
5,821 Expert 4TB
Heya, Purple.

You mean something like this:
Expand|Select|Wrap|Line Numbers
  1. array
  2. (
  3.     'name'            => 'John',
  4.     'age'            => '38',
  5.     'orientation'    => 'up'
  6. );
  7.  
into this:
Expand|Select|Wrap|Line Numbers
  1.     <name>John</name>
  2.     <age>38</age>
  3.     <orientation>up</orientation>
  4.  
?
Aug 31 '07 #2
Purple
404 Expert 256MB
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
Aug 31 '07 #3
pbmods
5,821 Expert 4TB
Heya, Purple.

The fastest way I can think of to accomplish this would be something like this:
Expand|Select|Wrap|Line Numbers
  1. $xml = '<?xml version="1.0" encoding="utf-8"?><introtags ... >';
  2. foreach( $data as $_key => $_value )
  3. {
  4.     $xml .= "<{$_key}>{$_value}</{$_key}>";
  5. }
  6. $xml .= '<closingtags ... >';
  7.  
It's only sightly more complicated if you want to make it pretty to look at :P
Aug 31 '07 #4
Purple
404 Expert 256MB
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
Aug 31 '07 #5
pbmods
5,821 Expert 4TB
Heya, Purple.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 31 '07 #6
gregerly
192 Expert 100+
Heya, Purple.

The fastest way I can think of to accomplish this would be something like this:
Expand|Select|Wrap|Line Numbers
  1. $xml = '<?xml version="1.0" encoding="utf-8"?><introtags ... >';
  2. foreach( $data as $_key => $_value )
  3. {
  4.     $xml .= "<{$_key}>{$_value}</{$_key}>";
  5. }
  6. $xml .= '<closingtags ... >';
  7.  
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
Aug 31 '07 #7
ak1dnar
1,584 Expert 1GB
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.
Aug 31 '07 #8
gregerly
192 Expert 100+
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
Aug 31 '07 #9
pbmods
5,821 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. $str = $flower . 's are red';
  2. $str = "{$flower}s are blue;";
  3.  
Or if you want to use a multidimensional array:
Expand|Select|Wrap|Line Numbers
  1. $str = $ingredients['baking'][$i] . ' is sweet.';
  2. $str = "And so are {$pronouns['personal'][$j]}";
  3.  
Or if you need to use variable variables:
Expand|Select|Wrap|Line Numbers
  1. $str = "I lost my {$$obj}; can I have yours?";
  2.  
For more information, check out this document.

PS. How's that article coming?
Aug 31 '07 #10
ak1dnar
1,584 Expert 1GB
But variables (excluding array's) can be placed within a string without the use of { }'s.
excluding arrays?
Does it have something to do with the way the variables are declared in the foreach loop? IE $_key => $_value?
Expand|Select|Wrap|Line Numbers
  1. <?Php
  2. $myArray = array('ABC','DEF','GHI');
  3.  
  4. echo "{$myArray[0]}<br>";
  5. echo "$myArray[0]<br>";
  6.  
  7. foreach($myArray as $key => $value){
  8. echo "$key >> $value<br>";
  9. }
  10. foreach($myArray as $key => $value){
  11. echo "{$key} >> {$value}<br>";
  12. }
  13. ?>
So now what? Its only another way only. but there might be some..(need to read the doc)
Aug 31 '07 #11
ak1dnar
1,584 Expert 1GB
Sorry about your thread Purple. It's greg and pb. They gonna hijack this.
Aug 31 '07 #12
gregerly
192 Expert 100+
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
Aug 31 '07 #13
pbmods
5,821 Expert 4TB
Sorry about your thread Purple. It's greg and pb. They gonna hijack this.
Hmph. [WHINE]Greg started it![/WHINE] >P
Aug 31 '07 #14
gregerly
192 Expert 100+
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.
Sep 1 '07 #15
pbmods
5,821 Expert 4TB
[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.
Very good. You used echo instead of print. But ++$i would give you slightly faster performance ~_^
Sep 1 '07 #16
gregerly
192 Expert 100+
Very good. You used echo instead of print. But ++$i would give you slightly faster performance ~_^
Hows this:

[PHP]<?
$sql="SELECT * FROM `what_pbmods_knows_tbl`;"
$result=@msql_query($sql);

//loop thru what pbmods knows
while(list($everything)=@mysql_fetch_row($result)) {
//this is a trick question....this loop will run forever!
}
?>[/PHP]
Sep 1 '07 #17
pbmods
5,821 Expert 4TB
Hows this:

[PHP]<?
$sql="SELECT * FROM `what_pbmods_knows_tbl`;"
$result=@msql_query($sql);

//loop thru what pbmods knows
while(list($everything)=@mysql_fetch_row($result)) {
//this is a trick question....this loop will run forever!
}
?>[/PHP]
Well done, Greg.

However...

The query passed to mysql_query() [or msql_query(), if you prefer 0:) ] should not end with a semicolon (no, really).

Don't make me split this into its own thread :P
Sep 1 '07 #18
gregerly
192 Expert 100+
Well done, Greg.

However...

The query passed to mysql_query() [or msql_query(), if you prefer 0:) ] should not end with a semicolon (no, really).

Don't make me split this into its own thread :P
Whoops, besides my typo....no semicolon?! I've never heard that before....are you pulling my leg?
Sep 1 '07 #19
pbmods
5,821 Expert 4TB
Whoops, besides my typo....no semicolon?! I've never heard that before....are you pulling my leg?
No, really. Check it out:

Parameters


query
A SQL query

The query string should not end with a semicolon.
Sep 1 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
5
by: sherifffruitfly | last post by:
Hi, I'm just learning cpp, and the exercise I'm working on is basically as follows: 1) Create a struct type with 4 members (char, char, char, int). 2) Create an array of, say 3 instances of...
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
5
by: KDawg44 | last post by:
Hi, Is there a way to get a multidimensional associative array with the entire result set? I would like to get a an array like this: resultsArray How can I accomplish this? Can I do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.