I am having problems creating nodes and adding them to an XML file:
<mainnode>
<secnode>
<data1></data2>
<data2></data2>
</secnode>
</mainnode>
I want to be able to add to the file like so:
<mainnode>
<secnode>
<data1></data2>
<data2></data2>
</secnode>
<secnode>
<data1></data2>
<data2></data2>
</secnode>
</mainnode>
I am having trouble getting the concept of adding nodes with Nokogiri.
This is my current code:
def openXML
f = File.open("file.xml")
doc = Nokogiri::XML(f)
end
def parseXML
mainnode.name = 'mainnode'
f = openXML
temp = Nokogiri::XML::Node.new "secnode", f
mainnode.add_next_sibling(temp)
end
What concepts am I missing?
I need to be able to add instance variables to <data1>
and <data2>
but I found the Nokogiri tutorial to not be of much help in this area and have not made it past just adding the <secnode>
node as a child of <mainnode>
.
Any help is appreciated.
inst
? – Miracleinst
yet... ie there is no line of code that saysinst = <something>
– Miracleinst = temp.<get _node('mainnode')>
(where I don't know what theget_node
function is called, but there must be one) right? – Miraclemainnode = <something>
and it should be that line that is where you fetch the<mainnode>
section from the xml? So something likemainnode = f.<get_node('mainnode')>
? – Miraclemainnode = f.xpath('//mainnode/')
or similar? (note: not tested) – Miracle