XML parsing to change tags from array



I have some xml document Data.xml



<?xml version="1.0" encoding="UTF-8"?>
<auto xmlns:xsi="http://ift.tt/ra1lAU">
<announcements>
<cars>
<Site1Tag1>1254</Site1Tag1>
<Site1Tag2>99</Site1Tag2>
<Site1Tag3>1340</Site1Tag3>
<Site1Tag4>92549</Site1Tag4>
<Site1Tag5>
<feature>3</feature>
<feature>5</feature>
</Site1Tag5>
<photos>
<photo>url</photo>
<photo>url</photo>
<photo>url</photo>
</photos>
</cars>
</announcements>
</auto>


I have array of tags i file arrays.php



$MyArray = array(
'Array1'=>array(
'Site1Tag1'=>array('Site2Tag1' => $Site1Tag1Tag),
'Site1Tag2'=>array('Site2Tag2' => $Site1Tag2Tag),
'Site1Tag3'=>array('Site2Tag3' => $Site1Tag3Tag),
'Site1Tag4'=>array('Site2Tag4' => $Site1Tag4Tag),
'Site1Tag5'=>array('Site2Tag5' => $Site1Tag5Tag),
'Site1Tag6'=>array('Site2Tag6' => $Site1Tag6Tag),
'Site1Tag7'=>array('Site2Tag7' => $Site1Tag7Tag),
'Site1Tag8'=>array('Site2Tag8' => $Site1Tag8Tag),

),
'NextArr'=>array()
);


so in arrays.php i hold tag names which must be replaced, so



'Site1Tag2'=>array('Site2Tag2' => $Site1Tag2Tag),


it means Site1Tag2 tag replace wiht Site2Tag2 tag. $Site1Tag2Tag - means probably requered function in future for this tag


Now i use this script



<?php
include("arrays.php");

foreach($MyArray as $DoskaName=>$DoskaArr){
$content = file_get_contents('data.xml');
foreach($DoskaArr as $pole=>$poleAlterArr){
foreach($poleAlterArr as $poleAlter=>$poleAlterVal){
$content = str_replace($pole, $poleAlter, $content);
}
}
file_put_contents($DoskaName . ".xml",$content);
}



?>


This code changes everything like I want except one moment? is that it replace not only tags names but all matches in text also.


So How to replace only tags names nothing more?


No comments:

Post a Comment