I have an xml file that is created thru php. The basic structure is:
<catgories> *this is root*
<category> *element*
<title>xy</title> *attribute*
<desc>yu</desc> *attribute*
<categoryLeaf> *child element of category*
<title>rt</title> *attribute of categoryLeaf*
</categoryLeaf>
<endtvod></endtvod>
</category>
</categories>
The xml is created on the fly at one time with the exception of the categoryLeaf and it's attributes which are created in a separate function. The xml is created correctly. In order to get the categoryLeaf I am parsing an html and extracting information and storing that information in an array with the follwoing:
if (!empty($dom)) { //IF NOT EMPTY FIND CATEGORIES
$clsort = $xpath->query('//div[@class="sort"]');
foreach($clsort as $clsorts){
$li= $clsorts->getElementsByTagName('li');
foreach ($li as $lis){
$links=$lis->getElementsByTagname('a');
foreach ($links as $link){
$href=$link->getAttribute('href');
$text=$link->nodeValue;
if ($text=="#"){
break;
}else{
$textarray=$text;
ECHO $textarray."<br>";
CategoryLeafXml($textarray);
}
}
}
}
}
else
{
echo "EMPTY CONTAINER". "<br>";
}
The desired information is obtained thru the code above and added to the textarray. I can echo it and each shows up. I pass the array to a function that is to open the xml, insert the categoryLeaf along with the title attribute and save thru code:
Function CategoryLeafXml($textarray){
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$xpath = new DOMXPath($xml);
$xml-> loadXML ('.\\xml\\test.xml');
$arrcount=count($textarray);
echo $arrcount."<br>";
$categoryLeaf=$xml->createElement("categoryLeaf");
for ($i=0; $i<$arrcount;$i++){
echo $textarray."<br>";
$endtvod=$xml->getElementsByTagName('endtvod')->item(0); //LOCATE tag
$category=$xml->getElementsByTagName('category');
$categories=$xml->getElementsByTagName('categories');
$newleaf=$xml->insertBefore($categoryLeaf, $endtvod);
$title = $xml->createAttribute("title");
$title->value= $textarray;
$categoryLeaf->appendChild($title);
$xml->save('.\\xml\\test.xml');
}
}
What is suppose to occur is the xml structure is created, the html is parse into the textarray, the textarray is passed to the function where the element categoryLeaf is inserted before the element tvod. The textarray is the attribute value for the categoryLeaf/title. I am having 2 problems, (1)the xml is opened and the categoryLeaf is created, but when saved the result is:
<?xml version="1.0" encoding="UTF-8"?>
<categoryLeaf title="value of textarray"/>
The xml is overwritten leaving only the one inserted element with only the last array value. (2) The array count always shows 1 and the for loop only runs one time writing only the array's last value. I anticipated a categoryLeaf being added for value of the array count but it is only seeing that value as being one. I know there are about 25 entries in the array.