How to move sections of XML with Ruby and Nokogiri between XML files -


i have program stores configuration data in single xml file. program installed on 2 servers, production , dev. want move piece of xml file git repository , push dev production.

the xml file has section each project need section project i’m working on. here example:

<?xml version="1.0" encoding="utf-8"?> <timers>     <timer autostart="0" container="" container_pwd="" container_user="" log_active="info" log_size="25" maxjobs="10" maxlogsize="5000" name="orderimports" ownprocess="0" vmargs="">         <job date_repeat="0" day="0" description="send certainteed shipfile - once day" disable="0" fri="1" high_priority="0" hour="23" inthour="" intmin="" intsec="" jobid="66e6aa67-0ad0-4480-91ae-d17c3e769f4a" min="00" mon="1" month="0" runonce="0" sat="0" sun="0" thu="1" timeouthour="" timeoutmin="04" timeoutsec="" tohour="" tomin="" tue="1" wed="1" year="0">             <unit>order_imports.putcertainteedshipfile</unit>         </job>         <job date_repeat="0" day="0" description="send certainteed budgetfile - once day" disable="0" fri="1" high_priority="0" hour="22" inthour="" intmin="" intsec="" jobid="dca2f65a-08d8-4dd3-b663-ccaf4718c558" min="00" mon="1" month="0" runonce="0" sat="0" sun="0" thu="1" timeouthour="" timeoutmin="04" timeoutsec="" tohour="" tomin="" tue="1" wed="1" year="0">             <unit>order_imports.putcertainteedbudgetfile</unit>         </job>         <job date_repeat="0" day="0" description="send certainteed budgetinvoice file - once day" disable="0" fri="1" high_priority="0" hour="23" inthour="" intmin="" intsec="" jobid="12ccb8dd-1f06-4767-a86e-364fd5a4ff52" min="30" mon="1" month="0" runonce="0" sat="0" sun="0" thu="1" timeouthour="" timeoutmin="" timeoutsec="" tohour="" tomin="" tue="1" wed="1" year="0">             <unit>order_imports.putcertainteedbudgetinvoice</unit>         </job>         <job date_repeat="0" day="0" description="send certainteed shiproof file - once day" disable="0" fri="1" high_priority="0" hour="23" inthour="" intmin="" intsec="" jobid="6ba478a6-82aa-45c9-abb3-cde1429012fe" min="30" mon="1" month="0" runonce="0" sat="0" sun="0" thu="1" timeouthour="" timeoutmin="" timeoutsec="" tohour="" tomin="" tue="1" wed="1" year="0">             <unit>order_imports.putcertainteedshipfileroof</unit>         </job>     </timer>     <timer autostart="0" container="" container_pwd="" container_user="" log_active="info" log_size="25" maxjobs="10" maxlogsize="5000" name="cpc8" ownprocess="0" vmargs="">         <job date_repeat="0" day="0" description="cpc8" disable="0" fri="1" high_priority="0" hour="05" inthour="" intmin="02" intsec="" jobid="804dfb35-d5a6-4075-b55c-328cf556da7c" min="00" mon="1" month="0" runonce="0" sat="1" sun="1" thu="1" timeouthour="" timeoutmin="" timeoutsec="" tohour="01" tomin="30" tue="1" wed="1" year="0">             <unit>cpc8.cpc8avettimain</unit>         </job>         <job date_repeat="0" day="0" description="cpc82" disable="0" fri="1" high_priority="0" hour="05" inthour="" intmin="02" intsec="" jobid="66cbe9a2-d79a-4cfd-9f21-b1c72f04aaa5" min="00" mon="1" month="0" runonce="0" sat="1" sun="1" thu="1" timeouthour="" timeoutmin="" timeoutsec="" tohour="01" tomin="30" tue="1" wed="1" year="0">             <unit>cpc8.cpc8avettimain2</unit>         </job>         <job date_repeat="0" day="0" description="cpc8 inbound order shop" disable="0" fri="1" high_priority="0" hour="05" inthour="" intmin="05" intsec="" jobid="05fb0f4c-f175-4e10-a885-63d2aa07f2ef" min="00" mon="1" month="0" runonce="0" sat="1" sun="1" thu="1" timeouthour="" timeoutmin="" timeoutsec="" tohour="01" tomin="30" tue="1" wed="1" year="0">             <unit>cpc8.cpc8inboundlauncherdev2</unit>         </job>         <job date_repeat="0" day="0" description="cpc8 sync journal monitor" disable="0" fri="1" high_priority="0" hour="07" inthour="03" intmin="" intsec="" jobid="8bb67c1a-6935-4069-8c04-48d53b678d75" min="00" mon="1" month="0" runonce="0" sat="1" sun="1" thu="1" timeouthour="" timeoutmin="" timeoutsec="" tohour="22" tomin="" tue="1" wed="1" year="0">             <unit>cpc8.syncmonitor</unit>         </job>     </timer> 

this example contains 2 projects "orderimports" , "cpc8". need "orderimports" project, identified name attribute on timer node.

i used ruby script nokogiri parse relevant information xml file. i’m using to_xml output , saving file in git repo.

here code save text file. how restore node text file original file?

    def find_timers_by_name         timers = []         nodes = @timer_noko.xpath("timers/timer/job/unit[contains(text(), '#{@project_name}')]")         nodes.each |node|             timers << node.parent.parent.attribute("name")         end         return timers.uniq     end      def dump_timer_xml         timers = find_timers_by_name         timer_dir = "./int_config/timers"         fileutils.mkdir_p(timer_dir)          timers.each |timer|             nodes = @timer_noko.xpath("timers/timer[@name='#{timer}']")             nodes.each |node|                 timer_xml_file = file.join(timer_dir, "#{timer}.xml")                 tx = "\t#{node.to_xml}"                 file.open(timer_xml_file, 'w') { |file| file.write(tx) }             end         end     end 

i take step further , not replace node unless has changed or node not exist.

i need have script check see if project section exists in main xml file and, if so, replace 1 git repo. i’m hoping nokogiri has sort of built-in methods doing this. not need use to_xml if that’s not going work.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -