reakt Posted November 13, 2009 Share Posted November 13, 2009 Hi guys, this is my first time working with xml and the dom and I'm just not getting it. What I need to do is reorder some nodes within an xml document, based on the value of an attribute of that node (which happens to be a number). I figured I would load the said attribtues into an array, re-order the array numerically, and then loop through the array, grabbing each node and outputting it to a new xml file, in order. First off, is that a feasible way to approach the problem? It's the best I could think of with my limited knowledge of php and extremely limited knowledge of dom/xml. The basic structure of the xml file looks something like this: <Notice Id="125891" Sequence="6" Amount="175.06" Discount_Amount_1="17.16" Discounted_Amount_1="157.90"> </Notice> <Notice Id="199193" Sequence="7" Amount="175.06" Discount_Amount_1="17.16" Discounted_Amount_1="157.90"> </Notice> <Notice Id="356841" Sequence="8" Amount="175.06" Discount_Amount_1="17.16" Discounted_Amount_1="157.90"> </Notice> and I need to be able to sort by the Id number. If the approach I outlined above sounds ok, is anyone able to give me an idea on how to code it? Thanks Link to comment https://forums.phpfreaks.com/topic/181347-dom-and-xml/ Share on other sites More sharing options...
reakt Posted November 16, 2009 Author Share Posted November 16, 2009 OK I have for the moment given up on DOM and used XMLReader instead. I still can't figure out how to write and entire node to a file. Surely there is a way to do it without parsing each child of that node? Here is what I have so far: <?php $xml = new XMLReader(); $xml->open('sample.xml'); $noticeids = array(); while ($xml->read()) { if ($xml->name == 'Notice' AND $xml->nodeType == 1) { //apparently nodeType 1 is opening element, 15 is closing array_push($noticeids, $xml->getAttribute('Id')); } } $xml->close; sort($noticeids, SORT_NUMERIC); foreach ($noticeids as $id) { $xml = new XMLReader(); $xml->open('sample.xml'); while ($xml->read()) { if ($xml->name == 'Notice' AND $xml->nodeType == 1 AND $xml->getAttribute('Id') == $id) { //somehow write/append the entire node to a file } } $xml->close; } ?> Any help would be greatly appreciated. Thanks! Link to comment https://forums.phpfreaks.com/topic/181347-dom-and-xml/#findComment-958261 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.