Hi All,
I m facing an issue while parsing the XML file using REXML. I
really don't know either this is proper flow or not.
Here is my scenario,
I have created the REXML object for a XML file , using XPath.match
method I m retrieving values. I stored the results into a variable, then
doing further processing . When I used sub! function with any of those
variables, it affects the xml content too. see my example below.
My code snippet
* require 'rexml/document' include REXML # Opening xml
file in the REXML document xml_doc =
Document.new(File.open("sample.xml")) # => <UNDEFINED> ... </>
maths_score=XPath.match(xml_doc,"college/students/student/marks/mathematics/@value")
# => [value='42'] maths_score=maths_score.pop.to_s # => "42"
puts "Initial match score is #{maths_score}" puts "Going to assign new
value for maths score" maths_score="99"
maths_score=XPath.match(xml_doc,"college/students/student/marks/mathematics/@value")
# => [value='42'] puts "maths_score after update : #{maths_score}" #
No college_name=XPath.match(xml_doc,"college/@name") # => [name='ABC
College'] college_name=college_name.pop.to_s # => "ABC College"
puts "Initial college name : #{college_name}" puts "college name
variable class name : #{college_name.class}" # Performing the
substitution to remove the college. # I except that this changes would
happens only at string variable college_name # But it doesn't
college_name.sub!(/(.*)College/,'\1') # => "ABC " puts "After
substitution the college name is #{college_name}" # Now I tried
to get the original value in the xml file, org_college_name =
XPath.match(xml_doc,"college/@name") # => "ABC " # If the
substitution succeed the value gets overwritten into the XML content *
* puts "Original college name : #{org_college_name}"*
* # This update doesn't updated for the maths score. *
Output for my snippet :
*Initial match score is 42Going to assign new value for maths
scoremaths_score after update : [value='42']Initial college name : ABC
Collegecollege name variable class name : StringAfter substitution the
college name is ABC Original college name is [name='ABC ']Original maths
score is [value='42']*
XML file :
<?xml version="1.0" encoding="UTF-8" ?>
<college id="12" name="ABC College" address="12th ABC nagar,Chennai,India">
<students>
<student>
<name value="karthickraja"/>
<regno value="101425"/>
<marks>
<language value="90"/>
<second_language value="100"/>
<mathematics value="42"/>
<commerce value="89"/>
<total value="321"/>
</marks>
</student>
</students>
</college>
Here my doubt is ,
Is there any memory related problem , can any one explain whats
happening here.
P.S
My ruby version is 2.3.0p0 running on a debian jessie
Thanks in advance!