If you want to keep the XML in a specific order, you can use XML::Parser and treat the data as a stream. It looks like you pass your XML::Parser some callback functions to activate when it hits the start of a tag, the end of a tag, and non-markup text.
Most of the stuff I'd keep in XML doesn't need to maintain a strictly hierarchical tree, so the simplest (and smallest) parser is best for my purposes. If you can use a tied hash with XML::Simple::XMLin, that is another option. (It acts like a hash, but it keeps the order consistent.) | [reply] |
XML::Parser is definitely the more powerful of the two
options, but it's more complex to use. One way to use it
is to process the XML as a stream; you give the parser some
callback functions to use as handlers, and those
functions get called each time a specific "event" occurs
in the parser: the start of a tag, the end of a tag,
character (non-markup) data. XML::Parser also has "styles"
that make it easier to use (like a "Tree" style that loads
your data into a tree--although don't expect a nice hash
like XML::Simple gives you).
XML::Simple returns a hash reference, so couldn't
you write a recursive routine that descends through that
hash and does whatever validation you want on it?
There are a bunch of XML modules, and here's a really good
article about them: Processing XML with Perl.
From what you said, you may be best off just going with
XML::Simple and writing a sub to recurse through the hash.
| [reply] |
If I have read the question correctly, order is important to
you - ie, you need to preserve the order in which the tags are
read.
According to the documentation I have read, you are right.
XML::Simple will not do what you want but XML::Parser will.
Further, it appears XML::Parser can be used in Table mode
and will return an array much like what you appear to be
asking for - saving you the pain of dealing with the Stream
mode directly.
I found a good series of articles at Perl Month
and, more to your problem, this article
Mik
Mik Firestone ( perlus bigotus maximus ) | [reply] |
I think you mean Tree mode, not Table mode. (There's also
an "Object" mode that returns a tree of Perl objects, named
based on the element names.)
The only bad thing about Tree mode is that the resultant
data structure is rather difficult to understand, in my
opinion. It's certainly not going to give you a nice hash
to play around with. :) But if you feel like figuring
out how the tree is structured, that may be your best
option.
There are some other modules that use tree representations:
XML::Grove, XML::Twig, and XML::DOM. These are all discussed
in that article
I posted before.
| [reply] |
Dear Friends,
I have an XML file that’s encoded in ISO-8859-1. I have some European characters coming in from 2 fields (Name, Comments) in the XML file. Can anyone suggest if there are any functions in Perl to read those characters? Using Perl can I parse this xml file?
Please suggest. I have not done this before.
Regards,
Madhavi.
| [reply] |