Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8315589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:09:58+00:00 2026-06-08T21:09:58+00:00

I am still new with using XML::Twig . From the main program I look

  • 0

I am still new with using XML::Twig.

From the main program I look for the elements with attribute @method="create"

In the subroutine player, I want to find the next element/sibling with attribute @method="modify".

Both of these methods fail in subroutine player with the error Can't call method "gi" on an undefined value

my $modif1=$valeur->next_sibling('[@method="modify"]')

my $modif2=$valeur->next_elt('[@method="modify"]')->parent

Example of input.xwo:

<top id="World">

  <middle id="France" method="create">
  </middle>

  <middle id="Germany" method="modify">
  </middle>

</top>

My simple code:

#!/bin/perl -w
use warnings;
use XML::Twig;

my $twig= new XML::Twig( 
  twig_handlers => {
    '[@method="create"]' => \&player
  } 
);                    

$twig->parsefile("input.xwo");
$twig->purge;  

sub player { 

  my ($twig, $valeur) = @_;                     

  my $modif1 = $valeur->next_sibling('[@method="modify"]');
  my $modif2 = $valeur->next_elt('[@method="modify"]')->parent;
  print "\nnextELT=" . $modif->gi . "\n";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:10:00+00:00Added an answer on June 8, 2026 at 9:10 pm

    Please always use strict at the top of your programs and declare variables using my at their first point of use. There is no point in both use warnings and the -w command-line option. The first is preferable.

    When the twig handler for the <middle method="create"> element is called, the sibling that you require, <middle method="modify"> hasn’t been processed. It can’t be found because it isn’t yet in the XML::Twig parse tree.

    You must either read the entire XML structure and process it afterwards, or write a handler for the element that encloses both the create and modify elements.

    This program does the latter.

    use strict;
    use warnings;
    
    use XML::Twig;
    
    my $twig= XML::Twig->new(
      twig_handlers => {
        top => \&player
      } 
    );                    
    
    $twig->parsefile('input.xwo');
    $twig->purge;  
    
    sub player { 
    
      my ($twig, $valeur) = @_;                   
    
      my $create = $valeur->first_child('[@method="create"]');
      return unless $create;
    
      my $modify = $create->next_sibling('[@method="modify"]');
      return unless $modify;
    
      printf "\nnextELT = %s\n", $modify->gi;
    
      $twig->purge;
    }
    

    Update

    This alternative reads the entire XML tree and extracts the data from it afterwards. It starts by finding all elements in the tree that have a method attribute equal to 'create', and then finds the following sibling of each of them with a method attribute of 'modify'.

    use strict;
    use warnings;
    
    use XML::Twig;
    
    my $twig= XML::Twig->new;
    
    $twig->parsefile('input.xwo');
    
    my @creates = $twig->get_xpath('//*[@method="create"]');
    
    foreach my $create (@creates) {
      my $modify = $create->next_sibling('[@method="modify"]');
      next unless $modify;
      printf "\nnextELT = %s\n", $modify->gi;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am from Java Swing background. May I know why using XML to create
So I am using my main.xml as a view in my program but I
I still new using Asp.net with vb.net Protected Sub login_Click(ByVal sender As Object, ByVal
Ok, I'm still new to using C sockets, but I was wondering if there
I am still pretty new to using jQuery to do anything, but I have
still new to XML parsing with the iphone so i have a few questions.
I have a program where it reads and writes XML using XMLReader and XMLWriter
I'm new to Open Xml, and have created a reporting application using Open Xml
I am having troubles finding elements in an Xml file using LINQ to XML
I've been using the new $.parseXML() method with jQuery 1.5. Whenever I insert a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.