r - 使用 R XML 包删除 XML 中的父节点
问题描述
我们如何使用 R XML 库从 xml 文件中删除根元素
<Result>
<Jobs id="1">
<Job ID="000000" PositionID="0000">
<Title>Development Manager - Investment Banking - Equities Business</Title>
<Summary><![CDATA[An experienced Development Manager with previous experience leading a small to mid-size team of developers in a Java/J2EE environment. A hands on role, you will be expected to manage and mentor a team of developers working on a mix of greenfield and maintenance projects.   My client, a well known investment bank, requires an experienced Development Manager to join their core technology team. This t]]></Summary>
<DateActive Date="2009-10-06T19:36:43-05:00">10/6/2009</DateActive>
<DateExpires Date="2009-11-05T20:11:34-05:00">11/5/2009</DateExpires>
<DateUpdated Date="2009-10-06 20:12:00">10/6/2009</DateUpdated>
<CompanyName>ABC Technology</CompanyName>
</Job>
</Jobs>
</Result>
所以,我想要输出如下
<Jobs>
<Job ID="000000" PositionID="0000">
<Title>Development Manager - Investment Banking - Equities Business</Title>
<Summary><![CDATA[An experienced Development Manager with previous experience leading a small to mid-size team of developers in a Java/J2EE environment. A hands on role, you will be expected to manage and mentor a team of developers working on a mix of greenfield and maintenance projects.   My client, a well known investment bank, requires an experienced Development Manager to join their core technology team. This t]]></Summary>
<DateActive Date="2009-10-06T19:36:43-05:00">10/6/2009</DateActive>
<DateExpires Date="2009-11-05T20:11:34-05:00">11/5/2009</DateExpires>
<DateUpdated Date="2009-10-06 20:12:00">10/6/2009</DateUpdated>
<CompanyName>ABC Technology</CompanyName>
</Job>
</Jobs>
所以,没有了
<Result></Result>
解决方案
只需通过 XPath 选择所需的节点,然后使用saveXML
. 下面显示等效调用:
newdoc <- xpathApply(doc, "/Result/Jobs") # OR getNodeSet(doc, "/Result/Jobs")
newdoc
# [[1]]
# <Jobs id="1">
# <Job ID="000000" PositionID="0000">
# <Title>Development Manager - Investment Banking - Equities Business</Title>
# <Summary><![CDATA[An experienced Development Manager with previous experience leading a small to mid-size team of developers in a Java/J2EE environment. A hands on role, you will be expected to manage and mentor a team of developers working on a mix of greenfield and maintenance projects.   My client, a well known investment bank, requires an experienced Development Manager to join their core technology team. This t]]> </Summary>
# <DateActive Date="2009-10-06T19:36:43-05:00">10/6/2009</DateActive>
# <DateExpires Date="2009-11-05T20:11:34-05:00">11/5/2009</DateExpires>
# <DateUpdated Date="2009-10-06 20:12:00">10/6/2009</DateUpdated>
# <CompanyName>ABC Technology</CompanyName>
# </Job>
# </Jobs>
# attr(,"class")
# [1] "XMLNodeSet"
saveXML(newdoc[[1]], file="Output.xml")
要删除顶级属性,请在保存之前运行以下命令:
removeAttributes(newdoc[[1]])
推荐阅读
- api - Paypal API 订阅 curl 沙箱目录产品 权限不足授权失败
- html - 输入文件未在 Android 11 的 FB 应用内浏览器中选择
- c - 端口 setrlimit RLIMIT_CPU 到 windows
- websocket - 我可以在 websphere 应用程序服务器中运行 wicket websocket 吗?
- xml - 尝试检查并替换我的 AIR 应用程序中使用的 xml 文件
- android - ConstraintLayout 中居中对齐的边距行为
- mediaelement.js - 安装 mediaelement.js - 怎么办?
- python - 无法弄清楚如何正确应用线程来阻止 tkinter 冻结
- javascript - 为什么 'google.script.run' 不能从 html body 'onload' 标签工作?
- memory - 对于计算程序,多少 RAM 才算太多?