删除XMLRuby中的注释块
我需要编写一个脚本,可以删除xml文件中的注释块,并将其保存回其目录删除XMLRuby中的注释块,ruby,xml,Ruby,Xml,我需要编写一个脚本,可以删除xml文件中的注释块,并将其保存回其目录 <Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- =========================================================== --> <!-- Server Thread Pool
<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>
10
10000
假的
50000
1500
假的
在这个xml上
如何才能仅注释掉此块?
<!--
<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>
-->
我用这个试过了,但是
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
需要“nokogiri”
file=file.read(“jetty.xml”)
xml=Nokogiri::xml(文件)
#替换为空格
xpath(“//comment()”)。每个do |节点|
node.content=node.content.gsub!(/(^\D\W[])/,“”)
结束
File.open(“newjetty.xml”,“w”)do | f|
f、 将xml.xml写入xml
结束
此代码仅删除注释中的文本
输出:
<!---->
<!---->
<!---->
<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>
<!---->
<!---->
<!---->
<!---->
<!---->
10
10000
假的
您应该删除该节点,因为它是一个注释节点。您可以使用内部文本来解析它并再次添加它
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
需要“nokogiri”
file=file.read(“jetty.xml”)
xml=Nokogiri::xml(文件)
#替换为空格
xpath(“//comment()”)。每个do |节点|
t=Nokogiri::XML::DocumentFragment.parse(node.content)
节点。添加下一个兄弟节点(t)
node.remove
结束
File.open(“newjetty.xml”,“w”)do | f|
f、 将xml.xml写入xml
结束
在这里,您正在解析注释内容,将其添加为下一个同级,并删除节点本身
这基本上是可行的,但是只包含字符串的内容也被添加为节点,这使得这是一个混合内容文档,您肯定不希望将其作为jetty配置文件
因此,还应该包含一些逻辑来检查节点类型(文本与元素),并且只包括元素。您的问题是什么?我如何取消XML中的块注释如果您坚持使用
Nokogiri
,您可能会尝试存储node.content
,然后删除整个注释节点并添加node.content
there.mudasobwa您能举个例子吗,我确实使用了node.contentware注释在xml中称为节点吗?是的,注释是一种节点类型,那么这是否意味着节点返回一个节点类型列表?我不知道这是什么意思。节点(作为对象)如何返回任何内容?另外,我对Ruby一无所知(我想这是我有史以来第一个Ruby代码段),所以如果你问关于Ruby或nokogiri的具体问题,我真的没有任何帮助。我问的是关于你给我的代码段,当我运行脚本时它不起作用。谢谢你的帮助