|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
is this code rep correct???
Hi,
i am new to XML and need ur help. i am creating a form in which user can make selections using select boxes. on the server side, i plan to generate an XML file that contains these user selections. how can i create such a XML file? for eg: the form has the following: <form> <select> <option> java <option> xml <option> php </select> </form> now i want to create a XML file that shows the user selection and is then interpreted by a servlet that extracts the data. say for example, the user chooses java from the list, then i want an XML to be generated that represents this selection: is this XML representation correct: <selection> <user id="1"> <name>java</name> </user> </selection> now if the user chooses 'php' from the select tag, the XML should contain 'php' in it. if not, how can i create the XML file that contains only the user selected values? thanks in advance. |
|
#2
|
|||
|
|||
|
The XML you listed was valid XML, however you could shorten the extra elements into attributes (which is pretty easy to read later). So instead of having:
Code:
<selection> <user id="1"> <name>java</name> </user> </selection> You could shorten it to something like this: Code:
<?xml version="1.0"?> <selections> <selection userID="1" userLang="php" /> </selections> Notice that one must have a root element, in this case "selections". I assume that you will want to append more users to this xml file, so you could end up with something looking like this: Code:
<?xml version="1.0"?> <selections> <selection userID="1" userLang="php"/> <selection userID="2" userLang="xml"/> <selection userID="3" userLang="php"/> </selections> I suppose it is a matter of preference. Anyways, I threw together an html form, along with an asp page (vbscript) that opens/creates an xml file and appends data to it. See below: form.html Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Appending XML
</title>
</head>
<body>
<form method="post" action="processLang.asp">
<select id="combo" name="language">
<option value="java" selected="selected">
java
</option>
<option value="xml" selected="selected">
xml
</option>
<option value="php" selected="selected">
php
</option>
</select> <input type="submit" class="submitLink"
value="Go" />
</form>
</body>
</html>
processLang.asp Code:
<%
'Initialize Variables
Dim lang, objDom, objRoot, objSelection, objUserID, objUserLang, objPi, blnFileExists, nodeCount, fileLocation
lang = Request.Form("language")
'Replace this with the full file location for your xml document.
'Be sure that you have read/write permissions for the file/folder.
fileLocation = "C:\Inetpub\wwwroot\devshed\testingTesting.xml"
'Instantiate the Microsoft XMLDOM.
Set objDom = server.CreateObject("Microsoft.XMLDOM")
objDom.preserveWhiteSpace = True
'
'Call the Load Method of the XMLDOM Object. The Load Method has a
'boolean return value indicating whether or not the file could be
'loaded. If the file exists and loads it will return true, otherwise,
'it will return false.
blnFileExists = objDom.Load(fileLocation)
'Test to see if the file loaded successfully.
If blnFileExists = True Then
'If the file loaded set the objRoot Object equal to the root element
'of the XML document.
Set objRoot = objDom.documentElement
nodeCount = objRoot.ChildNodes.Length
Else
nodeCount = 0
'Create your root element and append it to the XML document.
Set objRoot = objDom.createElement("selections")
objDom.appendChild objRoot
End If
'Create individual selection and append it to selections (root)
Set objSelection = objDom.createElement("selection")
objRoot.appendChild objSelection
'Add attributes to selection
Set objUserID = objDom.createAttribute("userID")
objUserID.Text = nodeCount + 1
objSelection.setAttributeNode objUserID
Set objUserLang = objDom.createAttribute("userLang")
objUserLang.Text = lang
objSelection.setAttributeNode objUserLang
'Check once again to see if the file loaded successfully. If it did
'not, that means we are creating a new document and need to be sure to
'insert the XML processing instruction.
If blnFileExists = False then
'Create the xml processing instruction.
Set objPI = objDom.createProcessingInstruction("xml", "version='1.0'")
'Append the processing instruction to the XML document.
objDom.insertBefore objPI, objDom.childNodes(0)
End If
'Save the XML document.
objDom.save fileLocation
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
</head>
<body>
User <%Response.Write(nodeCount + 1)%> selected
<%Response.Write(lang)%>.<br />
<br />
<a href="testingTesting.xml">View Generated XML file</a> (may
require a refresh)<br />
<br />
<a href="form.html">Try it again</a>
</body>
</html>
The code may look complicated, but I would encourage you to copy and paste it (be sure to change the xml file name to a file you can read/write). If you have any questions, post them here. Also, I used these sites when writing the asp code:
|
|
#3
|
|||
|
|||
|
is this code rep correct???
Hi tsprings,
thank u very much for the code. it works well but i am afraid i need a couple of more clarifications: (1) in the HTML form, instead of mere values like 'php', say i have to represent an entire file path that contains a image say a map of US; then i will be modifying my XML file as: <selections> <selection userid="1">C:/My Documents/usa.jpg</selection> .......... </selections> now is this XML file representation correct? (2) instead of ASP, is it possible to use any other language? thanks for the trouble and waiting to hear from u. |
|
#4
|
|||
|
|||
|
Hello yskh.
1) in the HTML form, instead of mere values like 'php', say i have to represent an entire file path that contains a image say a map of US; then i will be modifying my XML file as: Code:
<selections>
<selection userid="1">C:/My Documents/usa.jpg</selection>
..........
</selections>
You can represent the entire file path as the text of an element, but I prefer keeping it in attributes as I would rather access the value of an attribute than the text of an element. You can do a lot of things with attributes (sorting, etc.), another reason I prefer to leave them as such. Code:
<?xml version="1.0"?>
<selections>
<selection userID="1" fileLocation="C:/My Documents/mexico.jpg"/>
<selection userID="2" fileLocation="C:/My Documents/canada.jpg"/>
<selection userID="3" fileLocation="C:/My Documents/usa.jpg"/>
</selections>
Just to clarify, in the code above, "selections" and "selection" are both elements while "userID" and "fileLocation" are attributes. 2) instead of ASP, is it possible to use any other language? VBscript is the language I have always appended xml documents with, so this took me a second. Yes, you can use PHP, but you have to make sure that domxml is enabled. (Uncomment extension=php_domxml.dll in the php.ini file and move the php_domxml.dll from your PHP folder to your system folder.) form2.html Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Appending XML
</title>
</head>
<body>
<form method="get" action="processForm.php">
<select id="combo" name="fileLocation">
<option value="C:/My Documents/usa.jpg">
Map of USA
</option>
<option value="C:/My Documents/canada.jpg">
Map of Canada
</option>
<option value="C:/My Documents/mexico.jpg">
Map of Mexico
</option>
</select> <input type="submit" class="submitLink"
value="Go" />
</form>
</body>
</html>
processForm.php Code:
<?
//Replace this with the full file location for your xml document.
//Be sure that you have read/write permissions for the file/folder.
$file = "C:/Inetpub/wwwroot/devshed/testingTesting.xml";
//Test to see if the file loaded successfully.
if (file_exists($file))
{
// create a document object & reference to root node
$doc = xmldocfile($file);
$root = $doc->root();
}
else
{
//Create your root element and append it to the XML document.
$doc = domxml_new_doc("1.0");
$root = $doc->create_element("selections");
$root = $doc->append_child($root);
}
//Determine the number of children of "selections"
$nodes = $root->children();
$nodeCount = 1;
//I had to include this loop to make sure there was no whitespace
//that was being counted. Only count XML_ELEMENT_NODE.
for ($x=0; $x<sizeof($nodes); $x++)
if ($nodes[$x]->type == XML_ELEMENT_NODE)
$nodeCount++;
//create selection element
$selection = $doc->create_element("selection");
$selection = $root->append_child($selection);
//Grab the fileLocation from form
$fileLocation = $_GET['fileLocation'];
//add userID & userLang attributes
$selection->set_attribute('userID', $nodeCount);
$selection->set_attribute('fileLocation', $fileLocation);
//write to the file (first delete existing one if it exists)
if (file_exists($file))
unlink($file);
$doc->dump_file($file, false, true);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
</head>
<body>
User <? echo $nodeCount; ?> selected
<? echo $fileLocation; ?>.<br />
<br />
<a href= "testingTesting.xml">View Generated XML file</a> (may
require a refresh)<br />
<br />
<a href="form2.html">Try it again</a>
</body>
</html>
Hopefully this clarifies your questions! Let me know if you have any other questions, I'm happy to help. ![]() Also, a great resource for learning XML is W3 Schools. I'd encourage you to check it out. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > is this code rep correct??? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|