Write to XML

This explains how to write to an xml file using input from a html form. Both php and asp code are given. Both languages use DOM for writing in XML. "The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document(not XML alone)."The public interface of a DOM is specified in its application programming interface (API).

Visit W3c DOM tutorial for more details. The code is self explanatory.

PHP

This link give availbale functions for XML DOM.Click here... Here both html form and DOM are used in the same php file, with first part for html file.

project.php

<?php

$Id = $_POST["Id"];

$Projectname = $_POST["Projectname"];

$Customerid = $_POST["Customerid"];

?>

<html>

<head>

<title>

Contact Information

</title>

</head>

<body>

<form action="<?php echo $PHP_SELF;?>" method="post">

<h3>Enter your contact information</h3>

ID: <input type="text" name="Id"><br>

Project Name: <input type="text" name="Projectname"><br>

Customer ID: <input type="text" name="Customerid"><br>

<input type="submit" id="btnSub" name="btnSub" value="Submit"><br>

</form>

</body>

</html>

<?php

$doc = new DOMDocument();

$doc->formatOutput = true;

$r = $doc->createElement( "contacts" );

$doc->appendChild( $r );

$b = $doc->createElement( "contact" );

$r->appendChild( $b );

$id = $b->appendChild($doc->createElement( "id" ));

$id->appendChild($doc->createTextNode( $Id ));

$projectname = $b->appendChild($doc->createElement( "projectname" ));

$projectname->appendChild($doc->createTextNode( $Projectname ));

$customerid = $b->appendChild($doc->createElement( "customerid" ));

$customerid->appendChild($doc->createTextNode( $Customerid ));

#echo $doc->saveXML();

$myFile = "out.xml";

$fh = fopen($myFile, 'w') or die("can't open file");

fwrite($fh,$doc->saveXML());

?>

HTML FORM

form.html

<html>

<head>

<title>

Contact Information

</title>

</head>

<body>

<form action="projectForm.asp" method="post">

<h3>Enter your contact information</h3>

ID: <input type="text" id="ID" name="ID"><br>

Project Name: <input type="text" id="projectName" name="projectName"><br>

Customer ID: <input type="text" id="customerid" name="customerid"><br>

<input type="submit" id="btd" name="btd" value="Submit"><br>

</form>

</body>

</html>

ASP

projectForm.asp

<%

Set objDom = server.CreateObject("Microsoft.XMLDOM")

objDom.preserveWhiteSpace = True

Set objRoot = objDom.createElement("contact")

objDom.appendChild objRoot

For x = 1 To Request.Form.Count

If instr(1,Request.Form.Key(x),"btd") = 0 Then

Set objFieldValue = objDom.createElement(Request.Form.Key(x))

objFieldValue.Text = Request.Form(x)

objroot.appendChild objFieldValue

End If

Next Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")

objDom.insertBefore objPI, objDom.childNodes(0)

objDom.save "c:\project.xml"

If err.number = 0 then

Response.write("Your form submission has been saved.")

End If

%>

OUTPUT

project.xml

<contact>

<ID>123</ID>

<projectName>333</projectName>

<customerid>33</customerid>

</contact>