I want to reprint the modified xml after deleting entire child node

103 views Asked by At
<product>
<book>
    <id>111</id>
    <name>xxx</name>
</book>
<pen>
    <id>222</id>
    <name>yyy</name>
</pen>
<pencil>
    <id>333</id>
    <name>zzz</name>
</pencil>

I want to remove the "pencil" node and print the remaining xml using REXML (Ruby). Can anybody tell me how to do that ?

1

There are 1 answers

1
Ernest On

By using one of the delete methods http://rubydoc.info/stdlib/rexml/

require "rexml/document"
string = <<EOF
  <product>
  <book>
      <id>111</id>
      <name>xxx</name>
  </book>
  <pen>
      <id>222</id>
      <name>yyy</name>
  </pen>
  <pencil>
      <id>333</id>
      <name>zzz</name>
  </pencil>
  </product>
EOF
doc = REXML::Document.new(string)
doc.delete_element('//pencil')
puts doc

There is also nice tutorial to get you started: http://www.germane-software.com/software/rexml/docs/tutorial.html