使用 R XML 包删除 XML 中的父节点 [英] Remove parent node in XML Using R XML package

查看:30
本文介绍了使用 R XML 包删除 XML 中的父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用 R XML 库从 xml 文件中删除根元素

How can we remove root element from a xml file using R XML library

<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.&#160;&#160; 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.&#160;&#160; 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 保存到文件.下面显示等效调用:

Simply select the needed node by XPath then save to file with saveXML. Below show equivalent calls:

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.&#160;&#160; 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")

要删除顶级属性,请在保存前运行以下命令:

To remove the top level attributes, run below prior to saving:

removeAttributes(newdoc[[1]])

这篇关于使用 R XML 包删除 XML 中的父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆