Jquery MVC</t..."> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="../../css/main.css"> <link href="https://fonts.googleapis.com/css?family=Quicksand&display=swap" rel="stylesheet"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script>window.jQuery || document.write('<script src="../../js/vendors/jquery-3.3.1.min.js"><\/script>')</script> <script>document.write('<script src="//pagea' + 'd2.googles' + 'yndication.com/pagea' + 'd/js/a' + 'dsby' + 'google.js"><\/script>')</script> <script> var superSpecialObject = {}; superSpecialObject['google_a' + 'd_client'] = 'ca-p' + 'ub-4469282388984999'; superSpecialObject['enable_page_level_a' + 'ds'] = true; (window['a' + 'dsbygoogle'] = window['a' + 'dsbygoogle'] || []).push(superSpecialObject); </script> </head> <body> <!--[if lte IE 9]> <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="https://browsehappy.com/">upgrade your browser</a> to improve your experience and security.</p> <![endif]--> <header class="page-header js-page-header"> <a class="page-header-logo-container" href="https://geek-ask.imtqy.com/index.html"></a> <div class="page-header-text">Geek asks and answers</div> </header> <section class="page js-page"> <div itemscope="" itemtype="http://schema.org/QAPage" class="page"> <div class="main-col" itemprop="mainEntity" itemscope="" itemtype="http://schema.org/Question"> <div class="question-header"> <h1 itemprop="name"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">How to sort an XML file using the DOM</font></font></h1> </div> <div class="question"> <div class="question-row"> <div class="question-text"> <div class="description" itemprop="text"> <p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">I have an xml file structured as</font></font></p> <blockquote> <pre class="prettyprint-override"><code class="hljs xml"><span class="hljs-meta"><?xml version="1.0"?></span> <span class="hljs-tag"><<span class="hljs-name">library</span>></span> <span class="hljs-tag"><<span class="hljs-name">book</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"1003"</span>></span> <span class="hljs-tag"><<span class="hljs-name">title</span>></span>Jquery MVC<span class="hljs-tag"></<span class="hljs-name">title</span>></span> <span class="hljs-tag"><<span class="hljs-name">author</span>></span>Me<span class="hljs-tag"></<span class="hljs-name">author</span>></span> <span class="hljs-tag"><<span class="hljs-name">price</span>></span>500<span class="hljs-tag"></<span class="hljs-name">price</span>></span> <span class="hljs-tag"></<span class="hljs-name">book</span>></span> <span class="hljs-tag"><<span class="hljs-name">book</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"1001"</span>></span> <span class="hljs-tag"><<span class="hljs-name">title</span>></span>Php<span class="hljs-tag"></<span class="hljs-name">title</span>></span> <span class="hljs-tag"><<span class="hljs-name">author</span>></span>Me<span class="hljs-tag"></<span class="hljs-name">author</span>></span> <span class="hljs-tag"><<span class="hljs-name">price</span>></span>600<span class="hljs-tag"></<span class="hljs-name">price</span>></span> <span class="hljs-tag"></<span class="hljs-name">book</span>></span> <span class="hljs-tag"><<span class="hljs-name">book</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"1002"</span>></span> <span class="hljs-tag"><<span class="hljs-name">title</span>></span>Where to use IFrame<span class="hljs-tag"></<span class="hljs-name">title</span>></span> <span class="hljs-tag"><<span class="hljs-name">author</span>></span>Me<span class="hljs-tag"></<span class="hljs-name">author</span>></span> <span class="hljs-tag"><<span class="hljs-name">price</span>></span>300<span class="hljs-tag"></<span class="hljs-name">price</span>></span> <span class="hljs-tag"></<span class="hljs-name">book</span>></span> <span class="hljs-tag"></<span class="hljs-name">library</span>></span> </code></pre> </blockquote> <p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">To sort this xml according to book id,</font></font></p> <p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">after looking </font></font><a href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u="><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">overflow https://stackoverflow.com/a/318677/</font></font></a></p> <p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">i encoded this way</font></font></p> <pre class="prettyprint-override"><code class="hljs php">$dom = <span class="hljs-keyword">new</span> DOMDocument();<font></font> $dom->load(<span class="hljs-string">'DOM.xml'</span>);<font></font> $library = $dom->documentElement;<font></font> $xpath = <span class="hljs-keyword">new</span> DOMXPath($dom);<font></font> $result = $xpath->query(<span class="hljs-string">'/library/book'</span>); <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sort_trees</span><span class="hljs-params">($t1,$t2)</span></span>{ <span class="hljs-keyword">return</span> strcmp($t1[<span class="hljs-string">'id'</span>], $t2[<span class="hljs-string">'id'</span>]); <font></font> }<font></font> <font></font> usort($result, <span class="hljs-string">'sort_trees'</span>);<font></font> print_r($result);*/<font></font> </code></pre> <p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">But it gives me an error</font></font></p> <blockquote> <p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Warning: usort () expects parameter 1 to be an array, the object is listed in /var/www/html/testphp/phpxml/readxml.php on line 24</font></font></p> </blockquote> </div> <div class="favorite hidden"><i class="far fa-star"></i></div> <div class="votes-question"> <i class="glyphicon glyphicon-arrow-ups"></i> <div class="vote-count greenc" itemprop="upvoteCount"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> +3 </font></font></div> <i class="glyphicon glyphicon-arrow-down"></i> </div> <div class="tags"> <a href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" class="tag" title="show questions tagged 'sorting'" rel="tag"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">sorting</font></font></a> <a href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" class="tag" title="show questions tagged with 'xml'" rel="tag"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">xml</font></font></a> <a href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" class="tag" title="show questions tagged with 'php'" rel="tag"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">php</font></font></a> </div> <div class="clearfix"></div> <div class="action-time"> <span itemprop="author" itemscope="" itemtype="http://schema.org/Person"><span itemprop="name"><a href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" rel="noopener" target="_blank"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">user1066679</font></font></a></span></span> <time title="2012-05-10 05:59" itemprop="dateCreated" datetime="2012-05-10T05:59:02+0000"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> May 10 '12 at 5:59</font></font></time> </div> <a class="aa-link" href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" target="_blank" rel="noopener"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">source</font></font></a> <a class="s-link" href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" title="share"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">share</font></font></a> <div class="clearfix"></div> </div> </div> </div> <ins style="height: 90px;display: block;" data-zxname="zx-adnet" data-zxadslot="ZX-QRST" data-zxw="0" data-zxh="90" data-zxid="2" data-overlay="false"> </ins> <div class="answers " id="answers_hash"> <div class="answers-header"> <span class="pull-left"><span itemprop="answerCount"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">2</font></font></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> answers</font></font></span> <div class="clearfix"></div> </div> <div class="answer-pager hidden"> </div> <div class="answer accepted" id="5412535_hash" itemscope="" itemtype="http://schema.org/Answer" itemprop="acceptedAnswer"> <div class="answer-row"> <div class="answer-text"> <div class="desc" itemprop="text"> <p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">The answer you quote for SimpleXML but you are using DOMDocument.</font></font></p> <p><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">If you want to continue using DOMDocument, you need to consider its API.</font></font></p> <pre class="lang-php prettyprint-override"><code class="hljs php">$dom = <span class="hljs-keyword">new</span> DOMDocument();<font></font> $dom->load(<span class="hljs-string">'DOM.xml'</span>);<font></font> $xp = <span class="hljs-keyword">new</span> DOMXPath($dom);<font></font> <font></font> $booklist = $xp->query(<span class="hljs-string">'/library/book'</span>);<font></font> <font></font> <span class="hljs-comment">// Books is a DOMNodeList, not an array.</span> <span class="hljs-comment">// This is the reason for your usort() warning.</span><font></font> <font></font> <span class="hljs-comment">// Copies DOMNode elements in the DOMNodeList to an array.</span><font></font> $books = iterator_to_array($booklist);<font></font> <font></font> <span class="hljs-comment">// Second, your sorting function is using the wrong API</span> <span class="hljs-comment">// $node['id'] is SimpleXML syntax for attribute access.</span> <span class="hljs-comment">// DOMElement uses $node->getAttribute('id');</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sort_by_numeric_id_attr</span><span class="hljs-params">($a, $b)</span> </span>{ <span class="hljs-keyword">return</span> (int) $a->getAttribute(<span class="hljs-string">'id'</span>) - (int) $b->getAttribute(<span class="hljs-string">'id'</span>);<font></font> }<font></font> <font></font> <span class="hljs-comment">// Now usort()</span> usort($books, <span class="hljs-string">'sort_by_numeric_id_attr'</span>);<font></font> <font></font> <span class="hljs-comment">// verify:</span> <span class="hljs-keyword">foreach</span> ($books <span class="hljs-keyword">as</span> $book) { <span class="hljs-keyword">echo</span> $book->C14N(), <span class="hljs-string">"\n"</span>;<font></font> }<font></font> </code></pre> <p> , , , .</p> <pre class="lang-php prettyprint-override"><code class="hljs php">$newdoc = <span class="hljs-keyword">new</span> DOMDocument(<span class="hljs-string">'1.0'</span>, <span class="hljs-string">'UTF-8'</span>);<font></font> $libraries = $newdoc->appendChild($newdoc->importNode($dom->documentElement));<font></font> <span class="hljs-keyword">foreach</span> ($books <span class="hljs-keyword">as</span> $book) {<font></font> $libraries->appendChild($newdoc->importNode($book, <span class="hljs-keyword">true</span>));<font></font> }<font></font> <font></font> <span class="hljs-keyword">echo</span> $newdoc->saveXML(); </code></pre> <p> XSLT:</p> <pre class="lang-xml prettyprint-override"><code class="hljs"><span class="hljs-meta"><?xml version="1.0" encoding="UTF-8" ?></span> <span class="hljs-comment"><!-- file "sort_by_numeric_id.xsl" --></span> <span class="hljs-tag"><<span class="hljs-name">xsl:stylesheet</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"1.0"</span> <span class="hljs-attr">xmlns:xsl</span>=<span class="hljs-string">"http://www.w3.org/1999/XSL/Transform"</span>></span> <span class="hljs-tag"><<span class="hljs-name">xsl:output</span> <span class="hljs-attr">encoding</span>=<span class="hljs-string">"UTF-8"</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"xml"</span> /></span><font></font> <font></font> <span class="hljs-tag"><<span class="hljs-name">xsl:template</span> <span class="hljs-attr">match</span>=<span class="hljs-string">"node()|@*"</span>></span> <span class="hljs-tag"><<span class="hljs-name">xsl:copy</span>></span><span class="hljs-tag"><<span class="hljs-name">xsl:apply-templates</span> <span class="hljs-attr">select</span>=<span class="hljs-string">"node()|@*"</span>/></span><span class="hljs-tag"></<span class="hljs-name">xsl:copy</span>></span> <span class="hljs-tag"></<span class="hljs-name">xsl:template</span>></span><font></font> <font></font> <span class="hljs-tag"><<span class="hljs-name">xsl:template</span> <span class="hljs-attr">match</span>=<span class="hljs-string">"/*"</span>></span> <span class="hljs-tag"><<span class="hljs-name">xsl:copy</span>></span> <span class="hljs-tag"><<span class="hljs-name">xsl:apply-templates</span> <span class="hljs-attr">select</span>=<span class="hljs-string">"@*"</span>/></span> <span class="hljs-tag"><<span class="hljs-name">xsl:apply-templates</span> <span class="hljs-attr">select</span>=<span class="hljs-string">"*"</span>></span> <span class="hljs-tag"><<span class="hljs-name">xsl:sort</span> <span class="hljs-attr">select</span>=<span class="hljs-string">"@id"</span> <span class="hljs-attr">data-type</span>=<span class="hljs-string">"number"</span>/></span> <span class="hljs-tag"></<span class="hljs-name">xsl:apply-templates</span>></span> <span class="hljs-tag"></<span class="hljs-name">xsl:copy</span>></span> <span class="hljs-tag"></<span class="hljs-name">xsl:template</span>></span><font></font> <font></font> <span class="hljs-tag"></<span class="hljs-name">xsl:stylesheet</span>></span> </code></pre> <p> XSLTProcessor ( <code>xsltproc</code> ):</p> <pre class="lang-php prettyprint-override"><code class="hljs php">$xsltdoc = <span class="hljs-keyword">new</span> DOMDocument();<font></font> $xsltdoc->load(<span class="hljs-string">'sort_by_numeric_id.xsl'</span>);<font></font> <font></font> $xslt = <span class="hljs-keyword">new</span> XSLTProcessor();<font></font> $xslt->importStyleSheet($xsltdoc);<font></font> <font></font> <span class="hljs-comment">// You can now use $xslt->transformTo*() methods over and over on whatever documents you want</span><font></font> <font></font> $libraryfiles = <span class="hljs-keyword">array</span>(<span class="hljs-string">'library1.xml'</span>, <span class="hljs-string">'library2.xml'</span>);<font></font> <font></font> <span class="hljs-keyword">foreach</span> ($libraryfiles <span class="hljs-keyword">as</span> $lf) {<font></font> $doc = <span class="hljs-keyword">new</span> DOMDocument();<font></font> $doc->load($lf);<font></font> <font></font> <span class="hljs-comment">// write the new document</span> $xslt->transformToUri($doc, <span class="hljs-string">'file://'</span>.preg_replace(<span class="hljs-string">'/(\.[^.]+)?$/'</span>, <span class="hljs-string">'-sorted$0'</span>, $lf, <span class="hljs-number">1</span>);<font></font> <font></font> <span class="hljs-keyword">unset</span>($doc); <span class="hljs-comment">// just to save memory</span><font></font> }<font></font> </code></pre> </div> <div class="votes-answer"> <i class="glyphicon glyphicon-arrow-ups"></i> <div class="vote-count greenc" itemprop="upvoteCount"> +10 </div> <i class="glyphicon glyphicon-arrow-down"></i> </div> <div class="clearfix"></div> <div class="action-time"> <span itemprop="author" itemscope="" itemtype="http://schema.org/Person"><span itemprop="name"><a href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" rel="noopener" target="_blank">Francis Avila</a></span></span> <time title="2012-05-10 06:21" itemprop="dateCreated" datetime="2012-05-10T06:21:23+0000"> 10 '12 6:21</time> </div> <a class="aa-link" href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" target="_blank" rel="noopener"></a> <a itemprop="url" class="s-link" href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" title="share"></a> <div class="clearfix"></div> </div> </div> </div> <ins style="height: 90px;display: block;margin-top: 15px;" data-zxname="zx-adnet" data-zxadslot="ZX-QRST" data-zxw="0" data-zxh="90" data-zxid="3" data-overlay="false"> </ins> <div class="answer last rltd" id="5412536_hash" itemscope="" itemtype="http://schema.org/Answer" itemprop="suggestedAnswer"> <div class="answer-row"> <div class="answer-text"> <div class="desc" itemprop="text"> <p>( ) </p> <pre class="prettyprint-override"><code class="hljs php">$dom = <span class="hljs-keyword">new</span> DOMDocument();<font></font> $dom->load(<span class="hljs-string">'dom.xml'</span>);<font></font> $xp = <span class="hljs-keyword">new</span> DOMXPath($dom);<font></font> <font></font> $booklist = $xp->query(<span class="hljs-string">'/library/book'</span>);<font></font> $books = iterator_to_array($booklist);<font></font> <font></font> <font></font> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sort_by_numeric_id_attr</span><span class="hljs-params">($a, $b)</span> </span>{ <span class="hljs-keyword">return</span> (int) $a->getAttribute(<span class="hljs-string">'id'</span>) - (int) $b->getAttribute(<span class="hljs-string">'id'</span>);<font></font> }<font></font> <font></font> <font></font> usort($books, <span class="hljs-string">'sort_by_numeric_id_attr'</span>);<font></font> <font></font> $newdom = <span class="hljs-keyword">new</span> DOMDocument(<span class="hljs-string">"1.0"</span>);<font></font> $newdom->formatOutput = <span class="hljs-keyword">true</span>;<font></font> $root = $newdom->createElement(<span class="hljs-string">"library"</span>);<font></font> $newdom->appendChild($root);<font></font> <span class="hljs-keyword">foreach</span> ($books <span class="hljs-keyword">as</span> $b) {<font></font> $node = $newdom->importNode($b,<span class="hljs-keyword">true</span>);<font></font> $root->appendChild($newdom->importNode($b,<span class="hljs-keyword">true</span>));<font></font> }<font></font> <font></font> $newdom->save(<span class="hljs-string">'DOM2.xml'</span>); </code></pre> <p> , DOMDocument ( importNode DOMNodes DOMDocument ).</p> </div> <div class="votes-answer"> <i class="glyphicon glyphicon-arrow-ups"></i> <div class="vote-count " itemprop="upvoteCount"> 0 </div> <i class="glyphicon glyphicon-arrow-down"></i> </div> <div class="clearfix"></div> <div class="action-time"> <span itemprop="author" itemscope="" itemtype="http://schema.org/Person"><span itemprop="name"><a href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" rel="noopener" target="_blank">msgmash.com</a></span></span> <time title="2012-05-10 10:53" itemprop="dateCreated" datetime="2012-05-10T10:53:17+0000"> 10 '12 10:53</time> </div> <a class="aa-link" href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" target="_blank" rel="noopener"></a> <a itemprop="url" class="s-link" href="https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=" title="share"></a> <div class="clearfix"></div> </div> </div> </div> <div class="answer-pager hidden"> </div> </div> </div> </div> <section class="more-articles-navigation-panel js-more-articles-navigation-panel"> <h4>More articles:</h4> <nav class="list-of-articles-container js-list-of-articles-container"><ul class="list-of-pages js-list-of-pages"> <li><a href="../1908843/index.html">Using inotify to track all files on a system - database</a></li> <li><a href="../1908844/index.html">Inotifywait for large directories - callback</a></li> <li><a href="../1908845/index.html">Sorting a list of objects based on different data elements in java - java</a></li> <li><a href="../1908846/index.html">errors installing a package on a carrier wave - ruby ​​| fooobar.com</a></li> <li><a href="../1908847/index.html">editing some video frames using GPUImage - iphone</a></li> <li><a href="../1908849/index.html">Is embeddable audio embedded? - facebook</a></li> <li><a href="../1908850/index.html">Debugging combinational logic loops in Icarus Verilog - verilog</a></li> <li><a href="../1908851/index.html">Параметр Namespacing приводит к тому, что функция JSLint использовалась до того, как она определила ошибку - javascript</a></li> <li><a href="../1908852/index.html">Regular expression for InputFilter for EditText in Android - android</a></li> <li><a href="../1908853/index.html">Avoiding black screen when resuming activity in AndEngine - android</a></li> </ul></nav> </section><br /> <a href="../../allArticles.html"><strong>All Articles</strong></a> <script src="../../js/main.js"></script> <!-- Yandex.Metrika counter --> <script type="text/javascript" > (function (d, w, c) { (w[c] = w[c] || []).push(function() { try { w.yaCounter62683636 = new Ya.Metrika({ id:62683636, clickmap:true, trackLinks:true, accurateTrackBounce:true, webvisor:true }); } catch(e) { } }); var n = d.getElementsByTagName("script")[0], s = d.createElement("script"), f = function () { n.parentNode.insertBefore(s, n); }; s.type = "text/javascript"; s.async = true; s.src = "https://mc.yandex.ru/metrika/watch.js"; if (w.opera == "[object Opera]") { d.addEventListener("DOMContentLoaded", f, false); } else { f(); } })(document, window, "yandex_metrika_callbacks"); </script> <noscript><div><img src="https://mc.yandex.ru/watch/62683636" style="position:absolute; left:-9999px;" alt="" /></div></noscript> <!-- Google Analytics --> <script> window.ga = function () { ga.q.push(arguments) }; ga.q = []; ga.l = +new Date; ga('create', 'UA-166339405-1', 'auto'); ga('send', 'pageview') </script> <script src="https://www.google-analytics.com/analytics.js" async defer></script> </section> <footer class="page-footer"> <div class="page-footer-legal-info-container page-footer-element"> <p> Geek-Ask | <span class="page-footer-legal-info-year js-page-footer-legal-info-year">2020</span> </p> </div> <div class="page-footer-counters-container page-footer-element"> <a class="page-footer-counter-clustrmap" href='#' title='Visit tracker'><img src='https://clustrmaps.com/map_v2.png?cl=698e5a&w=271&t=t&d=-MuxZ73bDptjanSSx-nO3v5W1kSskzmZpKtFpxY6-yI&co=3a3a3a&ct=ffffff'/></a> </div> </footer> </body> </html>