Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6891853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:27:44+00:00 2026-05-27T06:27:44+00:00

I have a legacy file format which I’m converting into XML for processing. The

  • 0

I have a legacy file format which I’m converting into XML for processing. The structure can be summarised as:

<A>
    <A01>X</A01>
    <A02>Y</A02>
    <A03>Z</A03>
</A>

The numerical part of the tags can go from 01 to 99 and there may be gaps. As part of the processing certain records may have additional tags added. After the processing is completed I’m converting the file back to the legacy format by iterwalking the tree. The files are reasonably large (~150,000 nodes).

A problem with this is that some software which uses the legacy format assumes that the tags (or rather fields by the time it’s converted) will be in alpha-numeric order but by default new tags will be added to the end of the branch which then causes them to come out of the iterator in the wrong order.

I can use xpath to find the preceeding sibling based on tag name each time I come to add a new tag but my question is whether there’s a simpler way to sort the tree at once just prior to export?

Edit:

I think I’ve over summarised the structure.

A record can contain several levels as described above to give something like:

<X>
    <X01>1</X01>
    <X02>2</X02>
    <X03>3</X03>
    <A>
        <A01>X</A01>
        <A02>Y</A02>
        <A03>Z</A03>
    </A>
    <B>
        <B01>Z</B02>
        <B02>X</B02>
        <B03>C</B03>
    </B>
</X>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:27:44+00:00Added an answer on May 27, 2026 at 6:27 am

    It’s possible to write a helper function to insert a new element in the correct place, but without knowing more about the structure it’s difficult to make it generic.

    Here’s a short example of sorting child elements across the whole document:

    from lxml import etree
    
    data = """<X>
        <X03>3</X03>
        <X02>2</X02>
        <A>
            <A02>Y</A02>
            <A01>X</A01>
            <A03>Z</A03>
        </A>
        <X01>1</X01>
        <B>
            <B01>Z</B01>
            <B02>X</B02>
            <B03>C</B03>
        </B>
    </X>"""
    
    doc = etree.XML(data,etree.XMLParser(remove_blank_text=True))
    
    for parent in doc.xpath('//*[./*]'): # Search for parent elements
      parent[:] = sorted(parent,key=lambda x: x.tag)
    
    print etree.tostring(doc,pretty_print=True)
    

    Yielding:

    <X>
      <A>
        <A01>X</A01>
        <A02>Y</A02>
        <A03>Z</A03>
      </A>
      <B>
        <B01>Z</B01>
        <B02>X</B02>
        <B03>C</B03>
      </B>
      <X01>1</X01>
      <X02>2</X02>
      <X03>3</X03>
    </X>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java-application that loads data from a legacy file format into an
I have a task at work that involves converting legacy SGM files into XML.
I have to read a binary file in a legacy format with Java. In
I have a legacy VB6 program which installs an Access file in a sub-directory
I have the following legacy file (can't edit): class Test { public $abc=1; }
I have a legacy DB dump file which starts off something like this: ^C^@&D
We have a legacy schema file which has the following column names, would like
I have some (legacy embedded c) code which produces a .csv file by means
I have a legacy VB6 application that uploads file attachments to a database BLOB
We have some legacy code that needs to identify in the Page_Load which event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.