tags:

views:

190

answers:

4

I am using the XML::Simple module to parse an XML file. When I run the following script then I do not get data in human readable form and so it is difficult to see the output of the parsed XML file.

Code:

    #!usr/bin/perl -w

    use XML::Simple;
    my $ref = XMLin('SampleXML.xml');

    use Data::Dumper;
    print Dumper($ref);

Is there a way by which we can get parsed output in some readable format?

+4  A: 

What is unreadable about Data::Dumper? How would you define "human readable"? The original XML was the "human-readable" format. :)

Perhaps if you explained what you intend to do with the XML after it has been read in and parsed, we can help you with that step.

Ether
Good point about the original XML.
Daniel S
+3  A: 

By human-readable, I'm guessing you mean "pretty printed" (e.g. new lines for each tag, indentation and son on). You could try XMLPrettyPrint.

Jeff Foster
A: 
# remember to parse with KeepRoot
print XMLout($ref, (AttrIndent => 1,  KeepRoot => 1));

I don't know why you want to do this, but to just answer your question, this above is another way to print the XML back in human readable form.

Murali VP
Actually I want to store it into Database and so wanted to see if all attributes in XML are parsed properly allowing me to insert into appropriate database columns in our schema.
Rachel
So I guess your question really is "how can I check if my XML was parsed correctly?"
Ether
XML::LibXML allows you to validate the XML against an XML schema is it is read. That is what you want to do.
jrockway
Yes. "How can I check if my XML was parsed Correctly ?"
Rachel
+3  A: 

The definition of "human readable" really depends on which human you are talking about. Data::Dumper is OK for relatively small data structures that must be interpreted by a Perl programmer. Not so good if you are using a deeply nested structure or asking a receptionist to read the data.

YAML provides a more condensed format for dumping data structures and it is reasonably easy to read. It was originally developed by people working on a more compact version of Data::Dumper called Data::Denter.

If you need to work with huge structures or provide data to a non-programmer, you are better off building a custom format that is easy to read and hides complexity. In these cases you want to automate checks and summarize as much as possible. People suck at reviewing big lists of data. If this is the case, then you will need to design your own format that meets the needs of your particular data set and intended audience.

daotoad