Jump to content


Photo

Writing XML via DOM, from array under foreach?


  • Please log in to reply
No replies to this topic

#1 jdoe

jdoe
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 May 2006 - 10:09 PM

I'm working on a script to adapt the results from an existing script, which are arrays of text strings, into XML. After having some frustrating trouble with Active-Link's XML library, I've taken a break and am working with straight PHP using DOM.

I've got up to
$dom = new DomDocument('1.0', 'utf-8');
$dom->formatOutput = true;
                
$item = $dom->createElement("item");
$title = $dom->createElement("title");
$titletext = $dom->createTextNode($array['title']);
$title->appendChild($titletext);
$item->appendChild($title);
$dom->appendChild($item);

$dom->save("test.xml");

With the array in the sixth line above, and the whole thing in a foreach. My intent is for the foreach to apply this to each value in the array in turn.

When I run the script, however, I get the error
Fatal error: Cannot use object of type DOMElement as array in test.php on line 6

I at first thought explode might work, but the array doesn't appear to have a delimiter (it does operate normally with "echo," so I feel it's well formed, however strangely), that is, when one calls "echo $array;" or "print_r($array);", the results are valuevaluevalue, though "echo '$array<br />';", as I'd been using it before and it's meant to be used, returns the values line broken.

What's the correct way to solve this? I know this is a strange way to present the problem, but I'm hoping what I'm doing wrong is simple enough that someone'll understand without my having to try and replicate it in example code. Is there some function of DOM I haven't yet found that'll accept and pass the values of an array in turn like this?

Any help's very much appreciated!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users