根据Python中多个URL中一个元素的内容合并子嵌套的元素

时间:2019-10-10 19:33:41

标签: python xml elementtree

我想合并来自不同URL的3个xml文件:

XML1:

<Availability> 
 <Licence>
    <ShortIdentifier>2341</ShortIdentifier>
 </Licence>
</Availability>
  <CompList>
    <Comp>
      <CompNumber>61</CompNumber>
      <Name lang="en">comp1</Name>
      <CompType>
        <Name lang="en">comp2</Name>
      </CompType>
      <AverageInfoList>
        <AverageInfo>
          <Name lang="en">Small</Name>
        </AverageInfo>
        <AverageInfo>
          <Name lang="en">Medium</Name>
        </AverageInfo>
      </AverageInfoList>
        .
        .
        .
    </Comp>
  </CompList>

XML2:

<Availability> 
 <Licence>
    <ShortIdentifier>2341</ShortIdentifier>
 </Licence>
</Availability>
  <CompList>
    <Comp>
      <CompNumber>61</CompNumber>
      <Name lang="en">comp1</Name>
      <CompAssociationList>
        <CompAssociation>
             (a lot sub element)
        <CompAssociation>
        .
        .
        .
      </CompAssociationList>
    </Comp>
  </CompList>

XML3:

<Availability> 
 <Licence>
    <ShortIdentifier>2341</ShortIdentifier>
 </Licence>
</Availability>
  <CompList>
    <Comp>
      <CompNumber>61</ComprNumber>
      <Name lang="en">comp1</Name>
      <CompType>
        <Name lang="en">comp2</Name>
      </CompType>
      <RateList>
        <Rate>
          <RateClass>
            <Name lang="en">rate1</Name>
          </RateClass>
        </Rate>
      </RateList>
      .
      .
      .
    </Comp>
  </CompList>

我想基于CompNumber的内容将它们合并到大元素树中:

<Availability> 
 <Licence>
    <ShortIdentifier>2341</ShortIdentifier>
 </Licence>
</Availability>
  <CompList>
    <Comp>
      <CompNumber>61</CompNumber>
      <Name lang="en">comp1</Name>
      <CompType>
        <Name lang="en">comp2</Name>
      </CompType>
      <AverageInfoList>
        <AverageInfo>
          <Name lang="en">Small</Name>
        </AverageInfo>
        <AverageInfo>
          <Name lang="en">Medium</Name>
        </AverageInfo>
      </AverageInfoList>
      <CompAssociationList>
        <CompAssociation>
             (a lot sub element)
        <CompAssociation>
        .
        .
        .
      </CompAssociationList>
      <RateList>
        <Rate>
          <RateClass>
            <Name lang="en">rate1</Name>
          </RateClass>
        </Rate>
      </RateList>
       .
       .
       .
    </Comp>
  </CompList>

我现在要做的是开始合并前两个URL,并将其存储在基于相同CompNumber的空列表中的嵌套字典中,并遍历第三个URL。我想知道是否要合并更多网址,是否有任何有效的方法?谢谢!

0 个答案:

没有答案