Perl Twig help

I am using twig in perl to create a new XML using an existing XML as input. I have an element email id which I need to update based on a hash of account number and email ids. How do I traverse the XML tree to get the account number and do a match on the hash and update the email.

XML:

<?xml version="1.0" encoding="UTF-8"?>


<SCMATRoot
 xmlns:Notices="http://www.fiserv.com/schemas/Notices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>

<SCMAT xmlns="http://www.fiserv.com/schemas/Notices"

>

<MemberInformation

>
<AccountNumber>12345</AccountNumber>
<AddressLine1></AddressLine1>
<City></City>
<EmailAddress>enterpriseapplications@g1.com</EmailAddress>
<PrimaryName></PrimaryName>
<IsBadAddress>false</IsBadAddress>
<IsENotice>false</IsENotice>
<IsForeignAddress>false</IsForeignAddress>
<IsNoMail>false</IsNoMail>
<MaskedAccountNumber></MaskedAccountNumber>
<PostalCode></PostalCode>
<State></State>
<Suffix></Suffix>
<SuffixType></SuffixType>
</MemberInformation>

My Code:

$m=XML::Twig->new( twig_handlers => 					   
								   'SCMAT/MemberInformation/AccountNumber' => sub { $_->set_tag( 'DR4_AccountNumber')},
								   'SCMAT/MemberInformation/AddressLine1' => sub { $_->set_tag( 'DR11_AddressLine1')},
								   'SCMAT/MemberInformation/City' => sub { $_->set_tag( 'DR13_City')},
								   'SCMAT/MemberInformation/EmailAddress' => \&emailTag,
								   'SCMAT/MemberInformation/PrimaryName' => sub { $_->set_tag( 'DR8_PrimaryName')},
								   'SCMAT/MemberInformation/IsBadAddress' => sub { $_->set_tag( 'DR19_IsBadAddress')},
								   'SCMAT/MemberInformation/IsENotice' => sub { $_->set_tag( 'DR20_IsENotice')},
								   'SCMAT/MemberInformation/IsForeignAddress' => sub { $_->set_tag( 'DR21_IsForeignAddress')},
								   'SCMAT/MemberInformation/IsNoMail' => sub { $_->set_tag( 'DR22_IsNoMail')},
								   'SCMAT/MemberInformation/MaskedAccountNumber' => sub { $_->set_tag( 'DR7_MaskedAccountNumber')},
								   'SCMAT/MemberInformation/PostalCode' => sub { $_->set_tag( 'DR15_PostalCode')},
								   'SCMAT/MemberInformation/State' => sub { $_->set_tag( 'DR14_State')},
								   'SCMAT/MemberInformation/Suffix' => sub { $_->set_tag( 'DR6_SuffixType')},
								   'SCMAT/MemberInformation/SuffixType' => sub { $_->set_tag( 'DR5_Suffix')},								   
								  

                                 },
                pretty_print => 'indented',
              )
         ->parsefile($SourceFile1)
         ->print($fp);

sub emailTag {
    my ($t, $emailID) = @_;		
    $emailID->set_tag( 'DR18_EmailAddress');   
    $orig_emailID= $emailID->text;	
	print "orig_emailID: $orig_emailID \n";	
	$AccountNumber = $emailID->child('AccountNumber');
	print "AccountNumber: $AccountNumber \n";
	if (exists $hash{$AccountNumber})
    {
	$str=$hash{$AccountNumber};
	$emailID->set_text($str); 
	}
	else {
		$emailID->set_text($orig_emailID); 
	}
	
	#$SCMAT->insert_new_elt( 'DR2_RRD_Unique_ID' );
}

@gaun,

Hi, did you see the response to your previous post https://community.unix.com/t/perl-xml-help-needed/393498/4 , it would be courteous to get some sort of acknowledgement to that first , regardless of whether you used/ignored.

As for this request, what do you expect the output to be ?

Suggest you checkout the perl twig documentation (the link below)
https://xmltwig.org/xmltwig/
there's also a twig tutorial link in that which may enlighten.

rds

1 Like

Thank you. I'll look into this document

1 Like