recursion - Parsing XML in Ruby using a recursive function -
I am trying to change the data in the XML file below. I'm certain of one thing that the data & lt; String & gt;
will be in the tag, but can be nested to any extent in order to do this so that I come up with a recursive solution that looks like the right code, but there is no change.
Repeat in C or C ++ is easy because that change is easily reflected in the actual parameters but in Ruby, I am not able to understand how to reflect it.
Source XML:
& lt; Document & gt; & Lt; String id = "title" & gt; Continue without cable cord? & Lt; / String & gt; & Lt; String id = "bodytext" /> & Lt; String id = "f" & gt; This data is for 1 & lt; P & gt; & Lt; T & gt; This data is 2 & lt; / T> & Lt; / P & gt; This data is 3 & lt; / String & gt; & Lt; / Documents & gt;
Ruby code:
required 'nocagory' doc = nokia :: XML (file. Open ("d.xml") def def_func ( S) if s.child.class == Returns NilClass S and Array = s.children () array.each do | Element | If element == Nokogiri :: XML :: text s.content = s.content + "A" puts the other element = rec_func (element) "#########" element puts end-end s.children = Array # I have added this statement because I suspected whether changes in this array would show up in the parents or not. End of return doc.xpath ("// string"). Every do Node | K = rec_func puts the node "$$$$ $$$$$$$$$" end
, so someone recommends a change in the code to do this work.
Here you are with "// string" recursive method parse_children
Parsing every child. In this method I go through all the children and again for a specific child, I check that this is XML :: Text
. If so, this recursive ends (and do something with this lesson) If not, recursive call parse_children
with children of this element.
DERF parse_children (children) children.each do | Child | Case child when the nautygary :: XML :: text is child.content = child.content.strip + "A" when the Note: :: XML :: Element Pars_Child Child. Children End And Dock = Nochory :: XML Pars (XML) doctor .xpath ('// string'). Every do S | Parse_children s.children puts an end doc.to_xml
Comments
Post a Comment