Solved

Sorting IXMLDOMNodeList

Posted on 2008-11-03
4
1,539 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

762 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

20 Experts available now in Live!

Get 1:1 Help Now