Adding a nodeset to an existing Nokogiri::XML::Document

1,513 views
Skip to first unread message

daniele visaggio

unread,
Nov 26, 2013, 12:35:48 PM11/26/13
to nokogi...@googlegroups.com
Hi all,

I have the following nodeset:

#####################

<addOnModules>
  <addOnModule>
    <model>CKEM 36-Button Line Expansion Module</model>
    <index>1</index>
  </addOnModule>
  <addOnModule>
    <model>CKEM 36-Button Line Expansion Module</model>
    <index>2</index>
  </addOnModule>
  <addOnModule>
    <model>CKEM 36-Button Line Expansion Module</model>
    <index>3</index>
  </addOnModule>
</addOnModules>

#####################

I want to insert it inside an existing Nokogiri::XML::Document (as a child of <os> node), like the following:

#####################

doc = Nokogiri::XML::Document.parse <<-EOXML
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <ns:getOSVersionResponse xmlns:ns="http://www.cisco.com/AXL/API/8.5">
      <return>
        <os>
          <osName>Linux</osName>
          <osVersion>2.6.18-194.26.1.el5PAE</osVersion>
          <aarNeighborhoodName/>          
<addOnModules> <-- above nodeset should start here
...
</addOnModules>
</os>
</return> </ns:getOSVersionResponse> </soapenv:Body> </soapenv:Envelope> EOXML

#####################

I think I should use something like:

node_set = Nokogiri::XML::NodeSet.new(doc)

but then I'm stuck on how to proceed.

How can I do this?

Thank you in advance,

Daniele


Walter Lee Davis

unread,
Nov 27, 2013, 9:20:39 AM11/27/13
to nokogi...@googlegroups.com
First, you need to get a reference to the os element. This will work for that:

os = doc.at_css('os')

Next, get your snippet of XML into a DocumentFragment, so you can fling it around in Nokogiri:

elm = Nokogiri::XML::DocumentFragment([your string of XML above])

Finally, you simply add a new child to it with the node you created:

os.add_child(elm)

That should add it at the end of the os element. You have an empty <addOnModules> tag already in the first snippet, you could replace that in place or leave it out of the original template. To replace it, just change add_child() to replace(). Now if you may already have content in that element, and you want to merge contents, you will need to do a more granular approach, but this should get you started.

Walter

>
> --
> You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
> To post to this group, send email to nokogi...@googlegroups.com.
> Visit this group at http://groups.google.com/group/nokogiri-talk.
> For more options, visit https://groups.google.com/groups/opt_out.

Walter Lee Davis

unread,
Nov 27, 2013, 9:23:46 AM11/27/13
to nokogi...@googlegroups.com
Sorry, made a mistake here. To replace that element, you first need a reference to it:

modules = doc.at_css('os addOnModules')

Then you can replace it as I mentioned. If you followed my earlier message, you would replace the entire os block with the addOnModules.

Walter
Reply all
Reply to author
Forward
0 new messages