Forum: Ruby Remove comment block in xml

Cb3d13b4ac0029d800745ae1216bc333?d=identicon&s=25 Nonto Mb (nonto_s)
on 2014-07-24 15:12
Hi Everyone,

I'm required to write a ruby script that can edit an xml file, this
script must uncomment a block of the xml document.

jetty.xml

 <Configure id="Server" class="org.eclipse.jetty.server.Server">

    <!-- =========================================================== -->
    <!-- Server Thread Pool                                          -->
    <!-- =========================================================== -->
    <Set name="ThreadPool">
      <!-- Default queued blocking threadpool -->
      <New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
        <Set name="minThreads">10</Set>
        <Set name="maxThreads">10000</Set>
        <Set name="detailedDump">false</Set>
      </New>
    </Set>

    <!-- =========================================================== -->
    <!-- Set connectors                                              -->
    <!-- =========================================================== -->

      <!--
        <Call name="addConnector">
          <Arg>
              <New
class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <Set name="host"><SystemProperty name="jetty.host"
/></Set>
                <Set name="port"><SystemProperty name="jetty.port"
default="8983"/></Set>
                <Set name="maxIdleTime">50000</Set>
                <Set name="Acceptors">2</Set>
                <Set name="statsOn">false</Set>
                <Set name="confidentialPort">8443</Set>
          <Set name="lowResourcesConnections">5000</Set>
          <Set name="lowResourcesMaxIdleTime">5000</Set>
              </New>
          </Arg>
        </Call>
      -->
    <Call name="addConnector">
  <Arg>
      <New class="org.eclipse.jetty.server.bio.SocketConnector">
        <Set name="host"><SystemProperty name="jetty.host" /></Set>
        <Set name="port"><SystemProperty name="jetty.port"
default="8983"/></Set>
        <Set name="maxIdleTime">50000</Set>
        <Set name="lowResourceMaxIdleTime">1500</Set>
        <Set name="statsOn">false</Set>
      </New>
  </Arg>
</Call>

    </Configure>

How can i remove comments for this block only?

   <!--
        <Call name="addConnector">
          <Arg>
              <New
class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <Set name="host"><SystemProperty name="jetty.host"
/></Set>
                <Set name="port"><SystemProperty name="jetty.port"
default="8983"/></Set>
                <Set name="maxIdleTime">50000</Set>
                <Set name="Acceptors">2</Set>
                <Set name="statsOn">false</Set>
                <Set name="confidentialPort">8443</Set>
          <Set name="lowResourcesConnections">5000</Set>
          <Set name="lowResourcesMaxIdleTime">5000</Set>
              </New>
          </Arg>
        </Call>
      -->
on my ruby script i have the following code, when i run the code it
removes
all the comments and its content as well.
 require 'nokogiri'

file = File.read("jetty.xml")
xml = Nokogiri::XML(file)

#replace <!-- --> with a space
xml.xpath("//comment()").each do |node|
    node.content =node.content.gsub!(/(^\D\W[<!\-\-}]\W[\-\->])/,' ')
end

File.open("newjetty.xml","w") do |f|
    f.write xml.to_xml
end
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.