0
我正在通過simple_load_function()從xml文件中讀取新的xml數據。我輸出的'submitterId'和'pid'值正在重複。 「標題」值是確定的。For Loop輸出重複的XML值
我認爲問題出在循環內。有人可以解釋/協助這種情況發生的原因嗎?這裏是你可以看到輸出的一個working demo。
XML文件:堆疊的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和PID值被複制!!!! ////
<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>
終於想出了這一個。我編寫XML的代碼不在正確的循環中。 – Jgunzblazin