我有一个从引用管理器导出的xml文件。下面是一个包含两个条目的示例。
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* <?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 and 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 and colleagues ...Garner et al. discuss. Whether village women will be convinced remains to be 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 and 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 and Charts</style></keyword><keyword><style face="normal" font="default" size="100%">Demographic and 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 and 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 with age.</style></abstract><notes><style face="normal" font="default" size="100%">Demographic and 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>
*/第一个记录由<rec-number>3843</rec-number>标识。第二个记录号是3837。
这些记录还包含<custom2><style face="normal" font="default" size="100%">0</style></custom2>。该值在每个记录中都设置为0。
我有一个数据框架,其中包含以下类型的信息:
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我想循环遍历dat中的每一行,并更新custom2中特定id if include==1的值。我需要保持xml语法的文件,这样我可以将更新的版本导入到参考管理器软件中。
有什么想法吗?类似于子集dat到include==1,然后查找sequence...then中的每个id,用<custom2><style face="normal" font="default" size="100%">1<替换<custom2><style face="normal" font="default" size="100%">0<的下一个实例。
发布于 2015-08-19 17:49:52
好的。它只是错过了</records></xml>。这应该有效(假设一个有效的XML在so.xml中)。
library(XML)
library(pbapply) # free progress bars
doc <- xmlParse("mhealth.xml")
dat <- source("dat.R")$value
# iterate over data frame vs XML file
invisible(pblapply(1:nrow(dat), function(i) {
# get target rec num & include value
rec_no <- as.character(dat[i, "id"])
new_val <- dat[i, "include"]
# get the node set
node_set <- getNodeSet(doc, sprintf("//rec-number[text()=%s]/../custom2/style", rec_no))
# get the node (doing it this way makes it easier to target errors)
node <- node_set[[1]]
# update value
xmlValue(node) <- new_val
}))
saveXML(doc, "mhealth_updated.xml")您可能希望进行NULL (等)检查,但从我所看到的情况来看,这是可行的。
https://stackoverflow.com/questions/32101900
复制相似问题