Á¦7Àå. Parser & DOM

DOM Æ®¸® ¿îÇà ¿¹Á¦ : ¿¹Á¦ 1 (p.211~213)   

conversion.js
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)
{
   if(node.nodeType == 1)
   {
      if(node.nodeName == "name") // ¹Ì¿Ï¼º
         output.value += getText(node) + " : ";
      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);
   }
}

    

»óÅ°ü¸® : ¿¹Á¦ 2 [p.221] »óÅÂÁ¤º¸¸¦ À¯ÁöÇϸ鼭 Æ®¸® ¿îÇà

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";
  }
}
 

¼Ó¼º(attributes)ÀÇ Ã³¸® : ¿¹Á¦ 3 [¸®½ºÆ® 7.3 p.226]  

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 

¿À·ù [p.232]

DOM°ú Java ÀÀ¿ë

ºê¶ó¿ìÀú ÀÀ¿ë ÇÁ·Î±×·¥ : ¸®½ºÆ® 7.12 [p.240]

<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>