Jump to content

Archived

This topic is now archived and is closed to further replies.

jdoe

Writing XML via DOM, from array under foreach?

Recommended Posts

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
[code]
$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");
[/code]

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
[code]
Fatal error: Cannot use object of type DOMElement as array in test.php on line 6[/code]

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!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.