|
Posted by segfalt on 03/22/06 00:05
Hey All, I'm wondering if someone can give me a hand with the
following.
I'm frequently updating xml files, sometimes in bulk updates. I'm
having trouble getting the output formatted nicely (not one long line).
Here is an example.
I would create the doc using something like the following code:
$dom = new DOMDocument("1.0","iso-8859-1");
$dom->formatOutput = true;
$rootnode = $dom->createElement("users");
$id = $dom->createElement("user_id", "0");
$user = $dom->createElement("user_name", "Mark_0");
$rootnode->appendChild($id);
$rootnode->appendChild($user);
$dom->appendChild($rootnode);
$dom->save('domfile.xml');
Later, when I add information to the xml file, I'd use something like
this:
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->load('domfile.xml');
$rootnode = $dom->getElementsByTagName("users")->item(0);
for($x=1;$x<=1000;$x++) {
$id = $dom->createElement("user_id", "{$x}");
$user = $dom->createElement("user_name", "Mark_{$x}");
$rootnode->appendChild($user);
$rootnode->appendChild($id);
}
$dom->appendChild($rootnode);
$dom->save('domfile.xml');
The data get's in there but the xml file looks like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<users>
<user_id>0</user_id>
<user_name>Mark_0</user_name>
<user_name>Mark_1</user_name><user_id>1</user_id><user_name>Mark_2</user_name><user_id>2</user_id><user_name>Mark_3</user_name><user_id>3</user_id>.......</users>
Everything appended to the document will be added on the same line as
the last entry. I'd like it to contine on appending new lines for
elements but it doesn't appear to be happening.
Anyone know what I'm doing wrong or is this just 'the way it is'.
Thanks
Mark
[Back to original message]
|