nokogiri在ruby中创建XML中的新值



这是我的XML:

<Instance ID="1" Type="CrossProtocol.CrossProtocolHost" NoDelay="False" KeepAlive="False" LocalIP="0.0.0.0" LocalPort="443" ServiceID="-1" MaxConnections="0" RemoteIP="127.0.0.1" RemotePort="443" CallBackPort="444" Description="HttpProxy" DesignatedIP="1.1.1.1" ProtocolName="SftpProxy" >
    <IPAddressRestriction Name="Restrict or grant access to services based on IP addresses" Type="0" Description="">
    </IPAddressRestriction>
  </Instance>

我想添加字段(节点?)

"ProtocolName="SftpProxy""后的CertificateSubject="/OU=Domain Control Validated/CN=bla.com",仅当RemotePort为443,CallBackPort为444

我试着用这个:

doc = Nokogiri::XML.parse(File.open("/opt/file/Config.xml"))
doc.xpath('//Instance/[@RemotePort = 443 and @CallBackPort = 444]').each do |node|
  CertPath = Nokogiri::XML::Node.new "CertificateSubject", doc
  CertPath.content = "/OU=Domain Control Validated/CN=bla.com"
  node.add_next_sibling(CertPath)
end

但是有错误,正确的方法是什么?

编辑

预期的XML应该是这样的:

<Instance ID="1" Type="CrossProtocol.CrossProtocolHost" NoDelay="False" KeepAlive="False" LocalIP="0.0.0.0" LocalPort="443" ServiceID="-1" MaxConnections="0" RemoteIP="127.0.0.1" RemotePort="443" CallBackPort="444" Description="HttpProxy" DesignatedIP="1.1.1.1" ProtocolName="SftpProxy" CertificateSubject="/OU=Domain Control Validated/CN=bla.com">
    <IPAddressRestriction Name="Restrict or grant access to services based on IP addresses" Type="0" Description="">
    </IPAddressRestriction>
  </Instance>

您基本上要添加属性,而不是Node

require 'nokogiri'
xml = <<-XML
<Instance ID="1" Type="CrossProtocol.CrossProtocolHost" NoDelay="False" KeepAlive="False" LocalIP="0.0.0.0" LocalPort="443" ServiceID="-1" MaxConnections="0" RemoteIP="127.0.0.1" RemotePort="443" CallBackPort="444" Description="HttpProxy" DesignatedIP="1.1.1.1" ProtocolName="SftpProxy" >
    <IPAddressRestriction Name="Restrict or grant access to services based on IP addresses" Type="0" Description="">
    </IPAddressRestriction>
  </Instance>
XML
doc = Nokogiri::XML(xml)
doc.xpath('//Instance[@RemotePort = 443 and @CallBackPort = 444]').each do |node|
  node['CertificateSubject'] =  "/OU=Domain Control Validated/CN=bla.com"
end
puts doc
# >> <?xml version="1.0"?>
# >> <Instance ID="1" Type="CrossProtocol.CrossProtocolHost" NoDelay="False" KeepAlive="False" LocalIP="0.0.0.0" LocalPort="443" ServiceID="-1" MaxConnections="0" RemoteIP="127.0.0.1" RemotePort="443" CallBackPort="444" Description="HttpProxy" DesignatedIP="1.1.1.1" ProtocolName="SftpProxy" CertificateSubject="/OU=Domain Control Validated/CN=bla.com">
# >>     <IPAddressRestriction Name="Restrict or grant access to services based on IP addresses" Type="0" Description="">
# >>     </IPAddressRestriction>
# >>   </Instance>

Nokogiri::XML::Node 属性可以像散列键值对一样访问。因此,有一个方法[]=(name, value),其工作方式为- 将属性名称的属性值设置为值

#each块中,node只是一个Nokogiri::XML::Node,因此您可以使用上面所说的[]=方法将新的属性/值添加到这个node

正如我在评论中提到的,这是您想要添加的属性,此外,您的xpath中有一个错误(您试图在不应该使用正斜杠的情况下下降节点)。有关添加属性的信息,请参阅这个问题。

doc.xpath('//Instance[@RemotePort = 443 and @CallBackPort = 444]').each do |node|
  node['CertificateSubject'] = "/OU=Domain Control Validated/CN=bla.com"  
end  
> doc.to_xml
#=> "<?xml version="1.0"?>n<Instance ID="1" Type="CrossProtocol.CrossProtocolHost" NoDelay="False" KeepAlive="False" LocalIP="0.0.0.0" LocalPort="443" ServiceID="-1" MaxConnections="0" RemoteIP="127.0.0.1" RemotePort="443" CallBackPort="444" Description="HttpProxy" DesignatedIP="1.1.1.1" ProtocolName="SftpProxy" CertificateSubject="/OU=Domain Control Validated/CN=bla.com">n    <IPAddressRestriction Name="Restrict or grant access to services based on IP addresses" Type="0" Description="">n    </IPAddressRestriction>n  </Instance>n"

相关内容

  • 没有找到相关文章

最新更新