473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Extracting a string from an XML file which is located at a our local company URL

Hi ,

The xml file is generated as a webpage. The XML file is just 10 lines. The XML file will be like this :
Expand|Select|Wrap|Line Numbers
  1. <ABC>
  2. <D someattribute="">
  3. <EF baseRef="http://.............." id="some id value" />
  4. </D>
  5. <D someattribute="">
  6. <EF baseRef="http://.............." id="some id value" />
  7. </D>
  8. </ABC>
All i want to extract from this xml file is list of "id" in perl..
I tried doing like this :

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. my $xml_file = 'http://www.w3.org/TR/2001/REC-xsl-20011015/xslspec.xml';
  3. my @hosts;
  4.  
  5. open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
  6.  
  7. while(my $line=<$XMLFILE>)
  8. {
  9. if($line =~ /<head>(.*)<\/head>/)
  10. {
  11. push @hosts,$l;
  12. }
  13. }
  14. close($XMLFILE);
  15. 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 )
Nov 29 '07 #1
9 1583
eWish
971 Expert 512MB
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
Nov 29 '07 #2
eWish
971 Expert 512MB
To help debug you can add the following pragmas which is considered good practice.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
You can just rewrite the regex to look for the id and capture what is between the " " only.

Expand|Select|Wrap|Line Numbers
  1. if($line =~ /id="(.*)"/) {
There may be some speeding penalties.

--Kevin
Nov 29 '07 #3
Hi ,

The xml file is generated as a webpage. The XML file is just 10 lines. The XML file will be like this :
Expand|Select|Wrap|Line Numbers
  1. <ABC>
  2. <D someattribute="">
  3. <EF baseRef="http://.............." id="some id value" />
  4. </D>
  5. <D someattribute="">
  6. <EF baseRef="http://.............." id="some id value" />
  7. </D>
  8. </ABC>
All i want to extract from this xml file is list of "id" in perl..
I tried doing like this :

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. my $xml_file = 'http://www.w3.org/TR/2001/REC-xsl-20011015/xslspec.xml';
  3. my @hosts;
  4.  
  5. open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
  6.  
  7. while(my $line=<$XMLFILE>)
  8. {
  9. if($line =~ /<head>(.*)<\/head>/)
  10. {
  11. push @hosts,$l;
  12. }
  13. }
  14. close($XMLFILE);
  15. 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:

Expand|Select|Wrap|Line Numbers
  1. <EF baseRef="http://.............." id="HOST1" /> 
  2. <GH baseRef="http://.............." id="HOST2" /> 
  3. <IJ baseRef="http://.............." id="HOST3" />
  4.  
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
Feb 17 '08 #4
KevinADC
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. if($line =~ /id="(.*)"/) {
  2.     push @hosts,$1;
  3. }
  4. print "$_\n" for @hosts;
Feb 17 '08 #5
Expand|Select|Wrap|Line Numbers
  1. if($line =~ /id="(.*)"/) {
  2.     push @hosts,$1;
  3. }
  4. print "$_\n" for @hosts;

It Worked, Hurrayyyyyyyyyyyyyyyyyyyyyyy..
Thanks much much much...

Thanks a lot Kevin.. You are an Expert. Thank youuuuuuuuuu
Feb 17 '08 #6
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 :

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $xml_file = 'c.xml';
  5. my @hosts;
  6.  
  7. open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
  8. while(my $line=<$XMLFILE>)
  9. {
  10.         if($line =~ /id="(.*)"/)
  11.         {
  12.         push @hosts,$1;
  13.         }
  14. }
  15. print "$_\n" for @hosts;
  16. close($XMLFILE);
  17.  
  18.  
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 :
Expand|Select|Wrap|Line Numbers
  1. use LWP::Simple;
  2. $content = get("URL");die "Couldn't get it!" unless defined $content;
  3.  
  4.  
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.
Feb 17 '08 #7
mehj123
55
Sorry to trouble you again,


My Present Code is :

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $xml_file = 'c.xml';
  5. my @hosts;
  6.  
  7. open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
  8. while(my $line=<$XMLFILE>)
  9. {
  10.         if($line =~ /id="(.*)"/)
  11.         {
  12.         push @hosts,$1;
  13.         }
  14. }
  15. print "$_\n" for @hosts;
  16. close($XMLFILE);
  17.  
  18.  
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 :
Expand|Select|Wrap|Line Numbers
  1. use LWP::Simple;
  2. $content = get("URL");die "Couldn't get it!" unless defined $content;
  3.  
  4.  
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..
Expand|Select|Wrap|Line Numbers
  1.  my @contents = split(/\n/,$content);
  2. foreach my $line (@contents)
  3. {
  4.         if($line =~ /id="(.*)"/)
  5.         {
  6.         push @hosts,$1;
  7.         }
  8. }
...

Hope this helps..
Feb 20 '08 #8
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..
Expand|Select|Wrap|Line Numbers
  1.  my @contents = split(/\n/,$content);
  2. foreach my $line (@contents)
  3. {
  4.         if($line =~ /id="(.*)"/)
  5.         {
  6.         push @hosts,$1;
  7.         }
  8. }
...

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.
Mar 17 '08 #9
KevinADC
4,059 Expert 2GB
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.
Mar 17 '08 #10

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

Similar topics

20
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...
4
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...
6
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...
2
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...
4
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...
8
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" ...
6
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>...
9
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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
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
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
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...

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.