How to use perl to parsing a XML file to csv file which have the same sequence in xml file? The following XML file called booklist.xml, in example:
<book>
<author>Book 1 author 1</author>
<author>Book 1 author 2</author>
<title>Book 1 title</title>
<isbn>Book1ISBN</isbn>
</book>
Example of Converting from XML to a Perl hash record
#!/usr/bin/perl
# Script to illustrate how to parse a simple XML file
# and dump its contents in a Perl hash record.
use strict;
use XML::Simple;
use Data::Dumper;
my $booklist = XMLin('booklist.xml');
print Dumper($booklist);
Running this script outputs the following:
$VAR1 = {
'book' => {
'isbn' => 'Book1ISBN',
'title' => 'Book 1 title',
'author' => [
'Book 1 author 1',
'Book 1 author 2'
]
}
}
But hope we can get the sequence like this:
$VAR1 = {
'book' => {
'author' => [
'Book 1 author 1',
'Book 1 author 2'
]
'title' => 'Book 1 title',
'isbn' => 'Book1ISBN',
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.