Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a XML-file from my php-array

Status
Not open for further replies.

pknudsen

Programmer
Sep 20, 2010
5
0
0
DK
I have an array created in php:
$files[] = array(
"filename"=>$file,
"intLatitude"=>$intLatitude,
"intLongitude"=>$intLongitude);

I need to create a XML-file with the php-file informations included. It should look so:

The XML-file should look like this:

<metadata>
<link href=" <text>Garmin International</text>
</link>
<bounds minlat="55.74998330" minlon="12.21786460" maxlat="55.79861700" maxlon="12.26445030"/>
<extensions>
<time xmlns=" </extensions>
</metadata>
---------and then loop the array:
<wpt lat="55.79854" lon="12.22697"> ----> the variables $intLatitude and $intLongitude
<extensions>
<html>
<![CDATA[
<a href=" target="_blank"><img src="imagefilename" /></a>--->the $file
]]>
</html>
</extensions>
</wpt>

Please help me to create a procedure to generate this XML-file
 
[0] >I have an array created in php:
I am not sure that creates anything proper.
>$files[] = array(
[tt]$file[highlight]s [/highlight]= array([/tt]

[1] >The XML-file should look like this:
Again, that can't be an xml file looking like, it looks like more an xml fragment or whatever?

[1.1] If you simply want to know how to make a string of the sort, this is how you do with heredoc.
[tt]
$x=<<<EOD
<metadata>
<link href="[ignore][/ignore]">
<text>Garmin International</text>
</link>
<bounds minlat="55.74998330" minlon="12.21786460" maxlat="55.79861700" maxlon="12.26445030"/>
<extensions>
<time xmlns="[ignore][/ignore]">2010-09-10T08:09:33.842Z</time>
</extensions>
</metadata>
<wpt lat="[red]{$files['intLatitude']}[/red]" lon="[red]{$files['intLongitude']}[/red]">
<extensions>
<html>
<![CDATA[
<a href="[ignore][/ignore]" target="_blank"><img src="[red]{$files['filename']}[/red]" /></a>
]]>
</html>
</extensions>
</wpt>
EOD;
[/tt]
 
The [1.1] is the just the answer I need to tell me the syntax of the code.

Maybe you will understand if I tell what I should use the file to?
I should make a map in Gmaps (Googles map-system).
It is needed that the input should be an XML- (or an XML- look-like) file.
My code should result in an file saved with the extension XML. (how do I save the string?).

I have tried to handcode the file in Nodepad and save it to "myfile.xml" and then it works.

I think that your example will work if you can tell me:

1. How do I save the string in a file.

2. How do I "unpack" the string when i should use it.

3. The variables in this part:

<wpt lat="{$files['intLatitude']}" lon="{$files['intLongitude']}"><extensions><html><![CDATA[<a href=" target="_blank"><img src="{$files['filename']}" /></a>]]> </html></extensions></wpt>

is taken from the array and should therefore be a loop. How do I create this loop?
 
[2] The whole thing is based on some assumptions.
[2.1] Suppose the whole thing is wrapped inside a root called <root>.
[2.2] Suppose $files is really an array of array, such that.
[tt]
$files[0] = array(
"filename"=>$file,
"intLatitude"=>$intLatitude,
"intLongitude"=>$intLongitude);

//some another set of variables...
$files[1] = array(
"filename"=>$file_1,
"intLatitude"=>$intLatitude_1,
"intLongitude"=>$intLongitude_1);

//etc...
[/tt]
[2.3] That the string is to save to some file called "out.xml" of the current directory.

[3] The mechanism of looping through and saving can be illustrated by this.
[tt]
$x=<<<EOD
<root>
<metadata>
<link href="[ignore][/ignore]">
<text>Garmin International</text>
</link>
<bounds minlat="55.74998330" minlon="12.21786460" maxlat="55.79861700" maxlon="12.26445030"/>
<extensions>
<time xmlns="[ignore][/ignore]">2010-09-10T08:09:33.842Z</time>
</extensions>
</metadata>

EOD;

for ($i=0;$i<count($files);$i++) {
$x.=<<<EOD
<wpt lat="{$files[$i]['intLatitude']}" lon="{$files[$i]['intLongitude']}">
<extensions>
<html>
<![CDATA[
<a href="[ignore][/ignore]" target="_blank"><img src="{$files[$i]['filename']}" /></a>
]]>
</html>
</extensions>
</wpt>

EOD;
}

$x.=<<<EOD
</root>
EOD;

$spath="out.xml";
$f=fopen($spath,'w');
fwrite($f,$x);
fclose($f);
[/tt]
[4] If you do not really know how php works, you better ask general php syntax question on php in the dedicated forum:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top