Writing a node to an XML file

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
VmusicV
Forum Newbie
Posts: 23
Joined: Sun Jun 15, 2003 10:45 pm
Location: Columbus

Writing a node to an XML file

Post by VmusicV »

Hi,
I've seen tons and tons and tons of XML tutorials. All show you how to use the xml_parse along with the endElement, startElement, characterData, etc. to read and / or parse an XML file or instance.

NO PROBLEM...... nice, easy works great.


I need a SOLID tutorial that shows how to write a specific node and/or element set into an XML. I do NOT need to write an entire XML file.

If I have an XML document with 44 recipies, I want to add a 45th.

Or.... if I have an XML document and recipie 32 has 12 ingredients.... I want add 2 new ingredients.


Can anyone help? - I have a sneaky suspicision it is not so easy as reading or parsing an XML doc.

Does anyone know of a well documented tutorial that shows how to write into an existing xml document?

Thanks In Advance!
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

It's all down to your schema, a complex schema will be a real pain to add too on the fly, can you tell us what you are using the XML for, is it acting like a dB (file based) ?

I ask because it would be better to write the information to an actual database, write a script that loops through the database entries and writes out your XML document, new records are easy to add!

Trying to find the end of your XML document (or a record) can be acheived but you will need to read in (parse) your existing document (into an array/hash) and specifically look for the positon of the last closing (per recipie record) tag, insert the new record (as an array element) and then write out the array to a file (or overwrite the existing).

Hope this helps.

Regards,

To Add:

Another option would be to separate your XML into discrete "fragments", these would just contain the records (no declarations etc):

Code: Select all

<record value="1" name="recipie">
	<data>SOME DATA,</data>
</record>
<record value="2" name="recipie">
	<data>SOME DATA,</data>
</record>
<record value="3" name="recipie">
	<data>SOME DATA,</data>
</record>
<record value="4" name="recipie">
	<data>SOME DATA,</data>
</record>
<record value="5" name="recipie">
	<data>SOME DATA,</data>
</record>
You can easily add a new record to this with the standard file methods in PHP, the only issue is it won't be valid/welformed XML until you top and tail it with your schema (open/close) definitions, which you can do by another process:

Code: Select all

require_once("XMLHead.xml.frag");
require_once("XMRecords.xml.frag");
require_once("XMLFooter.xml.frag");
Would build a valid doc.
VmusicV
Forum Newbie
Posts: 23
Joined: Sun Jun 15, 2003 10:45 pm
Location: Columbus

Writing to an existing XML file?

Post by VmusicV »

Hi,
Well I'm saddened by that response. I appreciate the effort. But let's be honest....
If it takes a few hundred lines of code to write a node to an existing XML file... then yeah, I'd be better off using a database.

The 'potential' advantage to XML is among many things ... 1)you don't need a database 2) you can use stylesheets to easily get different outputs from a single data source (Not applicable here)

My schema is a setlist for a band the hierarchy is:
setlist (has one or many) artists
an artist (has one or many) medias or CDs
a CD (has one or many) songs

So if I have an artist who has one CD node and I want to add another.....well I want to add another and it's song or songs.

What really bothers me is....... what good does it do to have wonderfull php functions to read XML documents if you can't honestly and easily write to an XML document.

If I'm wrong on this... PLEASE show me the functions.... I just don't want to rewrite the whole document ...... I don't want to write hundred's of lines of code to add an element somewhere in an XML document.

Thanks In Advance...
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

In support of my previous post:

http://www.zdnet.com.au/builder/archite ... 023,00.htm

It's all a question of how you are parsing your XML documents, AFAIK the CVII. XML parser functions do just that, you need to implement , for example, the XXIV. DOM XML functions.

As for tutorials/info on PHP/DOM/XML:

http://pear.php.net/manual/en/package.xml.php
http://pear.php.net/packages.php?catpid=22&catname=XML
http://www.devshed.com/Server_Side/PHP/ ... page1.html

Hey, only trying to help. :(

Regards,
Vmusic
Forum Newbie
Posts: 10
Joined: Sat Sep 07, 2002 8:45 am
Location: Columbus, Ohio

Yes Thank You

Post by Vmusic »

Hi,
I do appreciate your help. I believe the PEAR XMLTree classes maybe what I want. They have functions like:

insertChild
removeChild


which is exactly what I asked for. We'll see how this goes and I'll let you know.

Vmusic
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

No problem, not sure of your hosting but, the PEAR/DOM(libxml) libs may not be installed if your on a shared server but you could always try a simple script to see if they are, or get on to your hosts and ask for them to be installed :)

Regards,
Vmusic
Forum Newbie
Posts: 10
Joined: Sat Sep 07, 2002 8:45 am
Location: Columbus, Ohio

Pear Libs

Post by Vmusic »

I'll find out if they're installed.....

I'm hoping, the latest or next versions of PHP has such buildin functions. XML is in such high demand, like it or not. And if there were such functions it was make good sense to use XML as opposed to MySQL in some cases.

Thanks Again
Vmusic
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

I'm a BIG fan of XML and XSL, but because I'm in a data driven industry (news etc), all our data is in either KVP files (key/value pairs) or database, it's far easier fo me to write a shell script to dump out XML from mySQL than try and parse/add/output with DOM etc.

Horses for courses, as they say :)

Regards,
Post Reply