Results 1 to 2 of 2

Thread: A question about reordering XML

  1. #1
    Join Date
    Feb 2010
    Location
    Vancouver, Canada
    Posts
    24

    A question about reordering XML

    Hi there, so here's my sample XML file:

    HTML Code:
    <root>
    <item number="1"/>
    <item number="4"/>
    <item number="2"/>
    <item number="3"/>
    </root>
    I'm building a backend to edit some XML for a client out of PHP, and I need it to sort the items by the "number" attribute. I've tried a few methods, including arrays, but I seem to be having some trouble still. I can't use XSLT unfortunately on the server where this is heading.

    I've played with the following code so far, but I seem to be having trouble saving it back into the XML file after.

    PHP Code:
        $url '_db/items.xml';
        
    $file simplexml_load_file($url);
        
    $sxe = new SimpleXMLElement($file->asXML());

        
    $nodes $sxe->xpath('/root/item');

        function 
    sort_items($t1$t2) {
            return 
    strcmp($t1['number'], $t2['number']);
        }

        
    usort($nodes'sort_items');
        
    $sxe->asXML($url); 
    Any ideas?

  2. #2
    Join Date
    Feb 2010
    Location
    Vancouver, Canada
    Posts
    24
    Oh, and I forgot to mention.

    PHP Code:
    var_dump($nodes); 
    This returns an array with SimpleXMLElement objects in it. So maybe the problem is that it's in an array?

Similar Threads

  1. XML file of DVD catalog - now what??
    By etcetera in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 07 Jan 2010, 02:50 PM
  2. Appending New XML To An XML File With PHP
    By Streakfury in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 27 Mar 2006, 02:21 PM

Tags for this Thread

Posting Permissions

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