IPInfoXMLBuilder.java~67~
上传用户: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.         try{
  28.             docBuilderFactory = DocumentBuilderFactory.newInstance();
  29.             docBuilder = docBuilderFactory.newDocumentBuilder();
  30.             doc = docBuilder.newDocument();
  31.             doc.normalize();
  32.             root = doc.createElement("IPADDRESS");
  33.         }
  34.         catch(Exception e){
  35.             e.printStackTrace();
  36.         }
  37.     }
  38.     public void TxtToXML(String strTxt, String strXml){
  39.         strTxtFileName = strTxt;
  40.         strXmlFileName = strXml;
  41.         String strSeparator = "|";  //the separator of the text file field
  42.         String strTmp = "";
  43.         try{
  44.             BufferedReader inTxt = new BufferedReader(new FileReader(strTxtFileName));
  45.             BufferedWriter outXml = new BufferedWriter(new FileWriter(strXmlFileName));
  46.             outXml.write("<?xml version= "1.0" encoding="ISO-8859-1"?>");
  47.             outXml.newLine();
  48.             outXml.write("<IPADDRESS>");
  49.             while((strTmp = inTxt.readLine()) != null){
  50.                 StringTokenizer strToken = new StringTokenizer(strTmp, "|");
  51.                 String arrTmp[];
  52.                 arrTmp = new String[3];
  53.                 for (int i = 0; i < 3; i++)
  54.                     arrTmp[i] = new String("");
  55.                 int index = 0;
  56.                 outXml.newLine();
  57.                 outXml.write("    <IPPHASE>");
  58.                 while(strToken.hasMoreElements()){
  59.                     strTmp = (String)strToken.nextElement();
  60.                     strTmp = strTmp.trim();
  61.                     arrTmp[index++] = strTmp;
  62.                     System.out.println(arrTmp[index-1]);
  63.                 }
  64.                 Element nodeIPPhase = doc.createElement("IPPHASE");
  65.                 Element nodeStartIP = doc.createElement("STARTIP");
  66.                 Element nodeEndIP = doc.createElement("ENDIP");
  67.                 Element nodeLocal = doc.createElement("LOCAL");
  68.                 nodeStartIP.setNodeValue(arrTmp[0]);
  69.                 nodeEndIP.setNodeValue(arrTmp[1]);
  70.                 nodeLocal.setNodeValue(arrTmp[2]);
  71.                 nodeIPPhase.appendChild(nodeStartIP);
  72.                 nodeIPPhase.appendChild(nodeEndIP);
  73.                 nodeIPPhase.appendChild(nodeLocal);
  74.                 root.appendChild(nodeIPPhase);
  75.                 outXml.newLine();
  76.                 outXml.write("        <STARTIP>" + arrTmp[0] + "</STARTIP>");
  77.                 outXml.newLine();
  78.                 outXml.write("        <ENDIP>" + arrTmp[1] + "</ENDIP>");
  79.                 outXml.newLine();
  80.                 outXml.write("        <LOCAL>" + arrTmp[2] + "</LOCAL>");
  81.                 outXml.newLine();
  82.                 outXml.write("    </IPPHASE>");
  83.             }
  84.             outXml.newLine();
  85.             outXml.write("</IPADDRESS>");
  86.             outXml.flush();
  87.             doc.appendChild(root);
  88.             Source source = new DOMSource(doc);
  89.             File file = new File("DOMXML.xml");
  90.             Result result = new StreamResult(file);
  91.             Transformer xformer = TransformerFactory.newInstance().newTransformer();
  92.             xformer.transform(source, result);
  93.         }
  94.         catch(Exception e){
  95.             e.printStackTrace();
  96.         }
  97.     }
  98.     public static void main(String[] args) {
  99.     }
  100. }