ruby - Creating XML nodes using Nokogiri -


I'm having trouble creating nodes and adding them to XML files:

  Lt; Mainnode & gt; & Lt; Secnode & gt; & Lt; Data1 & gt; & Lt; / Data2 & gt; & Lt; Data2 & gt; & Lt; / Data2 & gt; & Lt; / Secnode & gt; & Lt; / Mainnode & gt;  

I want to add to the file like this:

  & lt; Mainnode & gt; & Lt; Secnode & gt; & Lt; Data1 & gt; & Lt; / Data2 & gt; & Lt; Data2 & gt; & Lt; / Data2 & gt; & Lt; / Secnode & gt; & Lt; Secnode & gt; & Lt; Data1 & gt; & Lt; / Data2 & gt; & Lt; Data2 & gt; & Lt; / Data2 & gt; & Lt; / Secnode & gt; & Lt; / Mainnode & gt;  

I'm having trouble with the concept of adding nodes with nokia.

This is my current code:

  def openXML f = File.open ("file.xml") doc = Nokogiri :: XML (f) end DRP parseXML mainnode .name = 'mainnode' f = openXML temp = Nokogiri :: XML :: Node.new "secnode", f menonod.add_next_sibling (temporary) end  

What concepts I'm missing ?

Send me & lt; Data1 & gt; and & lt; Data2 & gt; is required to be able to add an example variable, but I found the Nokogiri tutorial very helpful in this area and it is only & lt; Secnode & gt; node to & lt; Mainnode & gt; Created as a child of , the past is not created.

Any help is appreciated.

Adding nodes in the nocogy is very simple than you think, but your question is not very clear.

If you want to do an existing node duplicate :

  requires 'nokogiri' xml = & lt; & Lt; EOT & LT; Mainnode & gt; & Lt; Secnode & gt; & Lt; Data1 & gt; & Lt; / Data2 & gt; & Lt; Data2 & gt; & Lt; / Data2 & gt; & Lt; / Secnode & gt; & Lt; / Mainnode & gt; EOT doc = Nokogiri :: XML (xml) secnode = doc.at ('secnode') doc.root.add_child secnode.dup puts doc.to_xml  

Which, after running, Results in:

  & lt ;? XML version = "1.0"? & Gt; & Lt; Mainnode & gt; & Lt; Secnode & gt; & Lt; Data1 / & gt; & Lt; Data2 / & gt; & Lt; / Secnode & gt; & Lt; Secnode & gt; & Lt; Data1 / & gt; & Lt; Data2 / & gt; & Lt; / Secnode & gt; & Lt; / Mainnode & gt;  

Funky alignment is the result of adding interfaces to the text nodes used for indentation. As a result, XML is valid.

If you are adding a different set of nodes, then it is still easier:

'nockery' is required

  xml = & Lt; & Lt; EOT & lt; Mainnode & gt; & Lt; Secnode & gt; & Lt; Data1 & gt; & Lt; / Data2 & gt; & Lt; Data2 & gt; & Lt; / Data2 & gt; & Lt; / Secnode & gt; & Lt; / Mainnode & gt; EOT node_to_add = & lt; & Lt; EOT & lt; Seconode & gt; & Lt; Data3 / & gt; & Lt; Data4 / & gt; & Lt; / Secnode & gt; EOT doc = Nokogiri :: XML (xml) doc.root.add_child node_to_add puts doc.to_xml  

Which Output:

  & lt ;? Xml version =? "1.0" & gt; & Lt; Mainnode & gt; & Lt; Secnode & gt; & Lt; Data1 / & gt; & Lt; Data2 / & gt; & Lt; / Secnode & gt; & Lt; Secnode & gt; & Lt; Data3 / & gt; & Lt; Data4 / & gt; & Lt; / Secnode & gt; & Lt; / Mainnode & gt;  

You can use it as a template:

  Required 'nokogiri' xml = & lt; & Lt; EOT & lt; Mainnode & gt; & Lt; Secnode & gt; & Lt; Data1 & gt; & Lt; / Data2 & gt; & Lt; Data2 & gt; & Lt; / Data2 & gt; & Lt; / Secnode & gt; & Lt; / Mainnode & gt; EOT v1 = 'foo' v2 = 'bar' node_to_add = & lt; & Lt; EOT & lt; Secondnode & gt; & Lt; Data 3 & gt; # {V1} & lt; / Data3 & gt; & Lt; Data4 & gt; # {V2} & lt; / Data4 & gt; & Lt; / Secnode & gt; EOT doc = Nokogiri :: XML (xml) doc.root.add_child node_to_add puts doc.to_xml  

what looks:

  & lt ;? Xml version? = "1.0" & gt; & Lt; Mainnode & gt; & Lt; Secnode & gt; & Lt; Data1 / & gt; & Lt; Data2 / & gt; & Lt; / Secnode & gt; & Lt; Secnode & gt; & Lt; Data3 & gt; Foo & lt; / Data3 & gt; & Lt; Data4 & gt; Bar & lt; / Data4 & gt; & Lt; / Secnode & gt; & Lt; / Mainnode & gt;  

Nokogiri makes it very easy to create nodes using HTML or string representation of HTML, which converts it into flying later.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -