The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 6 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Hi, This is part 2 of the XML
Â
-XML Applications Development
Â
Take the file measurements1.html and copy it to measurements2.html. Edit measurements2.html to add DOM parsing capabilities.
Â
Download the textbooks code from the Wrox web site and copy the zXml.src.js to your project folder.
Edit the measurements2.html file.
Within the head section of the html document, add these supporting script declarations, as well as these javascript function declarations:
Â
<script type="text/javascript" src="/zXml.src.js"></script>
Â
<script type="text/javascript">
Â
function whatFile() {
Â
var stringFile = document.form1.cmuds.value;
Â
var oResults = document.getElementById("txtXml");
Â
var fso = new ActiveXObject("Scripting.FileSystemObject");
Â
var ts = fso.OpenTextFile(stringFile);
Â
oResults.value += ts.ReadAll() + "n";
Â
ts.Close();
Â
}
Â
function getDom() {
Â
var oDom = null;
Â
try {
Â
oDom = new ActiveXObject("Msxml2.DomDocument.3.0");
Â
}
Â
catch (e) {
Â
alert("This code needs msxml version 3 installed to operate.");
Â
}
Â
return oDom;
Â
}
Â
function getXml() {
Â
return document.getElementById("txtXml").value;
Â
}
Â
function showParseError(err) {
Â
var sMessage = "Error parsing input.nReason: " + err.reason + "nSource: " + err.srcText;
Â
alert(sMessage);
Â
}
Â
function showResults(text) {
Â
var oResults = document.getElementById("txtResults");
Â
oResults.value += text + "n";
Â
}
Â
//The function countTotalMeasurements is counting how many measurement nodes are in the measurements1.xml file.
Â
function countTotalMeasurements(dom){
Â
}
Â
//The function countMeasurements is counting how many nodes of a "typeOfMeasurement" are in the measurements1.xml file.
Â
function countMeasurements(dom, measType){
Â
}
Â
//The function listElements outputs the values of the specific measure type for a specific element type (such as the value of all "weightType" of "troy").
Â
function listElements(dom, measType, elementType){
Â
}
Â
function analyze() {
Â
var oDom = getDom();
Â
if (!oDom) return;
Â
var bLoaded = oDom.loadXML(getXml());
Â
if (!bLoaded) {
Â
showParseError(oDom.parseError);
Â
return;
Â
}
Â
var x = "measureDistance";
Â
var y = "imperial";
Â
var resultText = "The document element is: " + oDom.documentElement.nodeName + "n";
Â
resultText += "There are " + countTotalMeasurements(oDom) + " measurement nodes in the filen";
Â
resultText += "There are " + countMeasurements(oDom, "distanceType") + " distance measurements in the filen";
Â
resultText += "There are " + countElements(oDom, x, y) + " " + y + " elements in the filen";
Â
resultText += listElements(oDom, x, y);
Â
y = "metric";
Â
resultText += "There are " + countElements(oDom, x, y) + " " + y + " elements in the filen";
Â
resultText += listElements(oDom, x, y);
Â
resultText += "There are " + countMeasurements(oDom, "weightType") + " weight measurements in the filen";
Â
x = "measureWeight";
Â
y = "gram";
Â
resultText += "There are " + countElements(oDom, x, y) + " " + y + " elements in the filen";
Â
resultText += listElements(oDom, x, y);
Â
y = "troy";
Â
resultText += "There are " + countElements(oDom, x, y) + " " + y + " elements in the filen";
Â
resultText += listElements(oDom, x, y);
Â
showResults(resultText);
Â
}
Â
</script>
Â
Within the body section, add this form after the text that already exists:
<form name=form1>
Â
<input type=file name="cmuds">
Â
<input type=button onClick="whatFile()" value="Open File"><br />
Â
<textarea cols="80" rows="10" id="txtXml"></textarea><br />
Â
<textarea cols="80" rows="10" id="txtResults"></textarea>
Â
<br>
Â
<input type="button" value="Analyze XML" onclick="analyze();">
Â
</form>
Â
Now fill in the functions countTotalMeasurements, countMeasurements, and the listElements.
Within these functions you should be using the DOM and Node properties to discover the information that each function is trying to find. You will be using things such as attributes, firstChild, childNodes and so forth.
When this is complete, you should be able to click the "Browse..." button, browse to your measurements1.xml file, click "Open", then click "Open File"
In the top box, you should have listed your xml file contents.
Example:
Â
<measurements>
Â
<measurement typeOfMeasurement ="distanceType">
Â
<distance measureDistance="imperial">1.45</distance>
Â
</measurement>
Â
<measurement typeOfMeasurement ="weightType">
Â
<weight measureWeight="troy">.6</weight>
Â
</measurement>
Â
<measurement typeOfMeasurement ="distanceType">
Â
<distance measureDistance="metric">25</distance>
Â
</measurement>
Â
</measurements>
Â
Click the "Analyze XML" button.
In the bottom box, the text from the analyze function should tell you:
Â
How many total measurement nodes are in the file.
How many distance nodes are in the file.
How many imperial elements are in the file.
List the imperial values.
How many metric elements are in the file.
List the metric elements.
How many weight nodes are in the file.
How many gram elements are in the file.
List the gram elements.
How many troy elements are in the file.
List the troy elements.
Example:
Â
The document element is: measurements
Â
There are 3 measurement nodes in the file
Â
There are 2 distance measurements in the file
Â
There are 1 imperial elements in the file
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly