【问题标题】:Removing parent nodes from xml file in r从r中的xml文件中删除父节点
【发布时间】:2020-06-11 14:52:43
【问题描述】:

我在下面有 xml 输出。我正在尝试使用 R 中的 XML 或 xml2 库删除父母。我发现了类似的帖子here,但这些函数不知何故给了我一个错误。我正在尝试删除这些父母,直到 analysisdata 父母。这就是我要解析的内容。

<assessment>
  <itembank />
  <instrument id="797393  ">
    <form id="Math G3">
      <formitem id="8243" position="0" />

    </form>
  </instrument>
  <admin id="797393  " instrument="797393  ">
    <genatt name="adminperiod" value="SP19" />
    <responseanalyses>
      <responsebankref id="3M" />
      <analysisdefs>
        <analysisoption name="mark" value="no" />
        <analysisoption name="notreached" value="ASNOTPRESENTED" />
        <statentity type="item" id="8243" varname="I_8243">
          <genatt name="type" value="CR" />
          <genatt name="codestart" value="0" />
          <genatt name="validcodes" value="2" />
          <genatt name="omitcode" value="9" />
          <genatt name="notpresentedcode" value="8" />
          <statentity code="0.000000" id="0.000000" type="itemscorept" />
          <statentity code="1.000000" id="1.000000" type="itemscorept" />
          <statentity code="9" id="omit" type="itemscorept" />
        </statentity>

          <genatt name="type" value="CR" />
          <genatt name="codestart" value="0" />
          <genatt name="validcodes" value="2" />
          <genatt name="omitcode" value="9" />
          <genatt name="notpresentedcode" value="8" />
          <statentity code="0.000000" id="0.000000" type="itemscorept" />
          <statentity code="1.000000" id="1.000000" type="itemscorept" />
          <statentity code="9" id="omit" type="itemscorept" />
        </statentity>
        <statentity id="DIFGROUP_TTS" varname="DIFGROUP_TTS" type="popgroup" description="DIFGROUP_TTS">
          <statentity code="0.000000" id="0.000000" description="0.000000" samplesize="40501" type="popsubgroup" />
          <statentity code="1.000000" id="1.000000" description="1.000000" samplesize="5532" type="popsubgroup" />
        </statentity>

      </analysisdefs>
      <genatt name="xmloutputfile" value="J:\out.xml" />
      <analysis creator="ST" description="" level="form" type="classical">
    <analysisdata formid="Math G3">

          <statentityref id="8243" type="item">
            <statval type="OmitCount" value="0.000000" />
            <statval type="NotReachedCount" value="43.000000" />
            <statval type="PolyserialCorrelation" value="0.644181" />
            <statval type="AdjustedPolyserial" value="0.644181" />
            <statval type="AverageScore" value="0.223074" se="0.011957" />
            <statval type="NCount" value="2609.000000" />
            <statval type="StdevItemScore" value="0.416308" />
              </statentityref>

            </statentityref>
          </statentityref>
        </analysisdata>
      </analysis>
    </responseanalyses>
    <responsebank findin="J:\outAM.am" sourcetype="AM" id="3M" />
  </admin>
</assessment>

我试过了 库(xml2) 库(XML) newdoc &lt;- xpathApply(doc, "/assessment/admin/responseanalyses") 运行后出现此错误:Error in UseMethod("xpathApply") : no applicable method for 'xpathApply' applied to an object of class "c('xml_document', 'xml_node')"。这个解决方案被建议here

删除这些父节点后,我希望仅在删除评估/管理/响应分析父节点后拥有analysisdata 节点。想要的出来是这样的:

<analysisdata formid="Math G3">
  <statentityref id="8243" type="item">
    <statval type="OmitCount" value="0.000000" />
    <statval type="NotReachedCount" value="43.000000" />
    <statval type="PolyserialCorrelation" value="0.644181" />
    <statval type="AdjustedPolyserial" value="0.644181" />
    <statval type="AverageScore" value="0.223074" se="0.011957" />
    <statval type="NCount" value="2609.000000" />
    <statval type="StdevItemScore" value="0.416308" />
      </statentityref>

    </statentityref>
  </statentityref>
</analysisdata>

有什么想法吗? 谢谢!

【问题讨论】:

  • 解压节点即可。例如rvest::html_node(doc, 'analysisdata')
  • 这是我的回答。感谢您的建议:) 如果您发布此内容,我将接受正确的解决方案。

标签: r xml parsing


【解决方案1】:

您可以使用 rvest 库中的 html_node

 rvest::html_node(doc, 'analysisdata')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2023-03-24
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多