madbee has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: XML parsing and Lists
by choroba (Cardinal) on Jul 04, 2013 at 23:38 UTC | |
by madbee (Acolyte) on Jul 05, 2013 at 00:23 UTC | |
by choroba (Cardinal) on Jul 05, 2013 at 00:50 UTC | |
by Anonymous Monk on Jul 05, 2013 at 01:30 UTC | |
by madbee (Acolyte) on Jul 05, 2013 at 04:28 UTC | |
by choroba (Cardinal) on Jul 05, 2013 at 08:41 UTC | |
by madbee (Acolyte) on Jul 05, 2013 at 01:56 UTC | |
by choroba (Cardinal) on Jul 05, 2013 at 02:00 UTC |