Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    66
    Thanks
    29
    Thanked 0 Times in 0 Posts

    help with parsing xml w/ perl and outputting as html

    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
    Code:
    #!/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
    Code:
    <?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>

  • #2
    Regular Coder
    Join Date
    Jun 2003
    Location
    Silicon Forest
    Posts
    155
    Thanks
    0
    Thanked 5 Times in 5 Posts
    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.
    Code:
    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.

    Code:
    #!/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";
    }
    Whats the point of a signature?

  • Users who have thanked CrzySdrs for this post:

    aaron4osu (10-29-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •