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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:19:32+00:00 2026-05-26T01:19:32+00:00

Edited: Sorry, I mistyped ‘name’ when I mean ‘ref’ and I’ve included the complete

  • 0

Edited: Sorry, I mistyped ‘name’ when I mean ‘ref’ and I’ve included the complete attributes as well

I have a number of xml files that contain, on a single line, a complete xml document. An example would be:

<Reqeusts>
    <WRRequest><Request domain="foo.com"><Rows><Row includeascolumn="n" interval="hour" ref="time" type="group"/><Row includeascolumn="n"  ref="domain_id" type="group"/><Row />...</Rows><Columns><Column ref="user_id"/><Column ref="country_id"/><Column ref="country_name"/>...</Columns></Request></WRRequest>
.
.
.
</Requests>

There are a number of attributes as well that I’m not including for the sake of clarity.

I’m parsing this using XML::Parser & XML::SimpleObject which work fine up to a point. For instance, I’m just printing out the attributes of each of the elements which works except when I try to print out the ‘ref’ attribute of the column element. Then I get an “uninitialized variable” error. The code is:

#!/usr/bin/perl
use warnings;
use diagnostics;
use XML::Parser;
use XML::SimpleObject;
use Cwd;


if ($ARGV[0] eq "") {
  die "usage: sumXML.pl <input file> \n";
}

my $fileName = $ARGV[0];

my $parser = new XML::Parser(Style => 'Tree');
my $xso = XML::SimpleObject->new( $parser->parsefile("$fileName") );


foreach my $wrRequest ($xso->child('WRRequests')->children('RWRequest')) {
  print "Client Name: " . $wrRequest->attribute('clientName') . "\n";
foreach my $xmlRequest ($wrRequest->child('REQUEST')) {
  print "Domain name: " . $xmlRequest->attribute('domain') . "\n";
  print "Service: " . $xmlRequest->attribute('service') . "\n";
  foreach my $xmlRow ($xmlRequest->child('ROWS')->children('ROW')) {
    print "Row Reference: " . $xmlRow->attribute('ref') . "\n";
  }
  foreach my $xmlColumn ($xmlRequest->child('COLUMNS')->children('COLUMN')) {
    print "Column Reference: " . $xmlColumn->attribute('ref') . "\n";
  }
 }
  print "\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-05-26T01:19:32+00:00Added an answer on May 26, 2026 at 1:19 am

    Your sample data does not parse (even if you remove the dots) so it is not valid XML. I’m not sure how your actual data looks like but this is quite important to find the problem.

    I’m certain that there is nothing wrong with XML::Parser or XML::SimpleObject. So please check the following:

    • Do you spell the element/attribute correctly (remember XML is case sensitive)
    • Does the element/attribute actually exist (for example: does every REQUEST-element have a service-attribute? Does every ROW have a ref-attribute?). If they do not exist you have to either reject the input data or deal with the data you have. This of course depends on your requirements.
    • Optional: validate the XML-document-tree against a DTD or XSD to verify the data integrity. This is like the advanced version of the second point.

    I have actually taken the time to make it work (by just changing the case of the element-names, and slightly modifying your “sample data”):

    use strict;
    use warnings;
    use XML::Parser;
    use XML::SimpleObject;
    use Cwd;
    
    
    my $inXML = join "", <DATA>;
    print $inXML;
    
    my $parser = new XML::Parser(Style => 'Tree');
    my $xso = XML::SimpleObject->new( $parser->parse($inXML) );
    
    
    foreach my $wrRequest ($xso->child('Requests')->children('WRRequest')) {
        print "Client Name: " . $wrRequest->attribute('clientName') . "\n";
        foreach my $xmlRequest ($wrRequest->child('Request')) {
            print "Domain name: " . $xmlRequest->attribute('domain') . "\n";
            print "Service: " . $xmlRequest->attribute('service') . "\n";
            foreach my $xmlRow ($xmlRequest->child('Rows')->children('Row')) {
                print "Row Reference: " . $xmlRow->attribute('ref') . "\n";
            }
            foreach my $xmlColumn ($xmlRequest->child('Columns')->children('Column')) {
                print "Column Reference: " . $xmlColumn->attribute('ref') . "\n";
            }
        }
        print "\n";
    }
    
    
    __DATA__
    <Requests>
      <WRRequest clientName="foo">
        <Request service="fooService" domain="foo.com">
          <Rows>
            <Row includeascolumn="n" interval="hour" ref="time" type="group"/>
            <Row includeascolumn="n"  ref="domain_id" type="group"/>
          </Rows>
          <Columns>
            <Column ref="user_id"/>
            <Column ref="country_id"/>
            <Column ref="country_name"/>
          </Columns>
        </Request>
      </WRRequest>
    </Requests>
    

    Output:

    Client Name: foo
    Domain name: foo.com
    Service: fooService
    Row Reference: time
    Row Reference: domain_id
    Column Reference: user_id
    Column Reference: country_id
    Column Reference: country_name
    

    I’ve tested it also with multiple WRRequest-elements (copy&paste) – worked like a charm.

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

Sidebar

Related Questions

--edited for clarity (hopefully) I have an XML file that looks something like this:
[EDITED - really sorry, the code I quoted was wrong - have changed the
Sorry for the title but I don't know other way of asking. EDITED FOR
Sorry for this question. Basically I have a page where I have it automatically
[Edited - Sorry Bart] I've looked at other answers but struggling to match this.
Edited........ sorry Sir I was referring this piece of code from Stephen Toub's article..
Sorry for my English. I have a strange problem. When user click adword link,
edited: <people> <person> <name>John Doe</name> <age>21</age> </person> <person> <name>Jane Smith</name> <age>24</age> </person> </people> actualy
I have a MySQL table here Starcraft2brackets: https://i.stack.imgur.com/welPq.png (re-edited so I could edit again,
EDITED :- Sorry for reediting the Question but actually i am not getting the

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.