donglang1976 2016-10-29 18:39
浏览 67
已采纳

For Loop输出重复的XML值

I am writing new xml data from the xml file I am reading via simple_load_function(). My outputted 'submitterId' and 'pid' values are being duplicated. 'Headline' value is OK.

I think the problem lies within the loop. Can someone explain/assist why this is happening? Here is a working demo where you can see the output.

XML FILE: stack-test.xml

<?xml version="1.0" encoding="utf-8"?>
<ContentEnvelope>
  <ContentBody>

    <Review>
      <Headline>Great product</Headline>
      <ReviewedBy> 
         <UniqueId>der_11111111</UniqueId> 
      </ReviewedBy>
      <ReviewedKey>
         <Key>cover_creme</Key>
      </ReviewedKey> 
    </Review>

    <Review>
      <Headline>Worst product</Headline>
      <ReviewedBy> 
         <UniqueId>der_88888888</UniqueId> 
      </ReviewedBy>
      <ReviewedKey>
         <Key>setting_powder</Key>
      </ReviewedKey> 
    </Review>

  </ContentBody>
</ContentEnvelope>

CODE:

error_reporting(0);

$devices = array();
$xml = simplexml_load_file('stack-test.xml');

//// Loops through xml to <Headline> tag ////

foreach($xml->ContentBody->Review as $item){
   $device = array();

   foreach($item as $key => $value){
     $device[(string)$key] = (string)$value;
   }
   $devices[] = $device;
}
$devices2 = array();


//// Loops through xml to <ReviewedBy> tag ////

foreach($xml->ContentBody->Review->ReviewedBy as $item2) {
    $device2 = array();

    foreach($item2[1] as $key2 => $value2){
        $device2[(string)$key2] = (string)$value2;
    }
    $devices2[] = $device2;
}
$devices3 = array();

//// Loop through xml to <ReviewedKey> tag ////

foreach($xml->ContentBody->Review->ReviewedKey as $item3) {
    $device3 = array();

    foreach($item3 as $key3 => $value3){
        $device3[(string)$key3] = (string)$value3;
    }
   $devices3[] = $device3;
}

//HEADLINE
foreach($devices as $key => $val){
    $headline = $val[Headline];

    //UNIQUE ID
    foreach($devices2 as $key => $val){
        $uniqueId = $val[UniqueId];
    }

    //KEY
    foreach($devices3 as $key => $val){
        $reviewedKey = $val[Key];
    }

    //// Writes Headline DATA /////

    $location_xml=new XMLWriter();
    $location_xml->openMemory();

    $location_xml->startElement("object-attribute");
    $location_xml->writeAttribute("attribute-id", "headline");
    $location_xml->text($headline);
    $location_xml->endElement();
    echo $location_xml->outputMemory(true);

    //// Writes Unique ID DATA ////

    $uniqueId_xml=new XMLWriter();
    $uniqueId_xml->openMemory();

    $uniqueId_xml->startElement("object-attribute");
    $uniqueId_xml->writeAttribute("attribute-id", "submitterId");
    $uniqueId_xml->text($uniqueId);
    $uniqueId_xml->endElement();
    echo $uniqueId_xml->outputMemory(true);

    //// Writes Key DATA ////

    $reviewedKey_xml=new XMLWriter();
    $reviewedKey_xml->openMemory();

    $reviewedKey_xml->startElement("object-attribute");
    $reviewedKey_xml->writeAttribute("attribute-id", "pid");
    $reviewedKey_xml->text($reviewedKey);
    $reviewedKey_xml->endElement();
    echo $reviewedKey_xml->outputMemory(true);
 }//// Closes loop ////

//// OUTPUT: submitterId and pid values are duplicated!!!! ////

<object-attribute attribute-id="headline">Great product</object-attribute>
<object-attribute attribute-id="submitterId">der_11111111</object-attribute
<object-attribute attribute-id="pid">cover_creme</object-attribute>

<object-attribute attribute-id="headline">Worst product</object-attribute>
<object-attribute attribute-id="submitterId">der_11111111</object-attribute>
<object-attribute attribute-id="pid">cover_creme</object-attribute>
  • 写回答

1条回答 默认 最新

  • douqin2108 2016-11-04 21:06
    关注

    Finally figured this one out. After picking apart my loops I realized that I was not correctly generating the XML because the code that writes the XML were not in the correct loops. This is the solution below. Here is a working demo.

    foreach($xml->ContentBody->Review as $item){
        foreach($item->Headline as $item2){
    
            ///////// Writes Headline DATA //////////
            $location_xml=new XMLWriter();
            $location_xml->openMemory();
            $location_xml->startElement("object-attribute");
            $location_xml->writeAttribute("attribute-id", "headline");
            $location_xml->text($item2);
            $location_xml->endElement();
            echo $location_xml->outputMemory(true);
        }
    
        foreach($item->ReviewedBy as $item2) {
            foreach($item2 as $key2 => $value2){
                if($key2=='UniqueId'){
    
                    ///////// Writes Submitter ID DATA //////////
                    $uniqueId_xml=new XMLWriter();
                    $uniqueId_xml->openMemory();
                    $uniqueId_xml->startElement("object-attribute");
                    $uniqueId_xml->writeAttribute("attribute-id", "submitterId");
                    $uniqueId_xml->text($value2);
                    $uniqueId_xml->endElement();
                    echo $uniqueId_xml->outputMemory(true);
                }
            }
        }
    
        foreach($item->ReviewedKey as $pid) {
            foreach($pid as $key2 => $value3){
                if($key2=='Key'){
    
                    ///////// Writes Product ID DATA //////////
                    $reviewedKey_xml=new XMLWriter();
                    $reviewedKey_xml->openMemory();
                    $reviewedKey_xml->startElement("object-attribute");
                    $reviewedKey_xml->writeAttribute("attribute-id", "pid");
                    $reviewedKey_xml->text($value3);
                    $reviewedKey_xml->endElement();
                    echo $reviewedKey_xml->outputMemory(true);
                }
            }
        }     
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 开发一个小区生态的小程序
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件