LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2004, 08:25 PM   #1
poison
Member
 
Registered: Dec 2003
Location: Layer 7 =D
Distribution: Slackware, LFS, Rock Linux
Posts: 165

Rep: Reputation: 30
_simple_ regex XML parser


hi ^^
I've started writing a little regex XML Parser in perl some time ago, just to practice regular expressions again...it works so far but I haven't come up with an idea how to make it possible to process nested XML tags (I actually mean nested tags with the same name =^ ), such as:
Code:
<data>
  <data>test</data>
</data>
without writing hundreds of lines for instance counting or similar solutions...

the current code is
Code:
# Simple Regex XML Parser
# 2003 by poison

open (XMLDATA, '< /home/poison/files/asz00m5ynC/sync.xml') or die "error, can't open file";
@content= <XMLDATA>;
close (XMLDATA) or die "error closing";
#print "@content";
$file_data=join ('', @content );

parse_xml ($file_data);

sub parse_xml {
	my $match_data=shift;
	my $match_level=shift;
	my $tag_matched;
	my $tag_parameters;
	my %tag_parameter;
	my $tag_content;
		
	while ( $match_data =~ m/<([^\/>][^> ]*)([^>]*)>([\s\S]*?)<\/\1>/gi ) {
		#extract parameter list and content from xml tag, seperate
		$tag_matched=$1;
		$tag_parameters=$2;
		%tag_parameter=parse_parameters ($tag_parameters);
		$tag_content=$3;
		#print $1 . '=>' . $2;
		#$match_data=$2;
		print (' ' x $match_level);
		#print $tag_matched;
		create_cfg ($tag_matched, $tag_parameters, $tag_content);
		if ($tag_content =~ m/<([^\/>][^> ]*)([^>]*)>([\s\S]*?)<\/\1>/gi) {
			print $tag_matched . ':';
			while (($key, $value) = each %tag_parameter) {
				print $key . '=>"' . $value . '" ';
 			}
 			print "\n";
		} else {
			print $tag_matched . ':';
			while (($key, $value) = each %tag_parameter) {
				print $key . '=>"' . $value . '" ';
 			}
 			print '("' . $tag_content . "\")\n" 
		}
		
		parse_xml ($tag_content, $match_level+1);
	}
	
}
#print $file_data;

sub parse_parameters {
	#split up parameter list
	my $parameters=shift;
	my %parameter;
	while ( $parameters =~ /([^ =]+)\s*=\s*"([^"]*)"/gi ) {
		$parameter{$1}=$2;
	}
	return %parameter;
}

sub create_cfg {
	my $tag_matched=shift;
	my $tag_parameters=shift;
	my $tag_content=shift;
	
	my %tag_parameter=parse_parameters ($tag_parameters);
	
	if ($tag_matched eq 'database') {
		@source=($tag_parameter{'source'}, $tag_parameter{'destination'});
	}
	
}
suggestions anybody ? ^^

Last edited by poison; 01-03-2004 at 04:40 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 07:24 AM
XML Parser bulkman Linux - Software 6 04-29-2005 07:01 AM
xml parser in linux shilpig Programming 1 05-13-2004 11:24 AM
gDesklets and the xml parser doralsoral Linux - Software 1 05-08-2004 11:56 AM
Galeon and XML::Parser virus2099 Slackware 1 01-20-2004 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration