Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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 making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-12-16 12:24 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (450 votes). Check out past polls.