/    Sign up×
Community /Pin to ProfileBookmark

Can I sort an xml file with php

Hi,
beginner question,
I want to re-sort an xml file at the beginning of my php file.
Let’s say for example my xml looks like this;

[CODE]<zoo>
<park one>
<cow>Friesian</cow>
<dog>Labrador</dog>
<sheep>Barbados Blackbelly</sheep>
<horse>Shire</horse>
</park one>
<park two>
<cow>Jersey</cow>
<dog>Doberman</dog>
<sheep>Debyshire Gritstone</sheep>
<horse>Spanish Mustang</horse>
</park two>
</zoo>[/CODE]

I would like to re-sort it in my php routine just for use in this routine to;

[CODE]<zoo>
<park one>
<sheep>Barbados Blackbelly</sheep>
<horse>Shire</horse>
<dog>Labrador</dog>
<cow>Friesian</cow><horse>Shire</horse>
</park one>
<park two>
<sheep>Derbyshire Grritstone</sheep>
<horse>Spanish Mustang</horse>
<dog>Doberman</dog>
<cow>Jersey</cow><horse>Shire</horse>
</park two>
</zoo>[/CODE]

Therefore cow, dog sheep horse to sheep horse dog cow.
I’d prefer not to use an xslt file and do it with php if possible
Thanks for any help.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 07.2009 — I'm sure some way could be found, but first it begs the question, "Why?" What does it matter in which order those elements appear in the XML? On the surface it seems like worrying about what order a DBMS stores a table's records: it shouldn't matter, should it?

And, if it does matter for some reason I have not imagined, what is the algorithm that determines the order in which they should be ordered?
Copy linkTweet thisAlerts:
@SodbusterMar 07.2009 — What NogDog said saves me a lot of typing, and he says it better than I would have. XML is for the "transport and storage" of data. Use PHP variables, objects, etc. for the manipulation.

Also, your element naming renders the XML invalid (no spaces allowed in names). If you want to designate a unique name for each park, use an attribute: e.g. "<park number="two">".

But if you need to sort an array of data that's already randomly accessible via its string keys, you can do like this ("number" attribute added to "park"):[code=php]$doc = <<<DOC
<zoo>
<park number="one">
<cow>Friesian</cow>
<dog>Labrador</dog>
<sheep>Barbados Blackbelly</sheep>
<horse>Shire</horse>
</park>
<park number="two">
<cow>Jersey</cow>
<dog>Doberman</dog>
<sheep>Debyshire Gritstone</sheep>
<horse>Spanish Mustang</horse>
</park>
</zoo>
DOC;


$sxml = new SimpleXMLElement($doc);
foreach ($sxml->children() as $park) {
$key = (string) $park->attributes()->number;
$data[$key]['sheep'] = (string) $park->sheep;
$data[$key]['horse'] = (string) $park->horse;
$data[$key]['dog'] = (string) $park->dog;
$data[$key]['cow'] = (string) $park->cow;
}

print_r($data);[/code]
×

Success!

Help @CityGal 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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