Currently I am working on a project that will handle several XML files from different sources with different formats. I am trying to handle all of them with a single piece of code but it is hard because nodes are different, depth is different etc.

I don't see how that is possible. If you have one XML file that has a tag called <super_duper_product> nested inside one other tag, and another XML file that has a tag called <item89001> nested inside three tags, I don't think there is any way you can use the same script to extract both tags. There has got to be some pattern you can exploit, either the tags have similar names, or they have similar locations in the document tree, or they have identical siblings or child elements, or similar text. Something.

As the first responders noted, XPath makes it easy to find a specific tag name anywhere in the document. XPath lets you treat an XML file as if it were a directory on your file system. You locate elements using path notation: /bookstore/book/title. XPath conveniently lets you omit the first 'directory', like this:

findnodes("//<book>");

which searches for all <book> tags anywhere in the document. The LibXML module provides that findnodes method which allows you to specify an XPath for the search.


In reply to Re: The best way to handle different type of XML files by 7stud
in thread The best way to handle different type of XML files by mahira

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.