Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

How to extract particular value from xml file and modify it using perl?

Here is the xml code where I want to extract 'string' value macromedia.jdbc.MacromediaDriver and modify it as NoDatabase using perl.

File name- neo-datasource.xml

<?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>
           </struct>
         </array>
       </data>      
</wddxpacket>   

Please anyone can share your ideas or perl script.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...