title: How to add child nodes in NodeSet using Nokogiri
body: <p>I am trying to add child nodes under a root node. I tried it with the following XML but it doesn't work.</p>
  
  <pre class="lang-ruby prettyprint-override"><code>builder = Nokogiri::XML::Builder.with(@doc) do |xml|   
    nodes = Nokogiri::XML::NodeSet.new(@doc, [])   
    [].each {|nodes_one_by_one|  
      &lt;&lt; nodes_one_by_one.Book  
      &lt;&lt; nodes_one_by_one.Pen 
    }      
  end  
  </code></pre>
  
  <p>I need to add nodes below a root node like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Catalog&gt;
      &lt;Book&gt;abc&lt;/Book&gt;
      &lt;Book_Author&gt;Benjamin&lt;/Book_author&gt;
  &lt;/Catalog&gt;
  </code></pre>
  
  <p>That works for me, but I to add these Nodes after a specific position in the document:</p>
  
  <pre><code>&lt;Catalog&gt;
      &lt;!-- 
      &lt;Book&gt;abc&lt;/Book&gt;
      &lt;Book_Author&gt;Benjamin&lt;/Book_author&gt;
      --&gt;
      &lt;Interface&gt;&lt;/Interface&gt;
      &lt;Dialog&gt;&lt;/Dialog&gt;
      &lt;Manifest&gt;&lt;/Manifest&gt;
  &lt;/Catalog&gt;
  </code></pre>
  
  <p>I tried it with <code>at_xpath('//Catlog')</code> but it is adding it at the end of the element:</p>
  
  <pre><code> &lt;Catalog&gt;
      &lt;Interface&gt;&lt;/Interface&gt;
      &lt;Dialog&gt;&lt;/Dialog&gt;
      &lt;Manifest&gt;&lt;/Manifest&gt;
       &lt;!-- 
        &lt;Book&gt;abc&lt;/Book&gt;
        &lt;Book_Author&gt;Benjamin&lt;/Book_author&gt;
       --&gt;
   &lt;/Catalog&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>book = Nokogiri::XML::Node.new('book', doc)
  pen  = Nokogiri::XML::Node.new('pen', doc)     
  .
  .
  </code></pre>
  
  <p>Is there a way to loop using <code>each</code> instead of adding them one by one. I tried this way but that doesn't work:</p>
  
  <pre><code>builder = Nokogiri::XML::Builder.with(doc) do |xml|   
      nodes = Nokogiri::XML::Node.new(doc, [])   
      [].each {|child_list_element|  
          child_list_element.Book  "value"
          child_list_element.Pen  "value" 
          child_list_element.Diary  "value"
          child_list_element.Pen_stand  "value"
          child_list_element.Pencil  "value"
          .
          .
          .        
  }      
  end  
  doc &lt;&lt; nodes
  </code></pre>
  
  <p>The code might be wrong, but I want to do this way. </p>
  
  <p>Also, can I add all the elements as a NodeSet instead of a Node.</p>
  
  <hr>
  
  <p>Nested OpenStruct doesn't seem to be working. I tried:</p>
  
  <p>Catalog collection of Store:</p>
  
  <pre><code>    require 'ostruct'
      require 'nokogiri'
  
      collection = [
      OpenStruct.new(:catalogStoreNumber =&gt; '657758',
      :catalogStoreId =&gt; 'CTH6536',
      :catalogStoreLocation =&gt; 'UnitedStates', 
      :catalogOwnerId =&gt; 'TYCT11190',
      :catalogOwner =&gt; 'McGrawHill Pub.',
      :catalogList =&gt; OpenStruct.new(
          :catalogProductInfo =&gt; OpenStruct.new(
        :productType =&gt; 'Book',
        :productName =&gt; 'The Client',
        :productId =&gt; 'CRSUS113246A',
        :productCategory =&gt; 'Crime And Suspense',
        :productSubcategory =&gt; 'Vintage Books',  
        :productPrice =&gt; '45.50 USD',
        :productAuthor =&gt; OpenStruct.new(
                  :authorFirstName =&gt;'John Grisham',
      :authorMiddleName=&gt; 'Willburt',
      :authorContact =&gt; '19876648981')),
    :catalogProductInfo =&gt; OpenStruct.new(
        :productType =&gt; 'Pen',
        :productName =&gt; 'Reynolds',
        :productId =&gt; 'PRREY546647',
        :productCategory =&gt; 'Misc. Stationary',
        :productSubcategory =&gt; 'Stationery Items',  
        :productPrice =&gt; '3.00 USD',
        :productManufacturer =&gt; 'Reynolds Inc.',
        :productAuthor =&gt; OpenStruct.new(
            :authorFirstName =&gt; 'Ryan Reynolds',
      :authorMiddleName =&gt; 'William',
      :authorContact =&gt; '16577589898')),
      :catalogListType =&gt; 'ProductCollection',
      :catalogListSource =&gt; 'Web'
      ),
      :catalogVerificaitionLog =&gt; OpenStruct.new(
          :catalogVerificationStatus =&gt; 'Verified',
    :catalogVerificationDateTime =&gt; '2012-03-12T13:00:15+5:30',
    :catalogVerificationId =&gt; '64774A',
    :catalogVerificationRequestedBy =&gt; 'user_121')
      )]
  </code></pre>
  
  <p>I want to access the <code>productType</code> of the first <code>catalogProductInfo</code> and I used</p>
  
  <pre><code>collection.catalogList.catalogProductInfo.productType.content
  </code></pre>
  
  <p>and I got this error:</p>
  
  <pre><code>undefined method `productType' for #&lt;Array:0x3057438&gt; (NoMethodError)
  </code></pre>
  
  <p>Does OpenStruct have the support for the nested OpenStruct I want to construct  XML in the following format using OpenStruct and Nokogiri?</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;CatalogOrder&gt;
    &lt;CatalogStoreNumber&gt;657758&lt;/CatalogStoreNumber&gt;
    &lt;CatalogStoreId&gt;CTH6536&lt;/CatalogStoreId&gt;
    &lt;CatalogStoreLocation&gt;UnitedStates&lt;/CatalogStoreLocation&gt;
    &lt;CatalogOwnerId&gt;TYCT11190&lt;/CatalogOwnerId&gt;
    &lt;CatalogOwner&gt;McGrawHill Pub.&lt;/CatalogOwner&gt;
    &lt;CatalogList&gt;
      &lt;CatalogProductInfo&gt;
        &lt;ProductType&gt;Book&lt;/ProductType&gt;
        &lt;ProductName&gt;The Client&lt;/ProductName&gt;                
        &lt;ProductId&gt;CRSUS113246A&lt;/ProductId&gt;
        &lt;ProductCategory&gt;Crime And Suspense&lt;/ProductCategory&gt;
        &lt;ProductSubCategory&gt;Vintage Books&lt;/ProductSubCategory&gt;
        &lt;ProductPrice&gt;45.50 USD&lt;/ProductPrice&gt;
        &lt;ProductAuthor&gt;
          &lt;AuthorFirstName&gt;John Grisham&lt;/AuthorFirstName&gt;
          &lt;AuthorMiddleName&gt;Willbur&lt;/AuthorMiddleName&gt;
          &lt;AuthorContact&gt;19876648981&lt;/AuthorContact&gt;
        &lt;/ProductAuthor&gt;
      &lt;/CatalogProductInfo&gt;
      &lt;CatalogProductInfo&gt;
        &lt;ProductType&gt;Pen&lt;/ProductType&gt;
        &lt;ProductName&gt;Reynolds&lt;/ProductName&gt;                
        &lt;ProductId&gt;PRREY546647&lt;/ProductId&gt;
        &lt;ProductCategory&gt;Misc. Stationary&lt;/ProductCategory&gt;
        &lt;ProductSubCategory&gt;Stationary Items&lt;/ProductSubCategory&gt;
        &lt;ProductPrice&gt;3.00 USD&lt;/ProductPrice&gt;
        &lt;ProductAuthor&gt;
          &lt;AuthorFirstName&gt;Ryan Reynolds&lt;/AuthorFirstName&gt;
          &lt;AuthorMiddleName&gt;William&lt;/AuthorMiddleName&gt;
          &lt;AuthorContact&gt;16577589898&lt;/AuthorContact&gt;
        &lt;/ProductAuthor&gt;
      &lt;/CatalogProductInfo&gt;
      &lt;CatalogListType&gt;ProductCollection&lt;/CatalogListType&gt;
      &lt;CatalogListSource&gt;Web&lt;/CatalogListSource&gt;
    &lt;/CatalogList&gt;
    &lt;CatalogVerificationLog&gt;
      &lt;CatalogVerificationStatus&gt;Verified&lt;/CatalogVerificationStatus&gt;
      &lt;CatalogVerificationDateTime&gt;2012-03-12T13:00:15+5:30&lt;/CatalogVerificationDateTime&gt;
      &lt;CatalogVerificationId&gt;64774A&lt;/CatalogVerificationId&gt;
      &lt;CatalogVerificationRequestedBy&gt;User_121&lt;/CatalogVerificationRequestedBy&gt;
    &lt;/CatalogVerificationLog&gt;
  &lt;/CatalogOrder&gt;
  </code></pre>
  
  <p>I want to do this using Nokogiri and OpenStruct, but I am not sure whether it is possible with OpenStruct, as it lacks nesting capabilities. Is there any other way to use JSON to accomplish this without any limitations?</p>