1

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.

4
  • Are you sure that the test.xml file is loading properly? Commented Mar 15, 2015 at 16:51
  • If I comment out the code that creates the insert and attributes for categoryLeaf but leave the $xml->save, the test.xml file is still intact when viewed. Commented Mar 15, 2015 at 16:56
  • Well, I thought it was but from another test, looks like it saves the xml blank with only the <?xml version="1.0" encoding="UTF-8"?>. Commented Mar 15, 2015 at 17:02
  • Well, got (1) problem fixed by combing the two functions into one so xml is created and categoryleaf is added at sametime. Still having trouble with getting total number of array entries to pass. Only get the last one. Commented Mar 15, 2015 at 18:39

1 Answer 1

1

You are only creating a single categoryLeaf node through:

$categoryLeaf=$xml->createElement("categoryLeaf")

and changing the attribute n times.

You have to create the categoryLeaf inside the for loop.

I also do not understand why you are doing this:

$category=$xml->getElementsByTagName('category');
$categories=$xml->getElementsByTagName('categories');

That does not seem to do anything inside the function.

Just try something like this:

Function CategoryLeafXml($textarray){
         $xml = new DOMDocument('1.0', 'UTF-8');
         $xml->formatOutput = true;
         $xpath = new DOMXPath($xml);
         $xml-> loadXML ('.\\xml\\test.xml');
         $endtvod = $xml->getElementsbyTagName('endtvod')->item(0)
        foreach ($textarray as $text){
            $categoryLeaf=$xml->createElement("categoryLeaf");
            $categoryLeaf->setAttribute('title', $text);
            $endtvod->parentNode->insertBefore($categoryLeaf, $enttvod)
        }
        $xml->save('.\\xml\\test.xml'); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for seeing that. I got it that worked out and you are correct the two lines you pointed out do not do anything, actually forgot to delete them. My problem left is storing the $text to an array. I got it to loop n number of times and create that many categoryleaf elements but the value is only giving me "array". I get error message Uninitialized string offset:
Thanks a million, the foreach ($textarray) solved all my problems. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.