首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP-合并两个不重复的xml属性

PHP-合并两个不重复的xml属性
EN

Stack Overflow用户
提问于 2012-06-30 03:32:22
回答 1查看 205关注 0票数 0

我有两个像这样的xml1:

代码语言:javascript
复制
 <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:

代码语言:javascript
复制
<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并输出如下所示

代码语言:javascript
复制
<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>

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

感谢您的回复。

EN

回答 1

Stack Overflow用户

发布于 2012-06-30 04:12:23

我写了一些这样的想法:

代码语言:javascript
复制
$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());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11267888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档