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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:34:57+00:00 2026-05-23T21:34:57+00:00

Okay… We have a contact books in Exchange that gets exported into an XML

  • 0

Okay… We have a contact books in Exchange that gets exported into an XML file… that gets used by our intranet… for our Associate Directory. “Something” happened that caused a chain of events that lead to the XML getting updated.

Apparently, our Squirrel Mail server uses a Perl script to transform this XML into a global.abook.

I’m not versed in Perl, but the generic idea’s seem easy to follow: Traversing the XML, for each person pull “Nickname”, Full Name, Email & Title and put into global.abook.

I’m certain the OLD XML file didn’t have the Root\XSD:Schema and Root\DataRoot layout. Uncertain as to what the best format for an update on this would be.

Perl Script:

#!/usr/bin/perl
use strict;

use XML::Parser;
use Data::Dumper;

my $url = 'http://intranet.mycompany.org/directory/directory.xml';
my $output = '/var/lib/squirrelmail/prefs/global.gabook';

my $file = "curl -sS '$url' |";
my $parser = new XML::Parser(Style => 'Tree');
my $tree = $parser->parsefile($file)->[1];

sub extract {
        my ($string, $record) = @_;
        for (my $i = 0; $i < @{$record}.''; $i++) {
                if ($record->[$i] eq $string) {
                        return $record->[$i + 1][2];
                }
        }
        return undef;
}

open FILE, "> $output"
        or die "Couldn't open: $!";
for (my $i = 4; $i < @{$tree}.''; $i += 4) {
        my $record = $tree->[$i];
        my $full = &extract('DisplayName', $record);
        my $title = &extract('JobTitle', $record);
        my $email = &extract('EMailDisplayName', $record);
        next unless($email);
        my $nickname;
        # Nickname is the first part of the email address
        if ($email =~ /^(\w+)\@/) {
                $nickname = $1;
        }
        print FILE "$nickname|$full||$email|$title" . "\n";
}
close FILE

XML File:

<?xml version="1.0" standalone="yes"?>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:od="urn:schemas-microsoft-com:officedata">
  <xsd:schema>
  ...
  </xsd:schema>
  <dataroot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" generated="2011-07-12T14:14:13">
    <ROW>
      <DisplayName>John Doe</DisplayName>
      <FirstName>John</FirstName>
      <LastName>Doe</LastName>
      <JobTitle>I.D. 10 Technologist</JobTitle>
      <Company>My Company</Company>
      <Department>Administration</Department>
      <FileAs>Doe, John</FileAs>
      <BusinessPhone>(800) 867-5309</BusinessPhone>
      <EMailAddress>jdoe@mycompany.org</EMailAddress>
      <EMailAddressType>SMTP</EMailAddressType>
      <EMailDisplayName>jdoe@mycompany.org</EMailDisplayName>
      <Initials>J.D.</Initials>
      <Private>0</Private>
    </ROW>
    <ROW>
      ...
    </ROW>
  </dataroot>
</root>

Desired Text file:

jdoe|John Doe||jdoe@atlanticgeneral.org|I.D. 10 Technician
...
...
  • 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-23T21:34:58+00:00Added an answer on May 23, 2026 at 9:34 pm

    Is this what you were looking for?

    use strict;
    use warnings;
    use XML::Simple;
    use LWP::Simple;
    
    
    my $url = 'http://intranet.mycompany.org/directory/directory.xml';
    my $outfile = '/var/lib/squirrelmail/prefs/global.gabook';
    
    
    my $xml = get( $url );
    my $structure = XMLin( $xml );
    
    open my $out_fh, '>', $outfile or die $!;
    foreach my $row ( @{ $structure->{dataroot}{ROW} } ) {
        next unless exists $row->{FileAs} and defined $row->{FileAs};
        my( $email, $name, $title ) = map{
            warn "Warning: $_ is undefined for $row->{FileAs}."
                unless exists $row->{$_} and defined $row->{$_};
            $row->{$_} || '';
        } qw/ EMailAddress DisplayName JobTitle /;
        my $nick;
        if( $email =~ m/^([^@]+)@/ ) {
            $nick = $1;
        } else {
            $nick = '';
            warn "Warning: No nickname for $row->{FileAs}.";
        }
        print $out_fh "$nick|$name||$email|$title\n";
    }
    
    close $out_fh or die $!;
    

    If your XML is not terribly complex, XML::Simple is an easy solution. Also, I don’t see a big need for using curl from the shell when you could just use LWP::Simple from within Perl. You could easily modify the above to become closer in its dependencies to your original script if you like though. My use of LWP::Simple could be replaced by your curl.

    I added on-screen warnings and default behavior in the case of a particular field not containing anything or not being present. For example, if EMailAddress is missing for a given row, you will get a couple of warnings about that. But a default empty string will be inserted into that column position for graceful recovery. If you considered such an issue to be serious enough you could change the warns to die.

    I’m also skipping any ROW that doesn’t have a defined FileAs tag, under the assumption that at least one tag in particular has to exist for the record to be valid. You could alter that to taste, but I would keep some form of graceful ‘move on if it’s not a valid record’ code in there just in case.

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

Sidebar

Related Questions

Okay here's my situation. I have a php file that contains a simple form
Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay - I have a dilemma. So far my script converts page titles into
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay... so I have a fairly interesting error... I declare a FileWriter called file,
Okay that title may not have been too clear, I am building a site
Okay, I kinda asked this question already, but noticed that i might have not
Okay getting some weirdness. I have a simple URLLoader in AS3 that loads an
Okay I have installed a theme in wordpress that returns a few errors. Warning:
Okay so I can't figure this out. Like a file that I using grep

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.