reorder tags in an xml file

I am in a situation where I need to re-order the sequence of data in an xml file.

To make sure this is clear, we are selling products online and use an xml file to load data to our web system.   each item/product has a list of features, up to 10.  the first 3 are the main features, then others follow....Each feature has a 'sequence number' that is indicates the order of the features (not necessarily the order they appear in the xml file)  The issue I am facing, is this xml file is used for many systems...all of which are able to recognize this sequence number and order the features accordingly.  I am in a situation where I need to use this xml file for a new system, on a tight timeline and need to 'process' the xml file to re-order the features for each item so they appear in order in the xml file, and this new system will read them in, in the correct order.  Can anyone suggest a tool or method to reorder this data within the xml file?  Just looking for a short term fix.  One more point I should add.  it is highly preferred that I can do this in a somewhat automated day.  the goal is to upload this xml file daily (i can get by with doing it weekly for now) but would like it to be as automated as possible.  Thanks for any help!
xml-change.jpg
ohmetwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
You cannot do it directly on the XML, but you could put the xml data into an array, do the sort on the element you need and then I don't know if you actually need it back to xml or if you could just work on the array directly.
Sounds acceptable?
0
ohmetwAuthor Commented:
Ok and what is the best way to get XML into an array?
0
ohmetwAuthor Commented:
Another option would be to somehow edit the XML tags and get the sequence number value added to the feature_name tag name. Ex,  feature_name_1, feature_name_2. I could then map these uniquely named tags to the correct field in the application I am trying to load to
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

GaryCommented:
Basically...
$my_xml = simplexml_load_file('your_xml.xml');
$myarray = (array) $my_xml;
var_dump($myarray); // just to be sure it's correct

Open in new window

If you are already loading the xml into a string then just replace your_xml.xml with the string name.
I don't know if the sample you posted is real or not, but when you have the array post a sample here. I have to go now but the array sorting is explained here
http://php.net/manual/en/function.array-multisort.php
0
käµfm³d 👽Commented:
What programming language(s) do you have available to you in order to accomplish this?
0
GaryCommented:
There I go again with 'assumption is the mother of all...'
0
ohmetwAuthor Commented:
Kaufmed, I'm pretty much an expert in vba and oracle ....so....those are my personal skill sets....just not sure they'll help much for this....unless I were to push it to db tables and rework thru a procedure and download
0
käµfm³d 👽Commented:
It just so happens that I created a similar solution for a VB.NET question of the same nature earlier today. So with a little modification, and a borrowed sorting implementation, you can try:

set xdoc = CreateObject("MSXML2.DOMDocument")
xdoc.load("input.xml")

listToSort = Array()

set feature_list = xdoc.selectSingleNode("/PRODUCT/FEATURE_LIST")

If Not feature_list Is Nothing Then
    For i = feature_list.childNodes.length - 1 To 0 Step -1
        Set node = feature_list.childNodes(i)

        ReDim Preserve listToSort(UBound(listToSort) + 1)
        
        Set listToSort(UBound(listToSort)) = node

        feature_list.removeChild(node)
    Next

    xdoc.save "output.xml"
End If

Sort listToSort

For Each node In listToSort
    feature_list.appendchild node
Next

xdoc.save "output.xml"






' Modified from bubble sort implementation found at:
'  http://www.robvanderwoude.com/vbstech_data_array.php
Sub Sort( ByRef myArray )
    Dim i, j, strHolder

    For i = UBound(myArray) - 1 To 0 Step -1
        For j = 0 To i
            Set leftNode = myArray(j)
            Set rightNode = myArray(j + 1)
            Set leftSequenceNode = leftNode.selectSingleNode("FEATURE_SEQ_NO")
            Set rightSequenceNode = rightNode.selectSingleNode("FEATURE_SEQ_NO")
            
            If Not leftSequenceNode Is Nothing And Not rightSequenceNode Is Nothing Then
                leftText = leftSequenceNode.text
                rightText = rightSequenceNode.text
                leftInt = CInt(leftText)
                rightInt = CInt(rightText)
            
                If leftInt > rightInt Then
                    Set strHolder = myArray(j + 1)
                    Set myArray(j + 1) = myArray(j)
                    Set myArray(j) = strHolder
                End If
            End If
        Next
    Next 
End Sub

Open in new window


The script was tested in VBScript, but it should work in VBA as well. It's saving to a new file (output.xml), and there's not much error handling, but I think it will suffice for your needs. You can modify it as you see fit.

For your reference, the documentation for MSXML2.DOMDocument is here:  http://msdn.microsoft.com/en-us/library/ms760218(v=vs.85).aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.