Hello All,

This for the first time I am interacting with a XML file through Perl. As a matter of fact I am using any XML file the first time so I may go here & there with the technical terms of the XML file. I apologize for that.

Following is the structure of my XML file

<root> <start_element> <element_num>1</element_num> <child>MyChild</child> </start_element> <start_element> <element_num>2</element_num> <child>MyChild</child> <user_id>MyUser</user_id> </start_element> <start_element> <element_num>3</element_num> <child>MyChild</child> </start_element> </root>

As you can see the xml file starts with the root element followed by many elements having the tag – ‘start_element’. Some of the elements may have child elements with the tag – user_id, as shown in the case of 2nd element. The value of the child element <user_id> can be anything.

Now I want to split the original xml file into 2 xml files. First xml files will contain all the elements having child element - <user_id>. The second file will contain all the remaining elements. So for above example, I will need to generate 2 files.

First xml file will contain following :

<root> <start_element> <element_num>2</element_num> <child>MyChild</child> <user_id>MyUser</user_id> </start_element> </root>

Second xml file will contain remaining 2 elements:

<root> <start_element> <element_num>1</element_num> <child>MyChild</child> </start_element> <start_element> <element_num>3</element_num> <child>MyChild</child> </start_element> </root>

Currently I am planning to process the above requirement using simple Perl regex. But I feel it can be made simpler using any of the available modules.

So I have following questions:

1. Which are the best available XML modules for Perl?

2. Out of the best available modules, which one would suite my requirement in the best way?

3. Any pointers to specific methods of the XML modules to suffice my needs would be helpful.

TIA

Cheers,

Parag


In reply to Need to process a XML file through Perl by paragkalra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.