Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading and Writing files. 2

Status
Not open for further replies.

sosuke

Programmer
Apr 5, 2005
3
US
Alright, so I have managed to open the file I want, read it in, edit it, and then save it. However right now I have hard coded the line I want, which is a bad idea IMHO. The file I am reading in is a XML file, I know I know, use XML::Simple, well I was told not to, and I like my job more than I want to make a point. So I read the XML file it, but I want to check each line for a string, the problem is nigh. All of the XML elements are not read! Only the node contents, lets look at an example:

<house>
<room>Kitchen</room>
<room>Computers</room>
</house>

I only end up with Kitchen and Computers when I read in the lines like so:

open FILE, "<$file";
@lines = <FILE>;
close FILE;

The string I want to look for is in the attribute of an element, so you see my dilema, I can't see the elements, how can I search for my string? Any ideas whats going on here?
 
You shouldn't get only that, you should get the whole thing.
Code:
$file = "yourXMLfile.xml";
open FILE, "<$file";
@lines = <FILE>;
close FILE;
print @lines;
gives you
Code:
<house>
<room>Kitchen</room>
<room>Computers</room>
</house>
Something else went wrong.
What about the rest of your code?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I agree the whle file should ALL be read in.

I use a slightly different method to ensure the file is locked while i work on the data retrieved.

my file is a pipe | dilemited text file.

Code:
my $my_file = "filename.txt";

open(CATAL, "+<$my_file"); 
      binmode(CATAL);
      flock(CATAL, 2);
      my @lines = <CATAL>;
      foreach my $line (@lines){
        $line =~ s/\s+$//g;
        if($line ne ""){
           my ($var1,$var2,$var3) = split(/\|/, $line);

           do some processing

        }
      }
close(CATAL);

it's never let me down yet :)
 
ROFL, alright, you both win, hahahahaha, sorry I'll get myself together here...

Guess what? This script was being using in a CGI script, so all the elements were not seen, because they came up to be rendered as HTML.

Whew, alright, now that I fixed that problem, I more research on string searching!

Thanks guys for your help.
 
you might want to try this (i cannot find the link for it but i used it for simple parsing of HTML file--and it should work for the xml file too).

#$mech->content is the content of the XML file----

$i = 0;
for ($i = 0; $i < $len; $i++){

$char = substr($mech->content, $i, 1);

if ($char eq "<"){
# YOU CAN TRANSLATE THIS BUFFER :)
print $htmlcontent;
$htmlcontent = "";
$html = "1";
}

if ($html eq "1"){
$htmlcode = $htmlcode . $char;
}

if ($html eq "0"){
$htmlcontent = $htmlcontent . $char;
}

if ($char eq ">"){
$html = "0";
# YOU CAN TRANSLATE THIS BUFFER :)
#print "html code: ", $htmlcode;
$htmlcode = "";
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top