Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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,
 
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..
The Following User Says Thank You to PeterPeiGuo For This Useful Post:
rangeshram (April 12th, 2010)




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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.