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);
                }
            }
        }     
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决