Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search and replace 1

Status
Not open for further replies.

vincebrown

Technical User
Apr 25, 2005
19
GB
Could someone please tell me how to write a script that searches an .xml file and insert a string between two xml tags? Thanks.
 
Can you give an example of the data you're using, both before and after this program would run? Depending on its complexity, you might get away with a one-liner or it might require using some of the XML modules to parse your XML correctly. Without seeing that data, we'd only be speculating.
 
Thanks for the reply Ishnid. I simply want it to read the file, and when it comes across a tag called '<PreprocessorFlags>' place a string after it.
Thanks. Vince
 
Does this tag has an end? what follows after this tag?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
The closing tag has a slash - </PreprocessorFlags>.
 
is it empty? And if not what is it in between the two tags?
And what is it that you want to put inside there?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
It is empty. I want to insert /U_CPPUNWIND between. them.
 
If you dont want to use any module then do something like this (if you use tabs before any tag ) if you use spaces then change the \t to \s
Code:
open FILE, "your_xml.xml";
foreach(<FILE>){
	push (@file,$_);
}
close FILE;
unlink "your_xml.xml";
open FILE, ">your_xml.xml";
	foreach (@file){
		if ($_ =~ /(.*)<PreprocessorFlags>/){
			print FILE "$_ $1\t/U_CPPUNWIND\n";
		}else{
			print FILE $_;
		}		
	}	
close FILE;
But maybe you should use a module, after all is this the only thing you want to do with this file?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Thanks a lot. The problem is is that it's placing it after the closing tag. Any ideas? See below.

<PreprocessorFlags></PreprocessorFlags>
/U_CPPUNWIND
<IncludeOptions/>
 
oh you have the tags on the same line, thats why.
I thought that you had them like this

<PreprocessorFlags>
</PreprocessorFlags>

Then change the script to
Code:
open FILE, 'your_xml.xml';
foreach(<FILE>){
	push (@file,$_);
}
close FILE;
unlink 'your_xml.xml';
open FILE, '>your_xml.xml';
	foreach (@file){
		if ($_ =~ /(.*)(<PreprocessorFlags>)(.*)/){
			print FILE " $1\t$2/U_CPPUNWIND$3\n";
		}else{
			print FILE $_;
		}		
	}	
close FILE;


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top