Gewusst wie: hinzufügen von untergeordneten Knoten im NodeSet mit Nokogiri
Ich versuche, fügen Sie untergeordnete Knoten unter einem root-Knoten .. habe ich ausprobiert, darunter xml, aber das funktioniert nicht. Ich bin Neuling auf Ruby und 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
Ich muss hinzufügen von Knoten unterhalb eines root-Knoten wie dieser
<Catalog>
<Book>abc</Book>
<Book_Author>Benjamin</Book_author>
Funktioniert gut für mich .. aber was ich genau brauchen, ist, diese Knoten nach einer bestimmten position in der doc.
<Catalog>
<!--
<Book>abc</Book>
<Book_Author>Benjamin</Book_author>
-->
<Interface></Interface>
<Dialog></Dialog>
<Manifest></Manifest>
</Catalog>
versuchte ich es mit at_xpath('//Catlog'), aber es ist das hinzufügen am Ende des Elements (wie Sie sagte)
<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)
.
.
Gibt es irgendwie eine Schleife mit "jeder" anstatt eins nach dem anderen .. habe ich versucht aber das funktioniert nicht.
builder = Nokogiri::XML::generator.mit(doc) |xml|
Knoten = Nokogiri::XML::Node.neue(doc, [])
[].jede {|child_list_element|
child_list_element.Buch "Wert"
child_list_element.Pen "Wert"
child_list_element.Tagebuch "Wert"
child_list_element.Pen_stand "Wert"
child_list_element.Bleistift "Wert"
.
.
.
}
Ende
doc << Knoten
Dem code falsch sein könnte, aber ich wollen, das zu tun diese Art und Weise. Eine mehr dies ist wie .. kann ich all die Elemente, die als Knotenmenge anstelle von Knoten. Bitte korrigieren Sie meine-code.
vielen Dank im Voraus
-
Verschachtelte OpenStruct scheint nicht einwandfrei zu funktionieren. Ich habe versucht, mit einem Beispiel unten
Katalog Sammlung von 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') )]
möchte ich den Zugriff auf die "productType" ersten "catalogProductInfo" und ich sagte " - Kollektion.catalogList.catalogProductInfo."productType".Inhalt"
und ich bekomme die folgende Fehlermeldung
"undefined method" productType " für # (NoMethodError)
Tut OpenStruct hat die Unterstützung für die Geschachtelte OpenStruct ich Möchte erstellen Sie eine XML-Datei in der unter-format mit der OpenStruct und 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>
Möchte ich erreichen, dies mit Nokogiri und OpenStruct, aber ich bin nicht sicher, ob es möglich ist mit OpenStruct, denn es fehlt Nesting-Funktionen. Es ist eine andere Art der Verwendung von JSon um dies zu erreichen, ohne jede offene Grenzen ? Bitte korrigieren Sie meine früheren code.
- Nokogiri::XML::generator ist gut für den Aufbau ein XML-Dokument von Grund auf, aber Sie bereits eine haben, hier arbeiten, so weit ich informiert bin gibt es keinen Grund, es zu benutzen.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Wenn ich verstehe Sie richtig, die folgenden sollte in etwa das, was du suchst:
Den
<<
Anhängen der Knoten kurz vor dem Ende des Elements.Update
Nachdem die aktualisierten Frage-und vereinfacht mit @Phrogz Vorschläge, das sollte erfüllen Ihre Anforderungen:
Update 2
Zur Iteration über eine Sammlung, fügen Sie Knoten dynamisch, und mit einer Knotenmenge, versuchen Sie Folgendes:
<<
kann auch annehmen, ein string, so können Sie einfach tun:catalog << "<Book>abc</Book>"
catalog.first_element_child.before("<Book>abc</Book>")
.