Hi ,
The xml file is generated as a webpage. The XML file is just 10 lines. The XML file will be like this : - <ABC>
-
<D someattribute="">
-
<EF baseRef="http://.............." id="some id value" />
-
</D>
-
<D someattribute="">
-
<EF baseRef="http://.............." id="some id value" />
-
</D>
-
</ABC>
All i want to extract from this xml file is list of "id" in perl..
I tried doing like this : - #!/usr/local/bin/perl
-
my $xml_file = 'http://www.w3.org/TR/2001/REC-xsl-20011015/xslspec.xml';
-
my @hosts;
-
-
open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
-
-
while(my $line=<$XMLFILE>)
-
{
-
if($line =~ /<head>(.*)<\/head>/)
-
{
-
push @hosts,$l;
-
}
-
}
-
close($XMLFILE);
-
print join (",",@hosts);
The error i got are :
Name "main::l" used only once: possible typo at host1.cgi line 11.
Can't open file: No such file or directory at host1.cgi line 5.
Can you suggest me the easiest way to get a string from an XML file(not present in my system , but available as a webpage in our company )
9 1583
Welcome to TSDN!
Please do not double post your questions. I have deleted your other post for you. Also, when posting code please use the [CODE][/CODE] tags.
Thank You,
Kevin
To help debug you can add the following pragmas which is considered good practice. - use strict;
-
use warnings;
You can just rewrite the regex to look for the id and capture what is between the " " only. - if($line =~ /id="(.*)"/) {
There may be some speeding penalties.
--Kevin
Hi ,
The xml file is generated as a webpage. The XML file is just 10 lines. The XML file will be like this : - <ABC>
-
<D someattribute="">
-
<EF baseRef="http://.............." id="some id value" />
-
</D>
-
<D someattribute="">
-
<EF baseRef="http://.............." id="some id value" />
-
</D>
-
</ABC>
All i want to extract from this xml file is list of "id" in perl..
I tried doing like this : - #!/usr/local/bin/perl
-
my $xml_file = 'http://www.w3.org/TR/2001/REC-xsl-20011015/xslspec.xml';
-
my @hosts;
-
-
open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
-
-
while(my $line=<$XMLFILE>)
-
{
-
if($line =~ /<head>(.*)<\/head>/)
-
{
-
push @hosts,$l;
-
}
-
}
-
close($XMLFILE);
-
print join (",",@hosts);
The error i got are :
Name "main::l" used only once: possible typo at host1.cgi line 11.
Can't open file: No such file or directory at host1.cgi line 5.
Can you suggest me the easiest way to get a string from an XML file(not present in my system , but available as a webpage in our company )
Thanks a lot for your reply.
Now the above code is returning the entire line that is matched.
EX: - <EF baseRef="http://.............." id="HOST1" />
-
<GH baseRef="http://.............." id="HOST2" />
-
<IJ baseRef="http://.............." id="HOST3" />
-
All I want now is HOST1,HOST2,HOST3 seperated with single comma as a single string.
How do i get only HOST1 rather than complete line
<EF baseRef="http://.............." id="HOST1" />
Please help me.
Thanks in Advance
- if($line =~ /id="(.*)"/) {
-
push @hosts,$1;
-
}
-
print "$_\n" for @hosts;
- if($line =~ /id="(.*)"/) {
-
push @hosts,$1;
-
}
-
print "$_\n" for @hosts;
It Worked, Hurrayyyyyyyyyyyyyyyyyyyyyyy..
Thanks much much much...
Thanks a lot Kevin.. You are an Expert. Thank youuuuuuuuuu
It Worked, Hurrayyyyyyyyyyyyyyyyyyyyyyy..
Thanks much much much...
Thanks a lot Kevin.. You are an Expert. Thank youuuuuuuuuu
Sorry to trouble you again,
My Present Code is : -
#!/usr/bin/perl
-
use strict;
-
use warnings;
-
my $xml_file = 'c.xml';
-
my @hosts;
-
-
open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
-
while(my $line=<$XMLFILE>)
-
{
-
if($line =~ /id="(.*)"/)
-
{
-
push @hosts,$1;
-
}
-
}
-
print "$_\n" for @hosts;
-
close($XMLFILE);
-
-
The Above code works absolutely if i give the XML file directly.
But the URL is not directly the xml file. We need to send a GET request for it.
Now i need to send a GET Request to a URL rather than directly specifying the link as C.XML
If i use something like this : -
use LWP::Simple;
-
$content = get("URL");die "Couldn't get it!" unless defined $content;
-
-
Now this is no more a file, rather $content contains string.
How do i Apply the above code in this case.
Waiting for your reply ,
Thanks for the help so far and please help me this time too.
-Alex.
Sorry to trouble you again,
My Present Code is : -
#!/usr/bin/perl
-
use strict;
-
use warnings;
-
my $xml_file = 'c.xml';
-
my @hosts;
-
-
open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
-
while(my $line=<$XMLFILE>)
-
{
-
if($line =~ /id="(.*)"/)
-
{
-
push @hosts,$1;
-
}
-
}
-
print "$_\n" for @hosts;
-
close($XMLFILE);
-
-
The Above code works absolutely if i give the XML file directly.
But the URL is not directly the xml file. We need to send a GET request for it.
Now i need to send a GET Request to a URL rather than directly specifying the link as C.XML
If i use something like this : -
use LWP::Simple;
-
$content = get("URL");die "Couldn't get it!" unless defined $content;
-
-
Now this is no more a file, rather $content contains string.
How do i Apply the above code in this case.
Waiting for your reply ,
Thanks for the help so far and please help me this time too.
-Alex.
Hi Alex,
After getting the entire data into $content, you can split it line by line and store it in an array and use the same code for this array.. - my @contents = split(/\n/,$content);
-
foreach my $line (@contents)
-
{
-
if($line =~ /id="(.*)"/)
-
{
-
push @hosts,$1;
-
}
-
}
...
Hope this helps..
Hi Alex,
After getting the entire data into $content, you can split it line by line and store it in an array and use the same code for this array.. - my @contents = split(/\n/,$content);
-
foreach my $line (@contents)
-
{
-
if($line =~ /id="(.*)"/)
-
{
-
push @hosts,$1;
-
}
-
}
...
Hope this helps..
I did so, but I am able to get everything from the first match of the regular expression ie., everything from the first id name.
The problem is when i am changing the xml into a string, everything is in a single string(i dont think that it is returning as a sequence of lines , rather it is single string i used split but then i am not sure where did it go wrong). and $1 is returning the first match, thus i am able to get the first id , and everything from there.
Check the LWP::Simple documentation, you can probably write the data to a file then open the file like you were doing. Not sure if it will work but you can try.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steve |
last post by:
I have a web app that needs to parse through a file that is located on the
client machine. I get the file string from a query string & then parse it.
It is working fine on my development box but...
|
by: Prabhat |
last post by:
Hi All,
I have BIG Problem in ASP.
I have a ADO Connection that I Want to Connect to DBASE IV Database Located
in a Local Networked System.
The DBASE Database is located in other system...
|
by: Andrew Edwards |
last post by:
I have program that downloads a file from the internet and extracts it using
calls to system(). All files are extracted into the directory where my
program is located. How do I redirect the...
|
by: Dickyb |
last post by:
Extracting an Icon and Placing It On The Desktop (C# Language)
I constructed a suite of programs in C++ several years ago that handle my
financial portfolio, and now I have converted them to...
|
by: sunilj20 |
last post by:
Hello,
I have a requirement wherein, a user clicks on a file name in an
ASP.NET web application, and the file should automatically be
downloaded (Without showing the "Open", "Save As") in the...
|
by: Sohan Kamat |
last post by:
Hi,
I have a webform and I want to create a hyperlink to a text file, so that it
opens on internet explorer in a new window.
I have added the following
<a href="file:///D:/Test/Test.txt" ...
|
by: Mag Gam |
last post by:
Hi All,
I am new to XML, and trying to extract some data from a file.
The file looks like this:
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>...
|
by: sebzzz |
last post by:
Hi,
I work at this company and we are re-building our website: http://caslt.org/.
The new website will be built by an external firm (I could do it
myself, but since I'm just the summer student...
|
by: =?Utf-8?B?S2VycnkgTW9vcm1hbg==?= |
last post by:
Perry,
Application.StartupPath gives you the location of the exe file. This is
where the database is assumed to be located if you don't say otherwise.
If your database is in a folder named...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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,...
|
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...
| |