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 6967025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:16:04+00:00 2026-05-27T16:16:04+00:00

I have many XML files in a directory that need to sorted and merged

  • 0

I have many XML files in a directory that need to sorted and merged into one file. The files are formatted as follows:

File1.xml:

<?xml version="1.0" encoding="utf-8"?>
<doctypea>
  <header someattr="1">
    <docnumber>111</docnumber>
  </header>
</doctypea>

File2.xml:

<?xml version="1.0" encoding="utf-8"?>
<doctypea>
  <header someattr="1">
    <docnumber>112</docnumber>
  </header>
</doctypea>

File3.xml:

<?xml version="1.0" encoding="utf-8"?>
<doctypeb>
  <header someattr="1">
    <docnumber>111</docnumber>
  </header>
</doctypeb>

File4.xml:

<?xml version="1.0" encoding="utf-8"?>
<doctypeb>
  <header someattr="1">
    <docnumber>112</docnumber>
  </header>
</doctypeb>

All the files in this directory need to be sorted on the following criteria:

  1. documentnumber
  2. doctype (a or b)

Then they need to be merged, so the output file should look like:

<?xml version="1.0" encoding="utf-8"?>
<doctypea>
  <header someattr="1">
    <docnumber>111</docnumber>
  </header>
</doctypea>
<doctypeb>
  <header someattr="1">
    <docnumber>111</docnumber>
  </header>
</doctypeb>
<doctypea>
  <header someattr="1">
    <docnumber>112</docnumber>
  </header>
</doctypea>
<doctypeb>
  <header someattr="1">
    <docnumber>112</docnumber>
  </header>
</doctypeb>

In order to accomplish this I am trying to use XML:Twig in Perl. I have the following code so far:

use XML::Twig;

my $xmldir = "/xmlfiles";
my $parser = XML::Twig->new(pretty_print => 'indented');

opendir(DIR, "$xmldir");
my @FILES= readdir(DIR);
closedir(DIR);

foreach (@FILES) {
        if ($_ ne "." && $_ ne "..") {
                print "reading file: $xmldir/$_\n";
                $parser->parsefile("$xmldir/$_");
        }
}

At this point I cannot seem to figure out the correct syntax to get the elements I want from the parser.

1. How do I get the value of the root element (“doctypea” or “doctypeb”)?

2. I assume I need that (1) in order to parsenode down to the docnumber field?

My plan then is to build some kind of has with doctype%number in order to sort, I am not sure the easiest way to merge them with that.

Appreciate any advise!

  • 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-27T16:16:05+00:00Added an answer on May 27, 2026 at 4:16 pm

    Please find below small example that should get you started. It shows how to get data from XML file similar to yours (I fixed the tags to match and quoted someattr value to get valid XML). You can use similar approach to gather data you need and produce output.

    use XML::Twig;
    
    XML::Twig->new(twig_handlers => {
        '/*'        => sub { print $_->gi;           },     # doctypea
        'docnumber' => sub { print $_->trimmed_text; },     # 111
    })->parse(\*DATA);    # use parsefile('xxx.xml') to parse a file
    
    __DATA__
    <?xml version="1.0" encoding="utf-8"?>
    <doctypea>
      <header someattr="1">
        <docnumber>111</docnumber>
      </header>
    </doctypea>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many, (15-20) different XML files that I need to load to VB.Net.
I have a large XML file (many MBs) that I cannot afford to download
I have an XML file which has many section like the one below: <Operations>
I have many many XML files that often contain nodes mutiple times (each time
Let's say I have to read from a directory that has many large XML
I wanted to put my log4j.xml file in WEB-INF/conf directory where I have many
I have a xml that has so many elements and most of that contain
I have an application that stores large amount of files (XML and binary) in
I have many xml files in my project. I want to find out R.id.details
I have user_preferences.xml in my XML directory. A PreferencesActivity uses this file to create

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.