473,396 Members | 1,891 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,396 software developers and data experts.

Extracting a specific string from an XML file

79
Hello,

I have an XML file that I need to extract data from. The file has multiple lines, one of which contains the following tag:

Expand|Select|Wrap|Line Numbers
  1. <Owner> owner's name goes here </Owner>
I need to be able to extract the owner's name from the XML file. I have never programmed Perl before, but I have a pretty strong background in C/C++ and Java. You're probably wondering why the hell I'm jumping into this with no experience in Perl...it wasn't my choice, really. I just started working for a company and the person in charge of the Perl scripts is no longer here, and I just happened to be the only person free enough to take over the project.

I've been doing some research on regular expressions and I found this code:

Expand|Select|Wrap|Line Numbers
  1. if($mystring =~ m/start(.*)end/) {
  2.     print $1;
  3. }
Apparently, this would print everything between the word "start" and "end". Based on this, would the following code work?

Expand|Select|Wrap|Line Numbers
  1. if($mystring =~ m/<Owner>(.*)</Owner>/) {
  2.     print $1;
  3. }
I'm thinking that the / in </Owner> would cause problems...how would that be fixed?

Thanks for your help.
Sep 24 '07 #1
1 2897
eWish
971 Expert 512MB
Really you should probably look at using XML::Simple or XML::Parser. However, you can do it the way you are thinking. To keep </Owners> from causing problems just escape the forward slash.
Expand|Select|Wrap|Line Numbers
  1. my $xml_file = 'myfile.xml';
  2. my @owners;
  3.  
  4. open(my $XMLFILE, '<', $xml_file) ||die "Can't open file: $!";
  5. while (my $line =<$XMLFILE>) {
  6.  
  7.     if($line =~ /<Owners>(.*)<\/Owners>/) {
  8.     push @owners, $1;
  9.     }
  10. close($XMLFILE);
  11.  
  12. print join("\n", @owners);
Not sure how big of a file you are talking about. So, there could be a performance issue.
Sep 24 '07 #2

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

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
2
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
5
by: Astra | last post by:
Hi All Is there an ASP way of extracting the height and width of a swf file so that I can specify these dims when adding the whole OBJECT code to the web page? Thanks Robbie
2
by: Branden | last post by:
hi guys, i was wondering if it is possible to extract selected words in a field to be put in different fields automatically. Do i have to write the code in vb? This is what im trying to do....
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
2
by: Lana rose | last post by:
I am trying to produce some code in C++ that will be able to scan through a mixed document and extract specific lines of data. The document will look like this (below) but will have hundreds of these...
4
by: Debbiedo | last post by:
My software program outputs an XML Driving Directions file that I need to input into an Access table (although if need be I can import a dbf or xls) so that I can relate one of the fields...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
4
by: dexter48 | last post by:
Hi I'm searching for a string occurance in a text file. I find the string ok and write the results to a log file. But on the line above is also some information I need. How can i get that. The string...
2
by: daveftl | last post by:
Hello, i've tried to extract certain data using Regex in a File. but it seems not working.No errors and warnings have been found. here is my code: Private Sub extractTxt(ByVal inputFile As...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...

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.