Hello! I'm trying to parse an XML file, sample of which is below.

<Aritcle> <Main> <Sect> <H4>Include</H4> ..... <P1> This is the criteria</P1> <L> <LI> <LI_Label>1.</LI_Label> <LI_Title>Critera 1</LI_Title> </LI> <LI> <LI_Label>2.</LI_Label> <LI_Title>Critera 2</LI_Title> </LI> <LI> <LI_Label>3.</LI_Label> <LI_Title>Critera 3</LI_Title> </LI> <LI> <LI_Label>4.</LI_Label> <LI_Title>Critera 3</LI_Title> </LI> </Sect> </Main> </Article>

I am using: XML::LibXML for parsing this. I can easily get the entire section of lists. However, my problem is I just need to know how many LIST elements are there. One way I was thinking is:

1. Parse the xml for the sec4. store contents in array

2. Loop through array until I get the first LIST element. Track counter;

3. Increment counter for each LIST element found until the last LIST element is reached.

While this approach may work, I feel this is very kludgy. So I am looking to see if there is an elegant way I can count the number of list elements in a section

The challenge ofcourse is that not every XML file I am parsing has the exact same structure. There could be variations where the <P1> This is the criteria</P1> may not exist before the start of the list.

Hoping someone here has some thoughts on how best I can capture the count of list elements

Sadly, I cannot use XPath here since I have an entire piece of code built around LibXML parsing. Also I'm having an Xpath installation nightmare which I just cannot get past.

Thanks so much in advance.

Regards,Madbee


In reply to XML parsing and Lists by madbee

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.