<HTML> <HEAD> <Script language="Javascript" src="conversion.js"> </Script> </HEAD> <BODY> <Form ID="controls"> ÆÄÀÏ:<Input type="text" name="fname" value="prices.xml"> ȯÀ²:<Input type="text" name="rate" value="1.123" Size="4"> <br> <Input Type="Button" Value="º¯È¯Çϱâ" OnClick="convert(controls,xml);"> <Input Type="Button" Value="Áö¿ì±â" OnClick="output.value='' "> <br> <Textarea name="output" rows="6" cols="35" Readonly> </Textarea> </Form> <xml id="xml"> </xml> </BODY> </HTML> |
function convert(fm, xmldoc) { var fname = fm.fname.value, output = fm.output, rate = fm.rate.value; output.value = ""; var doc = parse(fname,xmldoc), top = doc.documentElement; searchPrice(top,output,rate); } function parse(uri,xmldoc) { xmldoc.async = false; xmldoc.load(uri); if (xmldoc.parseError.errorCode != 0) alert(xmldoc.parseError.reason); return xmldoc; } function getText(node) { return node.firstChild.data; } |
function searchPrice(node,output,rate) |
function convert2(fm, xmldoc) { var fname = fm.fname.value, output = fm.output, rate = fm.rate.value; output.value = ""; var doc = parse(fname,xmldoc), top = doc.documentElement; walkNode(top,output,rate); // new } // function parse(,) : same function walkNode(node,output,rate) { if(node.nodeType == 1) { if(node.nodeName == "product") walkProduct(node,output,rate); else { var children, i; children = node.childNodes; for (i=0; i<children.length; i++) walkNode(children.item(i), output, rate); } } } // function getText() : same |
function walkProduct(node,output,rate) { if(node.nodeType==1 && node.nodeName=="product") { var name, price, children, i; children = node.childNodes; for (i=0; i<children.length; i++) { var child = children.item(i); if(child.nodeType == 1) { if(child.nodeName == "price") price = getText(child)*rate; else if(child.nodeName == "name") name = getText(child); } } output.value += name + ":" + price + "\r"; } } |
var USD = 1, CAD = 2; // US, Canada function getCurrencyCode(currency) { if (currency == "usd") return USD; else if (currency == "cad") return CAD; else return -1; } function convert(fm, xmldoc) { var pfile = fm.prices.value, // new rfile = fm.rates.value, // new output = fm.output, rates = new Array(3); // new output.value = ""; var doc = parse(rfile,xmldoc), // new top = doc.documentElement; // new searchRate(top,rates); // new doc = parse(pfile,xmldoc), top = doc.documentElement; walkNode(top,output,rates); } function parse(uri,xmldoc) // same |
function searchRate(node,rates) // new { if(node.nodeType == 1) { if(node.nodeName == "rate") walkRate(node,rates); var children, i; children = node.childNodes; for (i=0; i<children.length; i++) searchRate(children.item(i),rates); } } function walkRate(node,rates) { if (node.attributes != null) { var attr = node.attributes.getNamedItem("currency"); var currency = getCurrencyCode(attr.value); rate = node.attributes.getNamedItem("rate"); if (currency != -1) rates[currency] = rate.value; } } |
function walkNode(node,output,rates) { // almost same if(node.nodeType == 1) { if(node.nodeName == "product") walkProduct(node,output,rates); // else { . . . // »ý·« for (i=0; i<children.length; i++) walkNode(children.item(i), output, rates); } } } function walkProduct(node,output,rates) { //almost same if(node.nodeType==1 && node.nodeName=="product") { . . . // »ý·« for (i=0; i<children.length; i++) { var child = children.item(i); if(child.nodeType == 1) { if(child.nodeName == "price") price = walkPrice(child,rates); else if(child.nodeName == "name") name = getText(child); } } output.value += name + ": " + price + "\r"; } } |
|
function walkPrice(node,rates) { // similar to walkRate, ±³°ú¼ ¿À·ù if (node.attributes != null) { var attr = node.attributes.getNamedItem("currency"); var currency = getCurrencyCode(attr.value); if (currency != -1) return getText(node)*rates[currency]; } } function getText(node) // same |
xmldoc.loadXML('<p>Send comments and
suggestions to <url protocol="mailto">sblim@sookmyuing.ac.kr.</p>'); if (xmldoc.parseError) alert(xmldoc.parseError.reason); |
xmldoc.loadXML('<p>Send comments and
suggestions to <url protocol="mailto>sblim@sookmyuing.ac.kr.</url></p>'); if (xmldoc.parseError) alert(xmldoc.parseError.reason); |
xmldoc.loadXML('<xsl:stylesheet
xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xml:template match="p"> <P><xsl:apply-template/> </xsl:template> </xsl:stylesheet>'); if (xmldoc.parseError) alert(xmldoc.parseError.reason); |
<HTML> <HEAD> <title> º¯È¯ ¿¹Á¦ </title> </HEAD> <frameset cols="4*,6*"> <frame src="conversion.html" name="conv"> <frame src="prices.xml" name="xml"> </frameset> </HTML> |
function convert(fm, xmldoc) { var output = fm.output, rate = fm.rate.value; output.value = ""; var top = xmldoc.documentElement; searchPrice(top,output,rate); } function searchPrice(node,output,rate) { if(node.nodeType == 1) { if(node.nodeName == "price") output.value += (getText(node)*rate)+ "\r"; var children, i; children = node.childNodes; for (i=0; i<children.length; i++) searchPrice(children.item(i), output, rate); } } function getText(node) { if(node.nodeType == 1) { var text = "", children, i; children = node.childNodes; for (i=0; i<children.length; i++) if (children.item(i).nodeType == 3) text += children.item(i).data; return text } else return ""; } |
<HTML> <HEAD> <Script language="Javascript" src="convert.js"> </Script> </HEAD> <BODY> <center><Form ID="control2"> ÆÄÀÏ: <... name="prices" value="prices.xml"> <..."button" ..."¼Ò½ºº¸±â" onClick= "parent.xml.document.location=prices.value"> <br>ȯÀ²: <... name="rates" value="rates.xml"> <..."button" ..."¼Ò½ºº¸±â" onClick= "parent.xml.document.location=rates.value"> <br><..."º¯È¯Çϱâ" ><..."Áö¿ì±â" > <..."óÀ½»óÅ·Î" > ... </Form></center> <center><Form ID="controls"> ȯÀ²: <Input type="text" name="rate" value="1.12"><br> <Input type="button" value="º¯È¯Çϱâ" onClick= "convert(controls, parent.xml.document);"> <Input type="button" value="Áö¿ì±â" onClick="output.value='' "> <br> <textarea name="output" rows="6" cols="35" Readonly></textarea> </Form></center> </BODY> </HTML> |