...

View Full Version : help with parsing xml w/ perl and outputting as html



aaron4osu
10-28-2010, 02:15 PM
For an assignment I'm working on I need to parse a xml document with perl, then output it as a html document. At the point I'm currently stuck at I'm just trying to read in the xml document and just print the name element, followed by a tab, then the short_desc element. It is not reading the elements and just printing blank lines which makes me think the while loop is working, but just not the elements part. can someone tell me where I'm going wrong?

Here is what I have thus far.
perl file parser2.pl


#!/usr/bin/perl
# use module
use XML::Simple;

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

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

# access XML data
$i = 0;
while($i < 4)
{
print "$data->{NAME} \t $data->{SHORT_DESC}\n";
$i++;
}


xml file languages.xml


<?xml version="1.0" encoding="UTF-8"?>
<!-- <?xml-stylesheet type="text/xml" href="lab1StyleSheet.xsl"?> -->
<LANGUAGES>
<LANGUAGE>
<NAME>Java</NAME>
<SHORT_DESC>
Java is an imperative, object-oriented programming
language designed specifically for developing Web applications.
</SHORT_DESC>
<LONG_DESC> Java is an imperative, object-oriented programming language.
It provides an assortment of numeric data types as well as string and
Boolean data types. It allows you to define new classes. You can also
define new classes by inheriting the functionality of an existing class.
Begin and end block features help to structure code. Variables declared
within a block are visible only within the block.
</LONG_DESC>
<Year> 1995 </Year>
</LANGUAGE>

<LANGUAGE>
<NAME>JavaScript</NAME>
<SHORT_DESC>
JavaScript is an interpreted, with some object-oriented properties,
designed specifically for developing Web applications.
</SHORT_DESC>
<LONG_DESC>
JavaScript “can be characterized as a prototype-based object-oriented
scripting language that is dynamic, weakly typed and has first-class functions.
It is also considered a functional programming language…because it has
closures and supports higher-order functions” (Wikipedia, 2010). It uses a
variables and common data types, however they can be changed on the fly unlike Java.
</LONG_DESC>
<Year> 1995 </Year>
</LANGUAGE>
<LANGUAGE>
<NAME>Objective-C</NAME>
<SHORT_DESC>
Objective-C is a imperative, reflective, compiled, object-oriented
programming language.
</SHORT_DESC>
<LONG_DESC>
Objective-C is a imperative, reflective, compiled, object-oriented
programming language. It used many data types including strings, Boolean,
integers, doubles and more. Like java, it allows for the creation of classes,
but it does not use the same systems of inheritance. Objective-C requires
that the interface and implementation of a class are in separately declared
code blocks (Wikipedia, 2010).
</LONG_DESC>
<Year> 1986 </Year>
</LANGUAGE>

<LANGUAGE>
<NAME>HTML</NAME>
<SHORT_DESC> HTML is a markup language for specifying the Web page content. It doesn't
have looping control structure and data types.
</SHORT_DESC>
<LONG_DESC>
HTML is a markup language for specifying the Web page content.
It is not a programming language nor does it have looping control
structure and data types. It is not a Turing complete language.
It is not a Dynamic.
</LONG_DESC>
<Year> 1991 </Year>
</LANGUAGE>

<LANGUAGE>
<NAME>Perl</NAME>
<SHORT_DESC> Perl is a filler text more to come.
</SHORT_DESC>
<LONG_DESC>
Perl is a filler text more to come. filler text more to come.
filler text more to come.filler text more to come. filler text more to come.
</LONG_DESC>
<Year> 1991 </Year>
</LANGUAGE>
</LANGUAGES>

CrzySdrs
10-28-2010, 10:40 PM
If you have troubles understanding what the data structure is in an item, I would suggest you try this to see the entire data structure, this is particularly helpful with the xml.


use Data::Dumper;

print Dumper($data);

From seeing that, I was able to quickly fix your script to print out the data you were interested in.



#!/usr/bin/perl
use XML::Simple;

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

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

# access XML data
foreach my $lang (@{$data->{'LANGUAGE'}}) {
print "$lang->{NAME} \t $lang->{SHORT_DESC}\n";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum