Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 9th, 2010, 11:26 AM
Authorized User
Points: 145, Level: 2
Points: 145, Level: 2 Points: 145, Level: 2 Points: 145, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2008
Location: India
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Send a message via Yahoo to rangeshram
Default Perl XML::Twig

Dear All,

I am new to the PERL using XML::Twig, we need to navigate each reference element which is parent and compare both the <title> and <original_text> elements. While doing so in my xml there are multiple <title> tags present in the reference element. Thus our code produce wrong output for comparing each <title> text with <original_text> text.

I have tried the below code:
Perl-Twig.pl
Code:
use strict;
use XML::Twig;

my @arrTitle;
my @arrOriginalText;

my $t= XML::Twig->new( twig_roots   => { 'title' => \&title_ext, 'original_text' => \&org_ext   }
);

  $t->parsefile( 'Test.xml');

    sub title_ext 
    { my( $t, $elt)= @_;
	push(@arrTitle, $elt->text);
      $t->purge;           # frees the memory
    }

    sub org_ext 
    { my( $t, $elt)= @_;
	push(@arrOriginalText, $elt->text);
      $t->purge;           # frees the memory
    }
Our Input: Test.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<references>
<reference id="[1]">
    <book book_type="monograph">
      <book_metadata>
	<contributors>
		<person_name sequence="first" contributor_role="author">
			<given_name>L. I.</given_name>
			<surname>Rozonoer</surname>
			<suffix></suffix>
		</person_name>
		<organization sequence="first|additional" contributor_role="author|editor|chair|translator"></organization>
	</contributors>
	<titles>
		<title>Thermodynamics and Regulation of Biological Processes</title>
	</titles>
	<series_metadata>
		<titles>
			<title></title>
		</titles>
		<isbn></isbn>
		<doi_data>
			<doi></doi>
			<resource></resource>
		</doi_data>
		<comments>
			<comment></comment>
		</comments>
	</series_metadata>
      </book_metadata>
      <original_text>[11] L. I. Rozonoer, <i>Thermodynamics and Regulation of Biological Processes</i>, de Gryter, Berlin, 1984.</original_text>
    </book>
</reference>
<reference id="[2]">
    <journal>
      <journal_metadata>
	<full_title></full_title>
	<abbrev_title>Energie, Wirtschaftswachstum und technischer Fortschritt Phys. Bl.</abbrev_title>
	<issn></issn>
	<coden></coden>
      </journal_metadata>
      <journal_article>
	<titles>
		<title></title>
	</titles>
      </journal_article>
      <original_text>[1] R. K&#x00FC;mmel, D. Lindenberger, W. Eichborn, Energie, Wirtschaftswachstum und technischer Fortschritt, Phys. Bl., <b>53</b>, 869 (1997).</original_text>
    </journal>
</reference>
</references>

Is there any way to pass the argument as XPath like 'book/titles/title' and also for the instances 'Journal/titles/title'.

Kindly advise/help us on this.

Thanks,
Reply With Quote
  #2 (permalink)  
Old April 10th, 2010, 12:08 AM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

For what you are trying to do, you can simply use XML::Simple. use the below script to load your XML and dump its internal representation of it. This should help you understand how XML::Simple can be used, and make your decision - basically all you need to do is to iterate through that data strcture.
Code:
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

print Dumper(XMLin("foo.xml"));

Last edited by PeterPeiGuo; April 10th, 2010 at 12:12 AM..
Reply With Quote
The Following User Says Thank You to PeterPeiGuo For This Useful Post:
rangeshram (April 12th, 2010)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl help ZORCH Perl 3 April 25th, 2007 12:09 PM
perl vidura Perl 6 December 7th, 2006 06:50 AM
delete XML node in Perl crmpicco Perl 4 November 28th, 2006 12:12 PM
connecting to XML document with Perl script crmpicco XML 0 October 18th, 2006 12:00 PM
perl jeet Perl 1 March 6th, 2006 05:06 PM



All times are GMT -4. The time now is 11:06 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.