PHP-合并两个不重复的xml属性
我有两个这样的XML xml1:PHP-合并两个不重复的xml属性,php,xml,merge,duplicates,Php,Xml,Merge,Duplicates,我有两个这样的XML xml1: 巨变 N N 2012-04-01 2013-04-01 2012-04-01 xml2: 巨变 N N Y 2012-04-01 2013-04-01 2012-04-01 我想合并xml1和xml2,并像这样输出 <Title>Sea Change</Title> <thirdParty_ID></thirdParty_ID> <Animation_Indicator>N<
巨变
N
N
2012-04-01
2013-04-01
2012-04-01
xml2:
巨变
N
N
Y
2012-04-01
2013-04-01
2012-04-01
我想合并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>
巨变
N
N
Y
2012-04-01
2013-04-01
2012-04-01
2012-04-01
2013-04-01
2012-04-01
首先,我需要检查这两个XML是否具有相同的标题,如果它们具有相同的标题,则合并它们,如果可以从其中任何一个中找到,则合并所有值
谢谢你的回复。我写了这样的话:
$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());
$x1=addChild($key,$value[0]);
}
打印($oOutput->asXML());
<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>
$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());