r - update value in xml field conditional upon values in a data frame -


i have xml file exported reference manager. here sample 2 entries.

<?xml version="1.0" encoding="utf-8" ?><xml><records><record><database name="mhealth.enl" path="/users/ericpgreen/dropbox/+projects/maternal mhealth/mhealth.enl">mhealth.enl</database><source-app name="endnote" version="17.4">endnote</source-app><rec-number>3843</rec-number><foreign-keys><key app="en" db-id="z2xx2a2rpatev4e22v152e5idp2s5tdprtwv">3843</key></foreign-keys><ref-type name="journal article">17</ref-type><contributors><authors><author><style face="normal" font="default" size="100%">vickery, c.</style></author></authors></contributors><titles><title><style face="normal" font="default" size="100%">maternal supplementation , birthweight [letter]</style></title><secondary-title><style face="normal" font="default" size="100%">lancet</style></secondary-title></titles><periodical><full-title><style face="normal" font="default" size="100%">lancet</style></full-title><abbr-1><style face="normal" font="default" size="100%">lancet</style></abbr-1></periodical><pages><style face="normal" font="default" size="100%">53</style></pages><volume><style face="normal" font="default" size="100%">341</style></volume><number><style face="normal" font="default" size="100%">8836</style></number><keywords><keyword><style face="normal" font="default" size="100%">maternal mortality</style></keyword><keyword><style face="normal" font="default" size="100%">infant mortality</style></keyword><keyword><style face="normal" font="default" size="100%">antenatal care</style></keyword><keyword><style face="normal" font="default" size="100%">pregnancy</style></keyword><keyword><style face="normal" font="default" size="100%">malnutrition</style></keyword><keyword><style face="normal" font="default" size="100%">low birth weight</style></keyword><keyword><style face="normal" font="default" size="100%">food supplementation</style></keyword><keyword><style face="normal" font="default" size="100%">women</style></keyword><keyword><style face="normal" font="default" size="100%">developing countries</style></keyword><keyword><style face="normal" font="default" size="100%">mortality</style></keyword><keyword><style face="normal" font="default" size="100%">population dynamics</style></keyword><keyword><style face="normal" font="default" size="100%">demographic factors</style></keyword><keyword><style face="normal" font="default" size="100%">population</style></keyword><keyword><style face="normal" font="default" size="100%">maternal health services</style></keyword><keyword><style face="normal" font="default" size="100%">maternal-child health services</style></keyword><keyword><style face="normal" font="default" size="100%">primary health care</style></keyword><keyword><style face="normal" font="default" size="100%">health services</style></keyword><keyword><style face="normal" font="default" size="100%">delivery of health care</style></keyword><keyword><style face="normal" font="default" size="100%">health</style></keyword><keyword><style face="normal" font="default" size="100%">reproduction</style></keyword><keyword><style face="normal" font="default" size="100%">nutrition disorders</style></keyword><keyword><style face="normal" font="default" size="100%">diseases</style></keyword><keyword><style face="normal" font="default" size="100%">birth weight</style></keyword><keyword><style face="normal" font="default" size="100%">body weight</style></keyword><keyword><style face="normal" font="default" size="100%">physiology</style></keyword><keyword><style face="normal" font="default" size="100%">biology</style></keyword><keyword><style face="normal" font="default" size="100%">nutrition programs</style></keyword></keywords><dates><year><style face="normal" font="default" size="100%">1993</style></year><pub-dates><date><style face="normal" font="default" size="100%">1993 jan 2</style></date></pub-dates></dates><accession-num><style face="normal" font="default" size="100%">079558</style></accession-num><abstract><style face="normal" font="default" size="100%">dr. garner , colleagues ...garner et al. discuss. whether village women convinced remains seen. (full text) (4 references cited in original document)</style></abstract><urls></urls><custom1><style face="normal" font="default" size="100%">eg</style></custom1><custom2><style face="normal" font="default" size="100%">0</style></custom2><remote-database-name><style face="normal" font="default" size="100%">popline</style></remote-database-name><language><style face="normal" font="default" size="100%">english</style></language></record><record><database name="mhealth.enl" path="/users/ericpgreen/dropbox/+projects/maternal mhealth/mhealth.enl">mhealth.enl</database><source-app name="endnote" version="17.4">endnote</source-app><rec-number>3837</rec-number><foreign-keys><key app="en" db-id="z2xx2a2rpatev4e22v152e5idp2s5tdprtwv">3837</key></foreign-keys><ref-type name="report">27</ref-type><contributors><authors><author><style face="normal" font="default" size="100%">ngallaba, s.</style></author><author><style face="normal" font="default" size="100%">kapiga, s. h.</style></author><author><style face="normal" font="default" size="100%">ruyobya, i.</style></author><author><style face="normal" font="default" size="100%">boerma, j. t.</style></author></authors></contributors><titles><title><style face="normal" font="default" size="100%">tanzania demographic , health survey 1991/1992</style></title></titles><pages><style face="normal" font="default" size="100%">xviii, 306 p.</style></pages><keywords><keyword><style face="normal" font="default" size="100%">government publication</style></keyword><keyword><style face="normal" font="default" size="100%">tables , charts</style></keyword><keyword><style face="normal" font="default" size="100%">demographic , health surveys</style></keyword><keyword><style face="normal" font="default" size="100%">households</style></keyword><keyword><style face="normal" font="default" size="100%">fertility</style></keyword><keyword><style face="normal" font="default" size="100%">family planning</style></keyword><keyword><style face="normal" font="default" size="100%">fertility determinants</style></keyword><keyword><style face="normal" font="default" size="100%">family size desired</style></keyword><keyword><style face="normal" font="default" size="100%">maternal-child health services</style></keyword><keyword><style face="normal" font="default" size="100%">child nutrition</style></keyword><keyword><style face="normal" font="default" size="100%">infant nutrition</style></keyword><keyword><style face="normal" font="default" size="100%">aids</style></keyword><keyword><style face="normal" font="default" size="100%">health services</style></keyword><keyword><style face="normal" font="default" size="100%">statistics</style></keyword><keyword><style face="normal" font="default" size="100%">men</style></keyword><keyword><style face="normal" font="default" size="100%">developing countries</style></keyword><keyword><style face="normal" font="default" size="100%">demographic surveys</style></keyword><keyword><style face="normal" font="default" size="100%">population dynamics</style></keyword><keyword><style face="normal" font="default" size="100%">demographic factors</style></keyword><keyword><style face="normal" font="default" size="100%">population</style></keyword><keyword><style face="normal" font="default" size="100%">family , household</style></keyword><keyword><style face="normal" font="default" size="100%">sociocultural factors</style></keyword><keyword><style face="normal" font="default" size="100%">family size</style></keyword><keyword><style face="normal" font="default" size="100%">family characteristics</style></keyword><keyword><style face="normal" font="default" size="100%">primary health care</style></keyword><keyword><style face="normal" font="default" size="100%">delivery of health care</style></keyword><keyword><style face="normal" font="default" size="100%">health</style></keyword><keyword><style face="normal" font="default" size="100%">nutrition</style></keyword><keyword><style face="normal" font="default" size="100%">hiv infections</style></keyword><keyword><style face="normal" font="default" size="100%">viral diseases</style></keyword><keyword><style face="normal" font="default" size="100%">diseases</style></keyword><keyword><style face="normal" font="default" size="100%">research methodology</style></keyword></keywords><dates><year><style face="normal" font="default" size="100%">1993</style></year></dates><publisher><style face="normal" font="default" size="100%">dar es salaam, tanzania, bureau of statistics, 1993 jun.</style></publisher><accession-num><style face="normal" font="default" size="100%">084094</style></accession-num><abstract><style face="normal" font="default" size="100%">the complete results...knowledge of contraception increases age.</style></abstract><notes><style face="normal" font="default" size="100%">demographic , health surveys [dhs]</style></notes><urls><related-urls><url><style face="normal" font="default" size="100%">http://www.measuredhs.com</style></url></related-urls></urls><custom1><style face="normal" font="default" size="100%">eg</style></custom1><custom2><style face="normal" font="default" size="100%">0</style></custom2><remote-database-name><style face="normal" font="default" size="100%">popline</style></remote-database-name><language><style face="normal" font="default" size="100%">english</style></language></record> 

the first record identified <rec-number>3843</rec-number>. second record number 3837.

the records contain <custom2><style face="normal" font="default" size="100%">0</style></custom2>. value set 0 in every record.

i have data frame following type of info:

dat <- data.frame(id=c(1021, 3837, 3843),                    include=c(0, 1, 0)) dat #    id include #1 1021       0 #2 3837       1 #3 3843       0 

i want loop through every row in dat , update value of custom2 in xml particular id if include==1. need keep file in xml syntax can import updated version reference manager software.

any thoughts? subset dat include==1 each id in sequence...then replace next instance of <custom2><style face="normal" font="default" size="100%">0< <custom2><style face="normal" font="default" size="100%">1<...

aye. missing </records></xml>. should work (assuming valid xml in so.xml:

library(xml) library(pbapply) # free progress bars  doc <- xmlparse("mhealth.xml")  dat <- source("dat.r")$value  # iterate on data frame vs xml file  invisible(pblapply(1:nrow(dat), function(i) {    # target rec num & include value     rec_no <- as.character(dat[i, "id"])   new_val <- dat[i, "include"]    # node set   node_set <- getnodeset(doc, sprintf("//rec-number[text()=%s]/../custom2/style", rec_no))    # node (doing way makes easier target errors)   node <- node_set[[1]]    # update value   xmlvalue(node) <- new_val  }))  savexml(doc, "mhealth_updated.xml") 

you may want null (etc) checking, can see works.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -