473,499 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scanning a directory for XML files and reading them

79 New Member
Hello,

I would like to scan a certain directory for XML files. Then I want to take in every XML file, read it, and stores the contents as a string in an array. For example, if the directory has file1.xml, file2.xml, and file3.xml, they would all be scanned and read. Then, all the contents of file1.xml would be stored in an array as the first element:

@array[0] would contain the stuff in file1.xml
@array[1] would contain the stuff in file2.xml
etc.

This is what I have:

Expand|Select|Wrap|Line Numbers
  1. sub getComponents
  2. {
  3.     my            $dir                     = shift;
  4.     my             @Components        = ();
  5.     my            @xmlFiles                = ();
  6.  
  7.     if ( -d $dir )
  8.     {
  9.         open ( COMPONENTS, $dir );
  10.         @xmlFiles = grep ( /^(.+)\.xml$/, grep ( !/^.{1,2}$/, readdir( COMPONENTS ) ) );
  11.         closedir ( COMPONENTS );
  12.  
  13.         $dir = addSlash( $dir );
  14.  
  15.         foreach ( @xmlFiles )
  16.         {
  17.             my @fileInfo = readTextFile ( $dir . $_ );
  18.             push( @Components, \@fileInfo );
  19.         }
  20.     }
  21.  
  22.     return @Components;
  23. }
readTextFile is another sub that has been tested. No problems there. addSlash adds a slash at the end of the directory so that the files can be opened. This is also tried and tested.

The problem is this code simply doesn't work. I don't know where to start looking.

Thanks
Oct 1 '07 #1
1 1622
eWish
971 Recognized Expert Contributor
The open function opens a file not a directory. Use opendir to open the directory. Also, add some error checking to make sure the directory is actually being opened and read.

Excerpt form perldoc:
Expand|Select|Wrap|Line Numbers
  1.     opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
  2.     @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
  3.     closedir DIR;
Oct 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
16596
by: o'seally | last post by:
solaris/linux admins/rookie_developers that battle with this error are probably all frustrated when it happens. i bet you're also somehow frustrated by this seemingly unsolvable error :-) ...take...
2
17624
by: Dean | last post by:
Hi I've got a question relating to using Javascript on an Intranet. I have a directory with a list of files in the format week36.xls, week37.xls and I want to write a script that will scan...
79
5182
by: pinkfloydhomer | last post by:
I want to scan a file byte for byte for occurences of the the four byte pattern 0x00000100. I've tried with this: # start import sys numChars = 0 startCode = 0 count = 0
4
3204
by: Zen | last post by:
I'm using Access 2000, and I'd like to know if there is a way to use a scanner (flatbed, doc-feed, etc) to scan forms with OMR or OCR software, and have the data be automatically (or if not...
5
3828
by: Bas Hendriks | last post by:
Has anyone any idea how asp.net find it's files back after compiling them to the temporary asp.net directory? I found on numerous webpages that the directorynames are chosen random but cannot find...
3
3747
by: Nick | last post by:
Is it possible to read a list of files from a specified directory using VB.net We have company intranet and I have created a page that displays photos from different events. I have coded a page...
6
3835
by: Bob Alston | last post by:
I am looking for others who have built systems to scan documents, index them and then make them accessible from an Access database. My environment is a nonprofit with about 20-25 case workers who...
4
1359
by: tshad | last post by:
We have a few pages that accept uploads and want to scan the files before accepting them. Does Asp.net have a way of doing a virus scan? We are using Trendmicro to scan files and email but don't...
7
2084
by: paul86 | last post by:
Hi all, I have a vb question that i'm hoping I can get some help with. I have a directory of excel files (probably about 1000) of them. They are quote sheets for the company I work for, and they...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.