开发者

How can I make XML::Simple's output more human friendly?

开发者 https://www.devze.com 2022-12-10 18:53 出处:网络
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.

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.x开发者_开发技巧ml');

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

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


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.


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.


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.


# 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消