Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: XML Transaction - random access.

by andreychek (Parson)
on Jun 29, 2001 at 22:41 UTC ( #92756=note: print w/replies, xml ) Need Help??

in reply to XML Transaction - random access.

If thats as big as it's going to get, you could always consider using XML::Simple to transform the XML data into a hash. The biggest problem with that would be in a case where your XML grew to be large -- you'd end up using a lot of RAM.

To use XML::Simple, you can just use code like this:
use XML::Simple; # Gets XML data from a file $hashref = XMLin('/path/to/somefile.xml'); or even: # Assumes you already have the XML data within a string $hashref = XMLin($XMLinaString);
And that would provide you with a nested hash containing all your data, properly structured (as opposed to flattened). From that point it wouldn't be too difficult to write code to randomly access the keys/data within.

And by all means, no one could ever go wrong using XML::Twig for the task if you were concerned about the size of you XML document :-)

Update: Added code example.

Replies are listed 'Best First'.
Re: Re: XML Transaction - random access.
by epoptai (Curate) on Jun 30, 2001 at 00:28 UTC
    Two notes on the use of XML::Simple:

    1. Since malformed XML is fatal to the parser you may want to wrap its evocation in an eval block.
    2. Use forcearray to avoid problems with references when the returned structure has empty elements.

    $hashref = eval{ XMLin($XMLinaString, forcearray => 1) }; xml_parse_failure($@) if $@; # an error trapping sub

    Check out my Perlmonks Related Scripts like framechat, reputer, and xNN.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://92756]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2017-04-27 02:46 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (498 votes). Check out past polls.