It's easy enough using an XML Parser. Like XML::Twig
.
For example:
#!/usr/bin/perl;
use strict;
use warnings;
use XML::Twig;
XML::Twig->new(
'pretty_print' => 'indented_a',
'twig_handlers' => {
'var[@name="CLASS"]/string' => sub { $_->set_text('NoDatabase') }
}
)->parse( *DATA )->print;
__DATA__
<?xml version="1.0"?>
<wddxPacket version="1.0">
<header/>
<data>
<array length="2">
<struct type="coldfusion.server.ConfigMap">
<var name="CFASTSTJ">
<struct type="coldfusion.server.ConfigMap">
<var name="alter">
<boolean value="true"/>
</var>
<var name="CLASS">
<string>macromedia.jdbc.MacromediaDriver</string>
</var>
</struct>
</var>
</struct>
</array>
</data>
</wddxPacket>
This uses an xpath expression 'var[@name="CLASS"]/string'
- which means any element var
with a name
attribute equal to CLASS
with a subelement string
. It will apply this to any instance that matches this. You may need a more specific xpath for your data. (e.g. data/array/struct[@type="coldfusion.server.ConfigMap"]/var[@name="CFASTSTJ"]/struct[@type="coldfusion.server.ConfigMap"]/var[@name="CLASS"]/string
- that's probably overkill though :))
Note - I've fixed your XML - I'm assuming that's a typographic error, rather than broken source XML. If your source XML is broken, then you have bigger problems.
This outputs:
<?xml version="1.0"?>
<wddxPacket version="1.0">
<header/>
<data>
<array length="2">
<struct type="coldfusion.server.ConfigMap">
<var name="CFASTSTJ">
<struct type="coldfusion.server.ConfigMap">
<var name="alter">
<boolean value="true" />
</var>
<var name="CLASS">
<string>NoDatabase</string>
</var>
</struct>
</var>
</struct>
</array>
</data>
</wddxPacket>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…