将普通的键值对转换成xml形式的键值对
今天在做FOA域名转出的时候,有一个注册代码和注册公司的对应关系。就是说通过poll端口你可以查到注册代码,然后通过这个对应关系找到对应公司。
于是想把普通文本中的键值对,转换成xml文件。
a.txt文件中的内容(源文件中没有‘$’,为了好判断,将空格转换成‘$’):
1$Reserved$2$Network$Solutions,$LLC$3$Registry$Installation$8$Test$Registrar$9$register.com,$Inc.$13$Melbourne$IT,$Ltd$14$France$Telecom$15$CORE$Internet$Council$of$Registrars$16$AOL$LLC$30$NameSecure$L.L.C.$31$DSTR$Acquisition$PA$I,$LLC$dba$DomainBank.com$48$eNom,$Inc.$49$GMO$Internet,$Inc.$d/b/a$Onamae.com$52$Abacus$America,$Inc.$53$A$Technology$Company,$Inc.$54$Signature$Domains,$LLC$
<property name="1" value="Reserved" /><property name="2" value="Network Solutions, LLC" /><property name="3" value="Registry Installation" /><property name="8" value="Test Registrar" /><property name="9" value="register.com, Inc." /><property name="13" value="Melbourne IT, Ltd" /><property name="14" value="France Telecom" /><property name="15" value="CORE Internet Council of Registrars" /><property name="16" value="AOL LLC" /><property name="30" value="NameSecure L.L.C." /><property name="31" value="DSTR Acquisition PA I, LLC dba DomainBank.com" /><property name="48" value="eNom, Inc." /><property name="49" value="GMO Internet, Inc. d/b/a Onamae.com" /><property name="52" value="Abacus America, Inc." /><property name="53" value="A Technology Company, Inc." /><property name="54" value="Signature Domains, LLC" />
String line = null; InputStreamReader b = new InputStreamReader(new FileInputStream( new File("d:/a.txt")), "gbk");BufferedReader br = new BufferedReader(b);FileWriter fw = new FileWriter("d:\\b.txt");while((line = br.readLine()) != null) {String arr[] = line.split("\\$");String temp = "";if(arr.length >= 2) {for(int i = 1;i < arr.length;i++) {if(temp.equals("")) {temp = temp + arr[i];}else {temp = temp + " " + arr[i];}}}System.out.println("temp:" + temp);String sb = "<property name="" + arr[0] +"" value="" +temp + "" />";fw.write(sb);fw.write("\n");}br.close();fw.flush();fw.close();