So fügen Sie untergeordnete Knoten in NodeSet mit Nokogiri hinzu

Ich versuche, untergeordnete Knoten unter einem Stammknoten hinzuzufügen. Ich habe es unter xml ausprobiert, aber das funktioniert nicht. Ich bin ein Neuling bei Ruby und Nokogiri

<code>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  
</code>

Ich muss Knoten unter einem Stammknoten wie diesem hinzufügen

<code><Catalog>
    <Book>abc</Book>
    <Book_Author>Benjamin</Book_author>
</code>

Das funktioniert gut für mich .. aber was ich genau brauche, ist, diese Knoten nach einer bestimmten Position im Dokument hinzuzufügen.

<code><Catalog>
    <!-- 
    <Book>abc</Book>
    <Book_Author>Benjamin</Book_author>
    -->
    <Interface></Interface>
    <Dialog></Dialog>
    <Manifest></Manifest>
</Catalog>
</code>

Ich habe es mit at_xpath ('// Catlog') versucht, aber es wird am Ende des Elements hinzugefügt (wie Sie sagten)

<code> <Catalog>
    <Interface></Interface>
    <Dialog></Dialog>
    <Manifest></Manifest>
     <!-- 
      <Book>abc</Book>
      <Book_Author>Benjamin</Book_author>
     -->
</code>
<code>book = Nokogiri::XML::Node.new('book', doc)
pen  = Nokogiri::XML::Node.new('pen', doc)     
.
.
</code>

Gibt es überhaupt eine Möglichkeit, eine Schleife mit "each" durchzuführen, anstatt eine nach der anderen hinzuzufügen? Ich habe es auf diese Weise versucht, aber das funktioniert nicht.

builder = Nokogiri :: XML :: Builder.with (doc) do | xml |
node = 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". . .
}
Ende
doc << -Knoten

Der Code könnte falsch sein, aber ich möchte auf diese Weise tun. Ein weiteres ist wie .. Kann ich alle Elemente als NodeSet anstelle von Node hinzufügen. Bitte korrigieren Sie meinen Code.

Danke im Voraus

Geschachteltes OpenStruct scheint nicht richtig zu funktionieren. Ich habe es mit einem Beispiel unten versucht

Katalogsammlung des Speichers
<code>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')
)]
</code>

Ich möchte auf den "productType" des ersten "catalogProductInfo" zugreifen und habe "collection.catalogList.catalogProductInfo.productType.content" gesagt. Die folgende Fehlermeldung wird angezeigt

"undefinierte Methode" productType "für # (NoMethodError)

Unterstützt OpenStruct das geschachtelte OpenStruct? Ich möchte mit OpenStruct und Nokogiri ein XML im folgenden Format erstellen?

<code><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>
</code>

Ich möchte dies mit Nokogiri und OpenStruct erreichen, bin mir aber nicht sicher, ob dies mit OpenStruct möglich ist, da es keine Verschachtelungsmöglichkeiten gibt. Gibt es eine andere Möglichkeit, JSon zu verwenden, um dies ohne herausragende Einschränkungen zu erreichen? Bitte korrigieren Sie meinen früheren Code.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage