473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

DOM formatOutput issues

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

Mar 21 '06 #1
5 10720
Tried some variations myself, but no luck. However, I question the need
to view the XML file in a pretty fashion, since XML files aren't meant
for viewing. And if you do view it, view in a web browser that can
display the tree structure nicely.

Also, I wonder why you are constructing the DOM tree the way you are.
The way you have it now:

<users>
<user_id>0</user_id>
<user_name>Mark_0</user_name>
<user_id>1</user_id>
<user_name>Mark_1</user_name>
</users>

Wouldn't you want to seperate out the users into their own nodes? As
in:

<users>
<user>
<user_id>0</user_id>
<user_name>Mark_0</user_name>
</user>
<user>
<user_id>1</user_id>
<user_name>Mark_1</user_name>
</user>
</users>

Mar 22 '06 #2
I realize now that this was just your test data. But I still question
its structure ;-)

Mar 22 '06 #3
Yah, this actually isn't the schema I'm using at all. It was purely a
test case to generate the output example. The real schemas I'm using
are a fair amount more complex and yes I do split objects into their
own nodes in those.

I'd prefer the output be formatted nicely, really, only for debugging
and the odd hand fix purposes. Every once and a while it's nice to be
able to vi the xml and fix something by hand (especially if it was the
php which generated the bad xml). Trying to modify or spot errors in a
2 - 5MB file in vi (or anything for that matter) with no new line
characters is next to impossible. What I don't get is why the first
entry comes out formatted but not the subsequent entries.

Thanks
Mark

Mar 22 '06 #4
After thinking about it I think I may know why. The formatting works
when writing the intial XML document since it's easy enough for the
write method to know how far into the tree it is, and add the requisite
number of tabs or spaces. But to append the new nodes, it would have to
do some more complicated logic to find out how many tabs/spaces in the
tree the new nodes will be, and go from there. Likely it was lazy
and/or time restricted PHP programmers.

If this is the case, one workaround may be to read in the XML file,
append the nodes, then replace the XML file by creating a new one.
You'll get the nicely formatted tree, though sacrificing speed.

Mar 22 '06 #5
Hrm, yes interesting point. It's probably not worth it. I can just
use an xml inspector to view/modify by hand if needed. Thanks for the
help though.

Mark

Mar 22 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom Loredo | last post by:
Hi folks- I'm about to move from a Solaris 8/SPARC environment to a Dell running RedHat 9. Are there any issues I need to be aware of in bringing my Python code over (mostly scientific...
28
by: grahamd | last post by:
Who are the appropriate people to report security problems to in respect of a module included with the Python distribution? I don't feel it appropriate to be reporting it on general mailing lists.
5
by: sandy | last post by:
Hi All, I am a newbie to MySQL and Python. At the first place, I would like to know what are the general performance issues (if any) of using MySQL with Python. By performance, I wanted to...
2
by: malcolm | last post by:
Hello, We have a robust (.NET 1.1 c# winforms) client-server application that utilizes many typed DataSets, typed DataTables and typed DataRows. Our application is a series of windows and popup...
1
by: Aliandro | last post by:
Hi Does any one know where I can find information regarding any issues with SQL and IIS being run under windows XP SP2? as I am in the process of programmning in Dot net and neet some way of...
7
by: David Laub | last post by:
I have stumbled across various Netscape issues, none of which appear to be solvable by tweaking the clientTarget or targetSchema properties. At this point, I'm not even interested in "solving"...
1
by: GaryDean | last post by:
We have been developing all of our .net applications on 32 bit windows using 32 bit SQL Server. We are being asked to now deploy to servers running 64bit windows and 64bit SQL Server. Are there...
3
by: eschneider | last post by:
Just some common issues with WS: Using custom objects: When objects change, seems you are always fixing some issue. Update references, which sometimes does not work. Deployment: Weird errors...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.