IPInfoXMLBuilder.java~70~
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:4k
源码类别:

Java编程

开发平台:

Java

  1. package tsinghuaip;
  2. import org.w3c.dom.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.xml.parsers.*;
  6. import javax.xml.transform.sax.*;
  7. import javax.xml.transform.dom.*;
  8. import javax.xml.transform.*;
  9. import javax.xml.transform.stream.StreamResult;
  10. public class IPInfoXMLBuilder {
  11.     private String strTxtFileName;   //the text file name
  12.     private String strXmlFileName;   //the XML file name
  13.     private String indent=" ";
  14.     private String end="r";
  15.     private String header="<?xml version="1.0" ?>";
  16.     private Document documnet;
  17.     private Element element;
  18.     private DocumentBuilderFactory docBuilderFactory;
  19.     private DocumentBuilder docBuilder;
  20.     private Document doc;
  21.     private Element root;
  22.     public IPInfoXMLBuilder(){
  23.         strTxtFileName = new String();
  24.         strXmlFileName = new String();
  25.         try{
  26.             docBuilderFactory = DocumentBuilderFactory.newInstance();
  27.             docBuilder = docBuilderFactory.newDocumentBuilder();
  28.             doc = docBuilder.newDocument();
  29.             doc.normalize();
  30.             root = doc.createElement("IPADDRESS");
  31.         }
  32.         catch(Exception e){
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.     public void TxtToXML(String strTxt, String strXml){
  37.         strTxtFileName = strTxt;
  38.         strXmlFileName = strXml;
  39.         String strSeparator = "|";  //the separator of the text file field
  40.         String strTmp = "";
  41.         try{
  42.             BufferedReader inTxt = new BufferedReader(new FileReader(strTxtFileName));
  43.             BufferedWriter outXml = new BufferedWriter(new FileWriter(strXmlFileName));
  44.             outXml.write("<?xml version= "1.0" encoding="ISO-8859-1"?>");
  45.             outXml.newLine();
  46.             outXml.write("<IPADDRESS>");
  47.             while((strTmp = inTxt.readLine()) != null){
  48.                 StringTokenizer strToken = new StringTokenizer(strTmp, "|");
  49.                 String arrTmp[];
  50.                 arrTmp = new String[3];
  51.                 for (int i = 0; i < 3; i++)
  52.                     arrTmp[i] = new String("");
  53.                 int index = 0;
  54.                 outXml.newLine();
  55.                 outXml.write("    <IPPHASE>");
  56.                 while(strToken.hasMoreElements()){
  57.                     strTmp = (String)strToken.nextElement();
  58.                     strTmp = strTmp.trim();
  59.                     arrTmp[index++] = strTmp;
  60.                 }
  61.                 Element nodeIPPhase = doc.createElement("IPPHASE");
  62.                 Element nodeStartIP = doc.createElement("STARTIP");
  63.                 Element nodeEndIP = doc.createElement("ENDIP");
  64.                 Element nodeLocal = doc.createElement("LOCAL");
  65.                 nodeStartIP.setNodeValue(arrTmp[0]);
  66.                 nodeEndIP.setNodeValue(arrTmp[1]);
  67.                 nodeLocal.setNodeValue(arrTmp[2]);
  68.                 nodeIPPhase.appendChild(nodeStartIP);
  69.                 nodeIPPhase.appendChild(nodeEndIP);
  70.                 nodeIPPhase.appendChild(nodeLocal);
  71.                 root.appendChild(nodeIPPhase);
  72.                 outXml.newLine();
  73.                 outXml.write("        <STARTIP>" + arrTmp[0] + "</STARTIP>");
  74.                 outXml.newLine();
  75.                 outXml.write("        <ENDIP>" + arrTmp[1] + "</ENDIP>");
  76.                 outXml.newLine();
  77.                 outXml.write("        <LOCAL>" + arrTmp[2] + "</LOCAL>");
  78.                 outXml.newLine();
  79.                 outXml.write("    </IPPHASE>");
  80.             }
  81.             outXml.newLine();
  82.             outXml.write("</IPADDRESS>");
  83.             outXml.flush();
  84.             doc.appendChild(root);
  85.             Source source = new DOMSource(doc);
  86.             File file = new File("DOMXML.xml");
  87.             Result result = new StreamResult(file);
  88.             Transformer xformer = TransformerFactory.newInstance().newTransformer();
  89.             xformer.transform(source, result);
  90.         }
  91.         catch(Exception e){
  92.             e.printStackTrace();
  93.         }
  94.     }
  95.     public static void main(String[] args) {
  96.     }
  97. }