473,804 Members | 3,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

10 New Member
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 1605
eWish
971 Recognized Expert Contributor
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 Recognized Expert Contributor
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
alexscript
10 New Member
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,HOS T3 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 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. if($line =~ /id="(.*)"/) {
  2.     push @hosts,$1;
  3. }
  4. print "$_\n" for @hosts;
Feb 17 '08 #5
alexscript
10 New Member
Expand|Select|Wrap|Line Numbers
  1. if($line =~ /id="(.*)"/) {
  2.     push @hosts,$1;
  3. }
  4. print "$_\n" for @hosts;

It Worked, Hurrayyyyyyyyyy yyyyyyyyyyyyy..
Thanks much much much...

Thanks a lot Kevin.. You are an Expert. Thank youuuuuuuuuu
Feb 17 '08 #6
alexscript
10 New Member
It Worked, Hurrayyyyyyyyyy yyyyyyyyyyyyy..
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 New Member
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
alexscript
10 New Member
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 Recognized Expert Specialist
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
2014
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 when I put it out on the test server the application is looking for the file on the server & not on the client machine. I am essentially taking the query string & using a stream reader to read the file. I'm sure i'm doing something...
4
1657
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 then the IIS System in the same Network.But Both are in the same Local Network
6
2569
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 extracted files to a specified directory? I can use the following: system("move *.* c:\directory"); However, this will move my program. I end up having to call system for every
2
2819
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 C#. The only significant problem that I have encountered in the conversion is this one - extracting an icon from the 'KTEntryPoint' program into the software suite and placing that icon on the PC Desktop.
4
4517
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 local machine at a pre defined path (path defined in the registry), and launch the file in its associated application. I think this can be done through ActiveX Controls, but i dont know how to create ActiveX controls in .NET??
8
2594
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" id="hplTest" runat="server" >testhyperlink</a> Now when I click on the hyperlink, nothing is happening. No error. when I open a browser and type file:///D:/Test/Test.txt, the file is opening.
6
6317
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> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY>
9
2472
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 worker...). Anyways, to help them, they first asked me to copy all the text from all the pages of the site (and there is a lot!) to word documents. I found the idea pretty stupid since style would have to be applied from scratch anyway since we...
1
1655
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 Database that is a subfolder of the folder where your exe file is located then you should be able to do this: Data Source=Database\Database.mdb
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10320
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10308
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7609
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.