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

  • Home
  • SEARCH
  • 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 6575747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:25:33+00:00 2026-05-25T15:25:33+00:00

Currently trying to figure out how best to process the following sample XML data:

  • 0

Currently trying to figure out how best to process the following sample XML data:

<FOO>
    <A>1</A>
    <B>Some Stuff</B>
    <C>
      <C1>
        <C2A><![CDATA[xxx]]></C2A>
        <C2B><![CDATA[yyy]]></C2B>
      </C1>
    </C>
</FOO>

I’m currently using XML::Twig to operate on everything else, and I’d like to continue using this module to achieve my goals, which are:

extract the data from C2A and C2B, and assign those to variables. Note that there may be multiple entries for C2A and C2B, which need to be concatenated into an @array for example. However, my problem is navigating the tree downwards, for example if we were following another example I’ve found, this would suffice for this data:

<MOVIE_LIST>
    <MOVIE>
        <NAME>Name of the Movie</NAME>
            <MOVIE_ID>28372382</MOVIE_ID>
        <DESCRIPTIONS>
             <LONG_DESCRIPTION>This is a long description</LONG_DESCRIPTION>
             <SHORT_DESCRIPTION>short description</SHORT_DESCRIPTION>
        </DESCRIPTIONS>
        <DIRECTOR_LIST>
            <DIRECTOR>director 1</DIRECTOR>
            <DIRECTOR>director 2</DIRECTOR>
        </DIRECTOR_LIST>
    </MOVIE>
    <MOVIE>
      ...
     </MOVIE>
</MOVIE_LIST>

The solution being:
@directors = $elt->first_child(‘DIRECTOR_LIST’)->children_text(‘DIRECTOR’);

However, my issue is that sometimes, these children don’t exist (e.g. there is no C section data sent at all), which is giving me no end of grief as things like the following won’t work:

@C = $elt->first_child('C')->first_child('C1')->children_text('C2');

I’m getting rather stumped at how to achieve my goals, and would gratefully appreciate any advice, simplistic answers welcomed 😉

  • 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-05-25T15:25:33+00:00Added an answer on May 25, 2026 at 3:25 pm

    If one of the methods doesn’t find a child, then it will return undef, on which you can’t call a method of course.

    So you are left with 2 options:

    You can either test each step of your chained expression:

    @C =    $elt->first_child('C') 
         && $elt->first_child('C')->first_child('C1')
         && $elt->first_child('C')->first_child('C1')->children_text('C2')
         || ()
        ;
    

    or use XPath:

    @C= map { $_->text } $elt->findnodes( './C/C1/C2');
    

    The second option is probably easier to read and to maintain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out the best way to normalize the following data set,
I am currently trying to figure out the best way to create a .xls
I am currently trying to figure out what the best way is to create
I am currently building a Rails app, and trying to figure out the best
I'm trying to figure out the best build system for latex. Currently, I use
I am trying to figure out the best data structure to use for this
I am trying to figure out the best way to set our new process
I'm currently trying to figure out the best way to implement doctrine persisted Role
I am currently trying to figure out how to best go about implementing an
I'm trying to figure out the best way to send post data to 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.