如何将xml树元素转换为树中另一个元素的子元素?

时间:2019-04-29 15:48:25

标签: python xml parsing

我正在尝试解析XML文件。因此,我需要操纵XML树的某些元素。我有一个与此类似的文件:

<Resultdata>
   <Houses>
      <passed_results/>
      <failed_results/>
      <House_1_Passed>
         <Bricks>20</Bricks>
         <Windows>1</Windows>
         <Tables>3<Tables>
      <House_1_Passed>
      <House_2_Passed>
         <Bricks>23</Bricks>
         <Windows>2</Windows>
         <Tables>1<Tables>
      <House_2_Passed>
      <House_3_Failed>
         <Bricks>30</Bricks>
         <Windows>0</Windows>
         <Tables>2<Tables>
      <House_3_Failed>
      <House_4_Passed>
         <Bricks>20</Bricks>
         <Windows>4</Windows>
         <Tables>3<Tables>
      <House_4_Passed>
   <Houses>
<Resultdata>

因此,我真正需要做的是从下面的XML树中剪切已通过和失败的测试用例,并将通过和失败的用例放在内,将失败的用例放在内。因此输出看起来像这样:

<Resultdata>
   <Houses>
      <passed_results>
         <House_1_Passed>
            <Bricks>20</Bricks>
            <Windows>1</Windows>
            <Tables>3<Tables>
         <House_1_Passed>
         <House_2_Passed>
            <Bricks>23</Bricks>
            <Windows>2</Windows>
            <Tables>1<Tables>
         <House_2_Passed>
         <House_4_Passed>
            <Bricks>20</Bricks>
            <Windows>4</Windows>
            <Tables>3<Tables>
         <House_4_Passed>
      <passed_results/>
      <failed_results>
         <House_3_Failed>
            <Bricks>30</Bricks>
            <Windows>0</Windows>
            <Tables>2<Tables>
         <House_3_Failed>
      <failed_results/>
   <Houses>
<Resultdata>

我非常感谢您的帮助,因为我目前正从一个错误跳转到另一个错误。预先谢谢你。

0 个答案:

没有答案