我正在通过simple_load_function()从我正在阅读的xml文件中编写新的xml数据。我输出的'submitterId'和' pid'值正在重复。 '标题'价值还可以。
我认为问题在于循环。有人可以解释/协助为什么会这样吗?这是一个 working demo ,您可以在其中看到输出。
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和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>
答案 0 :(得分:0)
最后想出了这个。在分开我的循环后,我意识到我没有正确生成XML,因为编写XML的代码不在正确的循环中。这是下面的解决方案。这是一个 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);
}
}
}
}