Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7910221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:47:17+00:00 2026-06-03T12:47:17+00:00

I have a xml file structured like <?xml version=1.0?> <library> <book id=1003> <title>Jquery MVC</title>

  • 0

I have a xml file structured like

<?xml version="1.0"?>
 <library>
<book id="1003">
    <title>Jquery MVC</title>
    <author>Me</author>
    <price>500</price>
</book>
<book id="1001">
    <title>Php</title>
    <author>Me</author>
    <price>600</price>
</book>
<book id="1002">
    <title>Where to use IFrame</title>
    <author>Me</author>
    <price>300</price>
</book>
</library>

In order to sort this xml according to the book id,

after reviewing this method from stackoverflow

i coded like this

$dom = new DOMDocument();
$dom->load('DOM.xml');
$library = $dom->documentElement;
$xpath = new DOMXPath($dom);
$result = $xpath->query('/library/book');
function sort_trees($t1,$t2){
    return strcmp($t1['id'], $t2['id']);    
}

usort($result, 'sort_trees');
print_r($result);*/

But it gives me an error

Warning: usort() expects parameter 1 to be array, object given in /var/www/html/testphp/phpxml/readxml.php on line 24

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:47:18+00:00Added an answer on June 3, 2026 at 12:47 pm

    The answer you cite is for SimpleXML, but you are using DOMDocument.

    If you want to continue to use DOMDocument you need to keep its API in mind.

    $dom = new DOMDocument();
    $dom->load('DOM.xml');
    $xp = new DOMXPath($dom);
    
    $booklist = $xp->query('/library/book');
    
    // Books is a DOMNodeList, not an array.
    // This is the reason for your usort() warning.
    
    // Copies DOMNode elements in the DOMNodeList to an array.
    $books = iterator_to_array($booklist);
    
    // Second, your sorting function is using the wrong API
    // $node['id'] is SimpleXML syntax for attribute access.
    // DOMElement uses $node->getAttribute('id');
    function sort_by_numeric_id_attr($a, $b)
    {
        return (int) $a->getAttribute('id') - (int) $b->getAttribute('id');
    }
    
    // Now usort()
    usort($books, 'sort_by_numeric_id_attr');
    
    // verify:
    foreach ($books as $book) {
        echo $book->C14N(), "\n";
    }
    

    If you need to create a new output document with the nodes sorted, create a new document, import the root element, then import the book nodes in sorted order and add to the document.

    $newdoc = new DOMDocument('1.0', 'UTF-8');
    $libraries = $newdoc->appendChild($newdoc->importNode($dom->documentElement));
    foreach ($books as $book) {
        $libraries->appendChild($newdoc->importNode($book, true));
    }
    
    echo $newdoc->saveXML();
    

    However, a much better approach is to use XSLT:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!-- file "sort_by_numeric_id.xsl" -->
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" method="xml" />
    
    <xsl:template match="node()|@*">
        <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
    </xsl:template>
    
    <xsl:template match="/*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="*">
                <xsl:sort select="@id" data-type="number"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Then use XSLTProcessor (or xsltproc from the command line):

    $xsltdoc = new DOMDocument();
    $xsltdoc->load('sort_by_numeric_id.xsl');
    
    $xslt = new XSLTProcessor();
    $xslt->importStyleSheet($xsltdoc);
    
    // You can now use $xslt->transformTo*() methods over and over on whatever documents you want
    
    $libraryfiles = array('library1.xml', 'library2.xml');
    
    foreach ($libraryfiles as $lf) {
        $doc = new DOMDocument();
        $doc->load($lf);
    
        // write the new document
        $xslt->transformToUri($doc, 'file://'.preg_replace('/(\.[^.]+)?$/', '-sorted$0', $lf, 1);
    
        unset($doc); // just to save memory
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
I have an XML file that's structured like this: <foo> <bar></bar> <bar></bar> ... </foo>
I have an XML file that looks like this: <?xml version=1.0? encoding=UTF-8 standalone=no?> <dir
I have a plist structured like this: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD
I have the following XML file: <xml version=1.0 encoding=utf-8?> <Data> <Parameter1>1</Parameter1> </Data> I want
I have an XML file with this structure: <?xml version=1.0 encoding=utf-8 ?> <Photobank> <Landowner
I have a trouble with parsing an XML File called test.xml like this :
I have a xml file roughly like this: <batch> <header> <headerStuff /> </header> <contents>
I have an xml file that contains in products. File's structure this: <?xml version=1.0
I have a XML file template like this that comes from some IC chips,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.