Hunter的大杂烩 技术学习笔记

2007-05-20

xml 读写范例

Filed under: Java — hunter @ 11:49 pm

import java.io.*;
import java.lang.Integer;
import java.util.*;
import java.lang.Math;

import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;

public class htmlparsertest
{
 /**
  *
  * @param doc
  * @param i
  * @param Hcode
  *
  * @return String
  */
 public static String getXmlValue(NodeList nl, int offset, String codename)
 {


  String codeId = “”;
        NodeList childList = nl.item(offset).getChildNodes();
  for (int i = 0; i < childList.getLength(); i++)   {    //System.err.println("id:"+i+" nodename:"+childList.item(i).getNodeName()+    //       " value:"+childList.item(i).getTextContent());    if (childList.item(i).getNodeName() == codename)    {                 codeId = childList.item(i).getTextContent();     break;    }   }   return codeId;  }  /**   *   * @param next   * @param zherang   */  public static void showZherang(Double priceA, Double priceB)  {   String color_begin = "";   String color_end   = "";   Double zherang = CalPersen(priceA,priceB);   if (zherang >= 20)
  {
   color_begin = ““;
   color_end   = “
“;
  } 
  else if (zherang < 0)   {    color_begin = "“;
   color_end   = “
“;
  }
  System.out.println(“

                                           

 “+CalPersen(next._bprice,next._aprice)+”%”);
  }
  catch (ParserConfigurationException e)
  {
   e.printStackTrace();
  }
  catch (org.xml.sax.SAXException e)
  {
   e.printStackTrace();
  }
  catch ( IOException e )
  {
   //发生网络异常
   e.printStackTrace();
  }
 }
 public static Double CalPersen(Double priceA, Double priceB)
 {
  if (priceA == 0 || priceB == 0)
  {
   return 0.0;
  }
  else if (priceA – priceB < 0)   {    return Math.round((Math.abs(priceA-priceB)/priceA)*100)*1.0;   }   else   {    return -Math.round((Math.abs(priceA-priceB)/priceB)*100)*1.0;   }  } }

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress