www.webdeveloper.com
Results 1 to 3 of 3

Thread: Can I sort an xml file with php

  1. #1
    Join Date
    Jan 2009
    Posts
    16

    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>
    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>
    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.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    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?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2008
    Posts
    150
    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"):
    PHP Code:
    $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); 
    Last edited by Sodbuster; 03-07-2009 at 04:27 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles