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($i d);
$rootnode->appendChild($u ser);
$dom->appendChild($r ootnode);
$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->getElementsByT agName("users")->item(0);
for($x=1;$x<=10 00;$x++) {
$id = $dom->createElement( "user_id", "{$x}");
$user = $dom->createElement( "user_name" , "Mark_{$x}" );
$rootnode->appendChild($u ser);
$rootnode->appendChild($i d);
}
$dom->appendChild($r ootnode);
$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_n ame>Mark_2</user_name><user _id>2</user_id><user_n ame>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
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($i d);
$rootnode->appendChild($u ser);
$dom->appendChild($r ootnode);
$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->getElementsByT agName("users")->item(0);
for($x=1;$x<=10 00;$x++) {
$id = $dom->createElement( "user_id", "{$x}");
$user = $dom->createElement( "user_name" , "Mark_{$x}" );
$rootnode->appendChild($u ser);
$rootnode->appendChild($i d);
}
$dom->appendChild($r ootnode);
$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_n ame>Mark_2</user_name><user _id>2</user_id><user_n ame>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
Comment