/    Sign up×
Community /Pin to ProfileBookmark

Can not get XML format right

Hi All
Can anyone please help me to format my XML by using PHP, there are NO errors in my code but I need to output the result in this format.

[CODE]<states>
<state name=”Alaska” colour=”#ff0000″>
<point lat=”70.0187″ lng=”-141.0205″/>
<point lat=”70.1292″ lng=”-141.7291″/>
<point lat=”70.0187″ lng=”-141.0205″/>
</state>
</states>[/CODE]

This is my php code and output.

[code=php]$query = “SELECT u.uname, a.pcode1, a.latitude, a.county, a.longitude FROM crm_geo_area_allocs c, crm_geo_areas a, crmusers u
WHERE c.uid = u.uid
AND c.pcode1 = a.pcode1”;
$result = mysqli_query ($dbh, $query ) or die ( mysqli_error () );

$xml = new SimpleXMLElement(‘<xml/>’);
while($row = mysqli_fetch_assoc($result)) {

$draw = $xml->addChild(‘location’);
$draw->addChild(‘uname’,$row[‘uname’]);
$draw->addChild(‘colour’,$row[‘colour’]);
$draw->addChild(‘postcode’,$row[‘pcode1’]);
$draw->addChild(‘county’,$row[‘county’]);
$draw->addChild(‘latitude’,$row[‘latitude’]);
$draw->addChild(‘longitude’,$row[‘longitude’]);
}
mysqli_close($dbh);
$fp = fopen(“get_locationsXML.xml”,”wb”);
fwrite($fp,$xml->asXML());
fclose($fp);[/code]

The output.

[CODE]<xml>
<location>
<uname>Bruce Brassington</uname>
<colour/>
<postcode>LS10</postcode>
<county>Leeds</county>
<latitude>53.7653</latitude>
<longitude>-1.53195</longitude>
</location>
</xml>[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@zed420authorSep 16.2015 — Anyone with any bright idea ???
Copy linkTweet thisAlerts:
@zed420authorSep 17.2015 — Thanks everyone who looked at it, I have managed to solve it this is how,

[code=php]$query = "SELECT u.uname, a.pcode1, a.county, c.colour, a.latitude, a.longitude FROM crm_geo_area_allocs c, crm_geo_areas a, crmusers u
WHERE c.uid = u.uid
AND c.pcode1 = a.pcode1";
$result = mysqli_query ($dbh, $query ) or die ( mysqli_error () );

$xml = new SimpleXMLElement ( '<xml/>' );
while ( $row = mysqli_fetch_assoc ( $result ) ) {

$draw = $xml->addChild ( 'locations' );
$location = $draw->addChild ( "location" );
$location->addAttribute ( 'uname', $row ['uname'] );
$location->addAttribute ( 'postcode', $row ['pcode1'] );
$location->addAttribute ( 'county', $row ['county'] );
$location->addAttribute ( 'colour', $row ['colour'] );
$point = $location->addChild ( 'point' );
$point->addAttribute ( 'latitude', $row ['latitude'] );
$point->addAttribute ( 'longitude', $row ['longitude'] );
}
mysqli_close ( $dbh );
$fp = fopen ( "get_locationsXML.xml", "wb" );
fwrite ( $fp, $xml->asXML () );
fclose ( $fp );[/code]
×

Success!

Help @zed420 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...