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

Java编程

开发平台:

Java

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