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 7183187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:55:43+00:00 2026-05-28T17:55:43+00:00

Perl snippet: my $xml = new XML::Simple( KeyAttr=>{ property => ‘propertyname’, }, ForceArray =>

  • 0

Perl snippet:

my $xml = new XML::Simple(
    KeyAttr=>{
        property => 'propertyname',          
    },
    ForceArray => 1,
    ContentKey => '-content');

my $config = $xml->XMLin($configFile);

Configfile looks like:

<config>
<property propertyname="text1" b="text2" c="text3" d="text4">
 text5
</property>
<property propertyname="text6" b="text7" c="text8" d="text9">
 text10
</property>
</config>

How do I parse this config file so that c becomes a key and I can access the corresponding b and d? What does KeyAttr do?

  • 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-28T17:55:44+00:00Added an answer on May 28, 2026 at 5:55 pm

    XML::Simple returns a Perl data structure (see perldoc perldsc) which you can visualize using Data::Dumper.
    Here is one way to access the data you need:

    use warnings;
    use strict;
    use XML::Simple;
    
    my $xfile = '
    <config>
    <property propertyname="text1" b="text2" c="text3" d="text4">
     text5
    </property>
    <property propertyname="text6" b="text7" c="text8" d="text9">
     text10
    </property>
    </config>
    ';
    
    my $xml = new XML::Simple(
        KeyAttr=>{
            property => 'propertyname',          
        },
        ForceArray => 1,
        ContentKey => '-content');
    
    my $config = $xml->XMLin($xfile);
    
    print "$config->{property}{text1}{c}\n";
    print "$config->{property}{text6}{c}\n";
    print "$config->{property}{text1}{d}\n";
    print "$config->{property}{text6}{d}\n";
    

    Output:

    text3
    text8
    text4
    text9
    

    You can read about KeyAttr from perldoc XML::Simple

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

Sidebar

Related Questions

I'm reading Programming Perl , and I found this code snippet: sub new {
I am new to perl and can't seem to find why this snippet is
This snippet of Perl code in my program is giving the wrong result. $condition
I'm looking for ways to express this Python snippet in Perl: data = {A:
Let us say if I have a Perl module Resounces.pm with this code snippet
I'm using XML::XML2JSON in Perl to convert JSON data to XML, I am passing
I have the following snippet calling a perl script which writes to STDERR and
the following code snippet taken from http://perldoc.perl.org/perlrequick.html#Search-and-replace gives me Bareword found where operator expected
I created csv file with this perl module Text::CSV_XS on windows: Below a snippet
I'd like a simple perl script / sub to read a sql server table

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.