1

I'm required to write a script that can remove a commented block in xml file, and save it back to its directory.

    <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>

on this xml How can I comment out 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>
 -->  

I have tried it with this but

  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

this code only remove the text within the comment

Output:

     <!---->
        <!---->
        <!---->


 <Set name="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>

        <!---->
        <!---->
        <!---->

      <!---->

        <!---->
4
  • What is your question? Commented Jul 24, 2014 at 9:40
  • how can I uncomment a block in the xml Commented Jul 24, 2014 at 9:54
  • If you stuck to Nokogiri, you’d likely try to store node.content, then delete the entire comment node and add node.content there. Commented Jul 24, 2014 at 10:00
  • mudasobwa can you please give an example, I did use node.content Commented Jul 24, 2014 at 10:08

1 Answer 1

1

You should delete the node, as it is a comment node. You can use the inner text to parse it and add it again.

require 'nokogiri'

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

#replace <!-- --> with a space 
xml.xpath("//comment()").each do |node|
    t = Nokogiri::XML::DocumentFragment.parse(node.content)
    node.add_next_sibling(t)
    node.remove
end

File.open("newjetty.xml","w") do |f|
    f.write xml.to_xml
end

Here you are parsing the comments content, add it as the next sibling and remove the node itself.

This basically works, however the string-only contents are also added as nodes, making this a mixed content document, which you most certainly do not want for a jetty configuration file.

So there should also be some logic included that checks for the node type (text versus element) and only includes elements.

Sign up to request clarification or add additional context in comments.

7 Comments

are comments referred as node in xml?
Yes, comment is a node type
so does that mean a node returns a list of node types?
I don't know what you mean by that. How can a node (being an object) return anything? Also, I have no knowledge of Ruby (this was my first ruby snippet ever, I think), so I am really of no help at all if you ask specific stuff about ruby or nokogiri.
i was asking with regard to the code snippet you gave me, it doesn't work when i run my script. thanks for your help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.