I am trying to add child nodes under a root node .. I tried out with below xml but this doesn't work. I am newbie to Ruby and Nokogiri
builder = Nokogiri::XML::Builder.with(@doc) do |xml|
nodes = Nokogiri::XML::NodeSet.new(@doc, [])
[].each {|nodes_one_by_one|
<< nodes_one_by_one.Book
<< nodes_one_by_one.Pen
}
end
I need to add nodes below a root node like this
<Catalog>
<Book>abc</Book>
<Book_Author>Benjamin</Book_author>
That works good for me .. but what i exactly need is to add these Nodes after a specific position in the doc.
<Catalog>
<!--
<Book>abc</Book>
<Book_Author>Benjamin</Book_author>
-->
<Interface></Interface>
<Dialog></Dialog>
<Manifest></Manifest>
</Catalog>
i tried it with at_xpath('//Catlog') but it is adding at the end of the element (as you said)
<Catalog>
<Interface></Interface>
<Dialog></Dialog>
<Manifest></Manifest>
<!--
<Book>abc</Book>
<Book_Author>Benjamin</Book_author>
-->
book = Nokogiri::XML::Node.new('book', doc)
pen = Nokogiri::XML::Node.new('pen', doc)
.
.
Is there anyway to loop using "each" instead of adding one by one .. I tried this way but that doesn't work.
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 << nodes
The code might be wrong, but i want to do this way. One more this is like .. can i add all the elements as NodeSet instead of Node. Please correct my code.
thanks in advance
Nested OpenStruct doesn't seem to be working fine. I tried with an example below
Catalog collection of Store
require 'ostruct' require 'nokogiri' collection = [ OpenStruct.new(:catalogStoreNumber => '657758', :catalogStoreId => 'CTH6536', :catalogStoreLocation => 'UnitedStates', :catalogOwnerId => 'TYCT11190', :catalogOwner => 'McGrawHill Pub.', :catalogList => OpenStruct.new( :catalogProductInfo => OpenStruct.new( :productType => 'Book', :productName => 'The Client', :productId => 'CRSUS113246A', :productCategory => 'Crime And Suspense', :productSubcategory => 'Vintage Books', :productPrice => '45.50 USD', :productAuthor => OpenStruct.new( :authorFirstName =>'John Grisham', :authorMiddleName=> 'Willburt', :authorContact => '19876648981')), :catalogProductInfo => OpenStruct.new( :productType => 'Pen', :productName => 'Reynolds', :productId => 'PRREY546647', :productCategory => 'Misc. Stationary', :productSubcategory => 'Stationery Items', :productPrice => '3.00 USD', :productManufacturer => 'Reynolds Inc.', :productAuthor => OpenStruct.new( :authorFirstName => 'Ryan Reynolds', :authorMiddleName => 'William', :authorContact => '16577589898')), :catalogListType => 'ProductCollection', :catalogListSource => 'Web' ), :catalogVerificaitionLog => OpenStruct.new( :catalogVerificationStatus => 'Verified', :catalogVerificationDateTime => '2012-03-12T13:00:15+5:30', :catalogVerificationId => '64774A', :catalogVerificationRequestedBy => 'user_121') )]
i want to access the "productType" of first "catalogProductInfo" and i said "collection.catalogList.catalogProductInfo.productType.content" and I get the below error
"undefined method `productType' for # (NoMethodError)
Does OpenStruct has the support for the Nested OpenStruct I Want to construct an XML in the below format using the OpenStruct and Nokogiri ?
<CatalogOrder>
<CatalogStoreNumber>657758</CatalogStoreNumber>
<CatalogStoreId>CTH6536</CatalogStoreId>
<CatalogStoreLocation>UnitedStates</CatalogStoreLocation>
<CatalogOwnerId>TYCT11190</CatalogOwnerId>
<CatalogOwner>McGrawHill Pub.</CatalogOwner>
<CatalogList>
<CatalogProductInfo>
<ProductType>Book</ProductType>
<ProductName>The Client</ProductName>
<ProductId>CRSUS113246A</ProductId>
<ProductCategory>Crime And Suspense</ProductCategory>
<ProductSubCategory>Vintage Books</ProductSubCategory>
<ProductPrice>45.50 USD</ProductPrice>
<ProductAuthor>
<AuthorFirstName>John Grisham</AuthorFirstName>
<AuthorMiddleName>Willbur</AuthorMiddleName>
<AuthorContact>19876648981</AuthorContact>
</ProductAuthor>
</CatalogProductInfo>
<CatalogProductInfo>
<ProductType>Pen</ProductType>
<ProductName>Reynolds</ProductName>
<ProductId>PRREY546647</ProductId>
<ProductCategory>Misc. Stationary</ProductCategory>
<ProductSubCategory>Stationary Items</ProductSubCategory>
<ProductPrice>3.00 USD</ProductPrice>
<ProductAuthor>
<AuthorFirstName>Ryan Reynolds</AuthorFirstName>
<AuthorMiddleName>William</AuthorMiddleName>
<AuthorContact>16577589898</AuthorContact>
</ProductAuthor>
</CatalogProductInfo>
<CatalogListType>ProductCollection</CatalogListType>
<CatalogListSource>Web</CatalogListSource>
</CatalogList>
<CatalogVerificationLog>
<CatalogVerificationStatus>Verified</CatalogVerificationStatus>
<CatalogVerificationDateTime>2012-03-12T13:00:15+5:30</CatalogVerificationDateTime>
<CatalogVerificationId>64774A</CatalogVerificationId>
<CatalogVerificationRequestedBy>User_121</CatalogVerificationRequestedBy>
</CatalogVerificationLog>
I want to achieve 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 achieve this without any outstanding limitations ? Please correct my earlier code.