Perl Search and Replace XML tags conditionally
Hi there,
I am a newbie to Perl and I am trying to write a script that conditionally searches for particular tag and then replaces some elements within it. Here is the example XML <TRANSFORMATION DESCRIPTION ="" TYPE ="Expression"> <TRANSFORMFIELD DATATYPE ="bigint" PRECISION ="19"/> </TRANSFORMATION> <TRANSFORMATION DESCRIPTION ="" TYPE ="Sequence"> <TRANSFORMFIELD DATATYPE ="bigint" PRECISION ="19"/> </TRANSFORMATION> The script I want to write would search for Sequence element and change the datatype to Integer eg the output should be:- <TRANSFORMATION DESCRIPTION ="" TYPE ="Expression"> <TRANSFORMFIELD DATATYPE ="bigint" PRECISION ="19"/> </TRANSFORMATION> <TRANSFORMATION DESCRIPTION ="" TYPE ="Sequence"> <TRANSFORMFIELD DATATYPE ="Integer" PRECISION ="19"/> </TRANSFORMATION> I tried doing a find and replace but clearly that would do everything. I need to only do it the Sequence transformation tags. Your expert advice would be appreciated!!! |
Quote:
|
Your XML fragment is incomplete, so it is difficult to do any comprehensive testing, but I can offer this advice: don't try to contrive a Perl parser for your XML data. Use one of the numerous ready-made modules for the purpose. Which one you choose will depend somewhat on the nature of your data and your application. The easiest one to use is, not surprisingly, XML::Simple, which is a SAX style parser. The potential downside to this parser is that the order of sibling XML elements may be lost in the parse, and that it does not provide a method to write the acquired XML data back to a file.
The other two prominent XML parsers for Perl are the expat-based XML::Parser and the xerces-based XML::Xerces. The APIs for these two are distinctly different, and the XML::Xerces module also provides a method for writing your XML data back to a file. A sample Perl script that uses the XML::Simple module: Code:
#! /bin/perl -w --- rod. EDIT: As I composed my reply, Sergei was posting his response, and I see that he has given principally the same advice. |
Thanks for this. Unfortunately when I use the XMLOut function some of the tags are not coming out correctly. For example
in my original XML File I had <PARTITION DESCRIPTION ="" NAME ="Partition #1"/> this now translates to :- <PARTITION NAME="Partition #1" DESCRIPTION="" /> Any ideas? should I use something other than XML:Simple? |
This is what I meant when I said "the order of sibling XML elements may be lost in the parse". While this is still perfectly valid XML, some applications are sensitive to order, and XML::Simple will not work where this is the case.
--- rod. |
Quote:
|
Not sure what you mean by "after manipulation", but as I understand it, Perl hashes are always unordered. Since XML::Simple stores data in hashes, the order of retrieval, and presumably therefore, the order of writing, is not preserved.
--- rod. |
Quote:
If you change in a Perl hash something, you can't guarantee preservation of order. OTOH, there are modules implementing hashes with constant keys order (IIRC). It probably won't help the OP since most likely the XML parsers don't use such hashes. |
Quote:
|
Quote:
|
Quote:
|
Code:
#!/usr/bin/env ruby Code:
$ cat xmlfile |
Quote:
|
Quote:
The order of tag attributes is usually not a factor for XML-using applications, so if it has been shown that your application is sensitive in that way, it would not be surprising if it is also sensitive to other formatting issues, such as contained whitespace. Sounds like your application may have been written in just the manner that I and Sergei cautioned against. Pity. --- rod. |
Quote:
|
Hi,
seeing that - your application is on bad terms with the perl-parser - you do not have ruby - your application might be very formatting sensitive , thus - maybe also on bad terms with ruby well, maybe you could use something simpler approach, like Code:
$ cat file If you need other tags changed you might have to exchange the keywords with variables. |
All times are GMT -5. The time now is 02:47 PM. |