Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Problem with Pseudohash

by uday_sagar (Scribe)
on Feb 02, 2012 at 06:24 UTC ( #951366=perlquestion: print w/ replies, xml ) Need Help??
uday_sagar has asked for the wisdom of the Perl Monks concerning the following question:


I am having an XML document which I am parsing using XML::Simple module. Here is my xml document which is shown in 2 different styles: 1)In such a way that pseudohashh is created 2)no pseudohash


<booklist> <book type="technical"> <author>Book 1 author 1</author> <author>Book 1 author 2</author> <title>Book 1 title</title> <isbn>Book1ISBN</isbn> </book> </booklist>


<booklist> <book > <author>Book 1 author 1</author> <author>Book 1 author 2</author> <title>Book 1 title</title> <isbn>Book1ISBN</isbn> type="technical" </book> </booklist>

Just the position of attribute, type="technical" is changed. (thats it!)

Here is my perl code:

use strict; use XML::Simple; use Data::Dumper; my $booklist = XMLin('book.xml', ForceArray => 1); # print Dumper($booklist); foreach my $book (@{$booklist->{book}}) { print $book->{type}."\n"; }

When I give STYLE 1 to perl, its fine and displaying "technical". But this is not happening with STYLE 2. My original XML file has its contents similar to STYLE 2

What to do?


Uday Sagar.

Comment on Problem with Pseudohash
Select or Download Code
Replies are listed 'Best First'.
Re: Problem with Pseudohash
by choroba (Chancellor) on Feb 02, 2012 at 08:49 UTC
    In the 2nd style, the part type="technical" is not an attribute, it is just a text. You can find it in $book->{content}, including the newline before it.
    Update: Where do you see any pseudohash?
      if the content has type1="scientific" type2="technical" instead of type="technical", how to get only type1="sceintific"?
        OK, you have a string
        my $content = "\n" . 'type1="scientific" type2="technical"';
        Are you interested in type1 only? Use
        my ($type1) = $content =~ /type1="([^"]+)"/;
        Are you interested whether scientific is present? Use
        my $is_sceintific = $content =~ /type[0-9]*="scientific"/;
Re: Problem with Pseudohash
by Anonymous Monk on Feb 02, 2012 at 08:59 UTC
    Pseudohashes aren't involved in any way shape or form

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://951366]
Approved by philipbailey
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2016-05-04 14:36 GMT
Find Nodes?
    Voting Booth?