What I have done is: read the file line by line skip empty line, comment line, muiltiline comment lines close file Is it enough?
I thought you said you needed to report some sort of statistic, too. ;^)

The preference of whether or not to use a parser would depend on how simple or complex the input data is -- and how consistent or variable it is. Even if you don't need to worry about XML syntax errors (really? are you sure about that?), I would think that if you don't do some sort of sanity checking on the data, your statistics may turn out to be (ahem) inaccurate in ways you might not expect...

You should just check the docs for one or another XML parser module on CPAN to see whether it's suitable for you, and to see how to use it.

And... what is it exactly that you are trying to count? Lines? Elements? Tags? Text contained within tags? Depending on your goals, a parsing module could make things a lot easier in the long run.

If you know the data well, and it's fairly simple and consistent, and your output statistics seem reasonable, then you probably are doing well enough with your current approach; if it ain't broke, don't fix it. (But it's hard for me to say for sure, since you don't show any of your perl code, or any real data, and you don't say whether you're having any problem with it.)


In reply to Re: How to count lines of code for XML file? by graff
in thread How to count lines of code for XML file? by sachaer

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.