首页 > 解决方案 > 使用 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.&#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> 

标签: rxml

解决方案


只需通过 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.&#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")

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

removeAttributes(newdoc[[1]])

推荐阅读