IPInfoXMLBuilder.java~67~
资源名称:Java.rar [点击查看]
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:4k
源码类别:
Java编程
开发平台:
Java
- package tsinghuaip;
- import org.xml.*;
- import org.w3c.*;
- import org.w3c.dom.*;
- import java.io.*;
- import java.util.*;
- import javax.xml.parsers.*;
- import javax.xml.transform.sax.*;
- import javax.xml.transform.dom.*;
- import javax.xml.transform.*;
- import javax.xml.transform.stream.StreamResult;
- public class IPInfoXMLBuilder {
- private String strTxtFileName; //the text file name
- private String strXmlFileName; //the XML file name
- private String indent=" ";
- private String end="r";
- private String header="<?xml version="1.0" ?>";
- private Document documnet;
- private Element element;
- private DocumentBuilderFactory docBuilderFactory;
- private DocumentBuilder docBuilder;
- private Document doc;
- private Element root;
- public IPInfoXMLBuilder(){
- strTxtFileName = new String();
- strXmlFileName = new String();
- try{
- docBuilderFactory = DocumentBuilderFactory.newInstance();
- docBuilder = docBuilderFactory.newDocumentBuilder();
- doc = docBuilder.newDocument();
- doc.normalize();
- root = doc.createElement("IPADDRESS");
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- public void TxtToXML(String strTxt, String strXml){
- strTxtFileName = strTxt;
- strXmlFileName = strXml;
- String strSeparator = "|"; //the separator of the text file field
- String strTmp = "";
- try{
- BufferedReader inTxt = new BufferedReader(new FileReader(strTxtFileName));
- BufferedWriter outXml = new BufferedWriter(new FileWriter(strXmlFileName));
- outXml.write("<?xml version= "1.0" encoding="ISO-8859-1"?>");
- outXml.newLine();
- outXml.write("<IPADDRESS>");
- while((strTmp = inTxt.readLine()) != null){
- StringTokenizer strToken = new StringTokenizer(strTmp, "|");
- String arrTmp[];
- arrTmp = new String[3];
- for (int i = 0; i < 3; i++)
- arrTmp[i] = new String("");
- int index = 0;
- outXml.newLine();
- outXml.write(" <IPPHASE>");
- while(strToken.hasMoreElements()){
- strTmp = (String)strToken.nextElement();
- strTmp = strTmp.trim();
- arrTmp[index++] = strTmp;
- System.out.println(arrTmp[index-1]);
- }
- Element nodeIPPhase = doc.createElement("IPPHASE");
- Element nodeStartIP = doc.createElement("STARTIP");
- Element nodeEndIP = doc.createElement("ENDIP");
- Element nodeLocal = doc.createElement("LOCAL");
- nodeStartIP.setNodeValue(arrTmp[0]);
- nodeEndIP.setNodeValue(arrTmp[1]);
- nodeLocal.setNodeValue(arrTmp[2]);
- nodeIPPhase.appendChild(nodeStartIP);
- nodeIPPhase.appendChild(nodeEndIP);
- nodeIPPhase.appendChild(nodeLocal);
- root.appendChild(nodeIPPhase);
- outXml.newLine();
- outXml.write(" <STARTIP>" + arrTmp[0] + "</STARTIP>");
- outXml.newLine();
- outXml.write(" <ENDIP>" + arrTmp[1] + "</ENDIP>");
- outXml.newLine();
- outXml.write(" <LOCAL>" + arrTmp[2] + "</LOCAL>");
- outXml.newLine();
- outXml.write(" </IPPHASE>");
- }
- outXml.newLine();
- outXml.write("</IPADDRESS>");
- outXml.flush();
- doc.appendChild(root);
- Source source = new DOMSource(doc);
- File file = new File("DOMXML.xml");
- Result result = new StreamResult(file);
- Transformer xformer = TransformerFactory.newInstance().newTransformer();
- xformer.transform(source, result);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- }
- }