Jump to content

Writing XML via DOM, from array under foreach?


jdoe

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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.