Jump to content

inserting records in xml


bluetoother
 Share

Recommended Posts

guys i wonder what is the php code for adding new record in any xml fileexactly the same way in INSERT for SQLlike iam using my xml file as database........i looked in the DOM functions but didnt workkept of showing an errorthe DOMDocument is not defindex:<msg> <name>me</name></msg>i want add another name <name>me2</name>thanks again to all the ppl who always give the solutions

Link to comment
Share on other sites

Hi!You could use SimpleXML (which I preffer) and then do someting like this (using ->addChild())

$xml = new SimpleXMLElement('document.xml', NULL, TRUE);// add$xml->addChild('name', 'you');// save$xml->asXML( 'document.xml' );

If you want use DOM XML you could do something like this (i don't know that much about DOM XML)

$doc = domxml_open_file("document.xml");$node = $doc->create_element("name");$newnode = $doc->append_child($node);

Ok that's probalby wrong, I couldn't figure out how to do it... Go with SimpleXMLGood Luck and Don't Panic!

Link to comment
Share on other sites

Actually, DOM class is constructed like so:

$dom = new DomDocument; //If you want to load an existing file. Otherwise add brackets with info for the one you'll be generating.$dom->formatOutput = true; //Optional$dom->preserveWhiteSpace = true; //Optional$dom->load($xmlFile); //Replace $xmlFile with the actual location of the XML file.

Also, don't forget to finish with:

$dom->saveXML($xmlFile);

to save the result as a file, otherwise it's just a DOM tree to use within the PHP file. By the way, you might find this thing interesting.

Link to comment
Share on other sites

SimpleXML is be enabled by default (at least in PHP5+), but it may, for some unknown reason, be disabled on youre system.I don't now much about using PHP in M$ win (as I'm not a big fan of win...), so i don't know if there's a way to enable SimpleXML or DOM. One way to that is to update/reinstall with a new version of PHP (i.e.PHP 6) and make sure xml is enabled..

Link to comment
Share on other sites

i got this msg :Fatal error: Cannot instantiate non-existent class: simplexmlelement in c:\inetpub\wwwroot\mkawer\test.php on line 2could it be the version of php installed on my server ?coz DOM class is not defined too
If you have PHP4, neighter DOM or SimpleXML will work. Schek phpinfo() to see if you have 5 or 4.
Link to comment
Share on other sites

actually the server that host my web site runs php4 ..and give me the same erroris there any way to add that classes to my web site ?
No. Porting them to PHP4 would be really unefficient. You need to migrate to PHP5. The only way in which you can write XML files in PHP4 will be to write them as text files with the fopen() and the such functions.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...