You will need to use a stream analyzer with appropriate callbacks, it will also improve the parsing speed (and reduce memory consumption if everything is done correctly) when it comes to larger datasets, which is a good / strong> awesome.
I recommend that you use XML::SAX , an introduction to the module is available at the following link:
Provide callbacks for start_element so you can read the value of each element one at a time.
Could you write me a simple example?
Yes, and I already have it! ; -)
In the fragment below, the provided OP data will be analyzed and the name of each element will be printed, as well as the key / value of the attributes.
This should be fairly easy to understand, but if you have any questions, feel free to add them as comments, and I will update this post with more details.
use warnings; use strict; use XML::SAX; my $parser = XML::SAX::ParserFactory->parser( Handler => ExampleHandler->new ); $parser->parse_string (<<EOT <ds> <uint32 name='a'/> <uint32 name='b'/> <string name='c'/> <int16 name='d'/> <uint32 name='e'/> </ds> EOT );
Output
found element: ds found element: uint32 'name' = 'a' found element: uint32 'name' = 'b' found element: string 'name' = 'c' found element: int16 'name' = 'd' found element: uint32 'name' = 'e'
I am not satisfied with XML :: SAX, are there any other modules?
Yes, there is a choice. Read the following list and choose the one that suits your specific problem:
What is the difference between different parsing methods?
I also recommend reading the following XML parsing FAQs. It will generate Pro and Con using a parser tree (e.g. XML :: Parser :: Simple) or a stream analyzer:
source share