【问题标题】:PHP- merge two xml attribute withou duplicatedPHP-合并两个没有重复的xml属性
【发布时间】:2023-03-08 05:04:02
【问题描述】:

我有两个这样的xml xml1:

 <Title>Sea Change</Title>
    <thirdParty_ID></thirdParty_ID>
    <Animation_Indicator>N</Animation_Indicator>
    <Blackout_Indicator>N</Blackout_Indicator>
<SD_Purchase_Zoe></SD_Purchase_Zoe>
    <SD_Purchase_Window_Start_Zoe></SD_Purchase_Window_Start_Zoe>
    <SD_Purchase_Window_End_Zoe></SD_Purchase_Window_End_Zoe>
    <SD_Licensing_Window_start_Zoe></SD_Licensing_Window_start_Zoe>
<HD_Licensing_Window_start_Zoe>2012-04-01</HD_Licensing_Window_start_Zoe>
    <HD_Licensing_Window_end_Zoe>2013-04-01</HD_Licensing_Window_end_Zoe>
    <HD_Purchase_Window_Start_Zoe>2012-04-01</HD_Purchase_Window_Start_Zoe>

xml2:

<Asset_Record>
    <Title>Sea Change</Title>
    <thirdParty_ID></thirdParty_ID>
    <Animation_Indicator>N</Animation_Indicator>
    <Blackout_Indicator>N</Blackout_Indicator>
<SD_Purchase_Zoe>Y</SD_Purchase_Zoe>
    <SD_Purchase_Window_Start_Zoe>2012-04-01</SD_Purchase_Window_Start_Zoe>
    <SD_Purchase_Window_End_Zoe>2013-04-01</SD_Purchase_Window_End_Zoe>
    <SD_Licensing_Window_start_Zoe>2012-04-01</SD_Licensing_Window_start_Zoe>
<HD_Licensing_Window_start_Zoe></HD_Licensing_Window_start_Zoe>
    <HD_Licensing_Window_end_Zoe></HD_Licensing_Window_end_Zoe>
    <HD_Purchase_Window_Start_Zoe></HD_Purchase_Window_Start_Zoe>

我想合并 xml1 和 xml2 并像这样输出

<Title>Sea Change</Title>
    <thirdParty_ID></thirdParty_ID>
    <Animation_Indicator>N</Animation_Indicator>
    <Blackout_Indicator>N</Blackout_Indicator>
<SD_Purchase_Zoe>Y</SD_Purchase_Zoe>
    <SD_Purchase_Window_Start_Zoe>2012-04-01</SD_Purchase_Window_Start_Zoe>
    <SD_Purchase_Window_End_Zoe>2013-04-01</SD_Purchase_Window_End_Zoe>
    <SD_Licensing_Window_start_Zoe>2012-04-01</SD_Licensing_Window_start_Zoe>
    <SD_Licensing_Window_start_Zoe></SD_Licensing_Window_start_Zoe>
<HD_Licensing_Window_start_Zoe>2012-04-01</HD_Licensing_Window_start_Zoe>
    <HD_Licensing_Window_end_Zoe>2013-04-01</HD_Licensing_Window_end_Zoe>
    <HD_Purchase_Window_Start_Zoe>2012-04-01</HD_Purchase_Window_Start_Zoe>

首先我需要检查这两个xmls是否具有相同的标题,如果它们具有相同的标题并将它们合并,如果可以从其中任何一个中找到,则合并所有值。

感谢回复。

【问题讨论】:

    标签: php xml merge duplicates


    【解决方案1】:

    我是这样写的:

    $x1 = <<<XML
    <xml>
    <Title>Sea Change</Title>
    <thirdParty_ID></thirdParty_ID>
    <Animation_Indicator>N</Animation_Indicator>
    <Blackout_Indicator>N</Blackout_Indicator>
    <SD_Purchase_Zoe></SD_Purchase_Zoe>
    <SD_Purchase_Window_Start_Zoe></SD_Purchase_Window_Start_Zoe>
    <SD_Purchase_Window_End_Zoe></SD_Purchase_Window_End_Zoe>
    <SD_Licensing_Window_start_Zoe></SD_Licensing_Window_start_Zoe>
    <HD_Licensing_Window_start_Zoe>2012-04-01</HD_Licensing_Window_start_Zoe>
    <HD_Licensing_Window_end_Zoe>2013-04-01</HD_Licensing_Window_end_Zoe>
    <HD_Purchase_Window_Start_Zoe>2012-04-01</HD_Purchase_Window_Start_Zoe>
    </xml>
    XML;
    
    $x2 = <<<XML
    <xml>
    <Title>Sea Change</Title>
    <thirdParty_ID></thirdParty_ID>
    <Animation_Indicator>N</Animation_Indicator>
    <Blackout_Indicator>N</Blackout_Indicator>
    <SD_Purchase_Zoe>Y</SD_Purchase_Zoe>
    <SD_Purchase_Window_Start_Zoe>2012-04-01</SD_Purchase_Window_Start_Zoe>
    <SD_Purchase_Window_End_Zoe>2013-04-01</SD_Purchase_Window_End_Zoe>
    <SD_Licensing_Window_start_Zoe>2012-04-01</SD_Licensing_Window_start_Zoe>
    <HD_Licensing_Window_start_Zoe></HD_Licensing_Window_start_Zoe>
    <HD_Licensing_Window_end_Zoe></HD_Licensing_Window_end_Zoe>
    <HD_Purchase_Window_Start_Zoe></HD_Purchase_Window_Start_Zoe>
    </xml>
    XML;
    
    $oXML1 = new SimpleXMLElement($x1);
    $oXML2 = new SimpleXMLElement($x2);
    
    $oOutput = new SimpleXMLElement('<xml></xml>');
    
    foreach ( $oXML1 as $key => $value )
    {
        if ( empty($value[0]) )
            $oOutput->addChild($key, $oXML2->{$key}[0]);
        else
            $oOutput->addChild($key, $value[0]);
    }
    
    print_r($oOutput->asXML());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 2019-01-28
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 2015-02-04
      相关资源
      最近更新 更多