Jgunzblazin Jgunzblazin - 1 month ago 7
PHP Question

For Loop outputting duplicate XML value

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>

Answer

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);
            }
        }
    }     
}