Link to home
Start Free TrialLog in
Avatar of Chethan Bangera
Chethan Bangera

asked on

XML node text ascending order

Dears,

I am trying to sort the texts of one particular node (<Name>) of a XML file in ascending order using VB Scripting.

Say, I am using a XML file (xyz.xml) with content as shown below:

<?xml version="1.0" encoding="utf-8"?>
<!--Informations Vendor="Country S.A."-->
<India>
      <Statelist>
            <Name>2122</Name>
            <Name>1011</Name>
            <Name>Karnataka</Name>
            <Name>Goa</Name>
            <Name>Assam</Name>
      </Statelist>
</India>

The expected output is:

<?xml version="1.0" encoding="utf-8"?>
<!--Informations Vendor="Country S.A."-->
<India>
      <Statelist>
            <Name>1011</Name>
            <Name>2122</Name>
                <Name>Assam</Name>
            <Name>Goa</Name>
            <Name>Karnataka</Name>            
      </Statelist>
</India>

Can someone please help me here?

Best regards
Chethan Bangera
Avatar of Bill Prew
Bill Prew

What code do you have so far?


»bp
Avatar of Chethan Bangera

ASKER

I am inserting few more texts to <Name> node first. Since all the added texts append at the end, I want a sort script.

My current VB script code is:

Set xmlDoc = _
  CreateObject("Microsoft.XMLDOM")

xmlDoc.Async = "False"
xmlDoc.Load("\\Client\D$\Statelist\State.xml")


Set nNode = xmlDoc.selectsinglenode("//India/Statelist")
  

Set objFieldValue = _
  xmlDoc.createElement("Name")
objFieldValue.Text = "Kashmir"
nNode.appendChild objFieldValue



xmlDoc.Save "\\Client\D$\Statelist\State.xml" 

Set objNodeList = xmlDoc.getElementsByTagName("Name")

Msgbox objNodeList.length


For i = 0 To objNodeList.Length - 1 Step 2
' I am stuck here to sort the content
  Msgbox objNodeList(i).text
Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Provided solution is accepted.

Thanks a lot Mr. Bill Prew.