Solved

Sorting IXMLDOMNodeList

Posted on 2008-11-03
4
1,557 Views
Last Modified: 2009-03-24
I have an Nodelist that i want to read in to an Excelsheet, but first i want to sort the list by 1.ProjectId or Name, 2. by AssignmentID?
Dim AssignmentsNode As IXMLDOMNodeList

Dim AssignmentNode As IXMLDOMNode
 

Set oXMLDAssignments = New DOMDocument30

oXMLDAssignments.loadXML (XML)

        

If oXMLDAssignments.parseError.errorCode = 0 Then

   If Not oXMLDAssignments Is Nothing Then

     Set AssignmentsNode = oXMLDAssignments.selectNodes("//Assignment")

   End if

end if
 

How can i sort the assignmentsNode (IXMLDOMNodeList)  by "ProjectID" or "ProjecName" and "Assignment" before 
 
 

For Each AssignmentNode In AssignmentsNode

  Get assignmentdata 

Next
 

Result:

Project1

  Assignment1

  Assignment2...

Project2

  Assignment1

  Assignment2...

Open in new window

0
Comment
Question by:Torsleff-Experten
  • 2
4 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 22866094
Check this:
AssignmentsNode.SortRecordsByContent("Assignment", True)

AssignmentsNode.SortRecordsByContent("ProjectID", True)

Open in new window

0
 
LVL 27

Expert Comment

by:BigRat
ID: 22866098
There is no method in NodeList to sort the elements, and probably trying to write one will take a lot of time. THere is however a solution using XSL which produces the document sorted as you want. It runs like :-

XMLDoc.Load sXMLFile
XSLDoc.Load sortXSLFile
XMLDoc.loadXML(XMLDoc .transformNode(XSLDoc));

Hereafter you select the top level nodes you want.

sortXSLFile
----------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

 <xsl:template match="/toplevel">
  <xsl:copy>
   <xsl:apply-templates select="/leveltosort" />
    <xsl:sort select="sortColumn" data-type="number" order="ascending"/>
   </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

Here column to sort is numeric. See XSLT Sort element for further options.
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 22866629
Sorry, DOMDocument30 has no SortRecordsByContent() method.
0
 
LVL 2

Author Comment

by:Torsleff-Experten
ID: 22866700
hmm yes i came to that conclusion too, could i use a XSL stylesheet or some other way?

i have tried sorting the data after i put it to Excel, but im getting this error:

1004 Method 'Range' of object '_Global' failed
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now