Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieve portions of XML data via perl and output to text file

Status
Not open for further replies.

GZPM

IS-IT--Management
Jan 20, 2006
18
US
Hi there,

I'm new to XML and having a hard time with below.

here is my original XML data:

<productidentifier>
<b221>02</b221>
<b244>0123456789</b244>
</productidentifier>
<productidentifier>
<b221>03</b221>
<b244>9780123456789</b244>
</productidentifier>
<othertext>
<d102>02</d102>
<d103>02</d103>
<d104><![CDATA[<div>book synopsis. <br></div>]]></d104>
</othertext>
<othertext>
<d102>04</d102>
<d103>02</d103>
<d104><![CDATA[<div>Table of Contents. <br></div>]]></d104>
</othertext>
<othertext>
<d102>08</d102>
<d103>02</d103>
<d104><![CDATA[<div>``book review blah blah blah.'--Jane Doe, Ph.D.,<br>University of blah blah blah <br></div>]]></d104>
</othertext>
<othertext>
<d102>08</d102>
<d103>02</d103>
<d104><![CDATA[<div>"book review blah blah blah' --<i>Joe Journal</i> <br></div>]]></d104>
</othertext>
<othertext>
<d102>13</d102>
<d103>02</d103>
<d104><![CDATA[<div><div>Author biography blah blah blah.</div></div>]]></d104>
</othertext>

Now here's the output I get back with below code:
Code:
use strict;
use XML::Simple;
use Data::Dumper;

#create object
our $xml = new XML::Simple;

#open output file
open OUTPUT, ">xml_output.txt";

#read XML file
our $data = $xml->XMLin("xml_test_file.xml");

#dereference hash ref
print OUTPUT Dumper($data);
print Dumper($data);
close OUTPUT;

========================
OUTPUT
========================

'othertext' => [
{
'd103' => '02',
'd102' => '02',
'd104' => '<div>book synopsis. <br></div>'
},
{
'd103' => '02',
'd102' => '04',
'd104' => '<div>Table of Contents. <br></div>'
},
{
'd103' => '02',
'd102' => '08',
'd104' => '<div>``book review blah blah blah.'--Jane Doe, Ph.D.,<br>University of blah blah blah <br></div>'
},
{
'd103' => '02',
'd102' => '08',
'd104' => '<div>"book review blah blah blah' --<i>Joe Journal</i> <br></div>'
},
{
'd103' => '02',
'd102' => '13',
'd104' => '<div><div>Author biography blah blah blah.</div></div>'
}
],
'productidentifier' => [
{
'b221' => '02',
'b244' => '0898624347'
},
{
'b221' => '03',
'b244' => '9780898624342'
}
];

==============================================

How would I edit above code so as to get only the following values in my output?

9780123456789

<d102>02</d102>
book synopsis. <br>

<d102>04</d102>
Table of Contents. <br>

<d102>08</d102>
``book review blah blah blah.'--Jane Doe, Ph.D.,<br>University of blah blah blah <br>

<d102>08</d102>
"book review blah blah blah' --<i>Joe Journal</i> <br>

<d102>13</d102>
Author biography blah blah blah.

thanks.
 
I am reading through and trying out the perl forum post right before mine and will post back my results (if any). In the meantime, please send whatever suggestions you may have.

thx
 
ok...i'm officially lost.
 
I came up with this code, but it only gives me the first product record. How would i edit it to get all of the records?

Code:
#!usr/bin/perl

use strict;
use XML::Simple;

our $file = 'test_file.xml';
our $xml = XML::Simple->new();

our $data = $xml->XMLin($file);

foreach my $product(@{$data->{'product'}}) {
	my @eans =  @{$product->{'productidentifier'}};
	my @texts = @{$product->{'othertext'}};
	my @records = (@eans, @texts);
	foreach my $record(@records) {
		print "$record->{'b244'}\n";
		print "$record->{'d104'}\n";
	}
}

Here's the warning and output of my script:

Not an ARRAY reference at test_file.plx line 13.
0898624347

9780898624342

book synopsis. <br>

Table of Contents. <br>

``book review blah blah blah.'--Jane Doe, Ph.D.,<br>University of blah blah blah <br>

"book review blah blah blah' --<i>Joe Journal</i> <br>

Author biography blah blah blah.



Any help would be greatly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top