1.[代码][Java]代码
package com.pjiang.xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.StringWriter;
import java.util.Iterator;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class JavaCreateDom4j {
public static Document docStr(){
Document doc=null;
String docStr="000000086480024453" +
"000000086480024453雷雪梅" +
"CHAN CHAN CHAN2女" +
"20110101CN湖南省长沙市市辖区" +
"01汉族其它430721196111212506" +
"市医保06" +
"长沙市居民1212121" +
"湖南省安乡县下渔口镇永南村03006号" +
"1171"+
"湖南省长沙市市辖区" +
"云南省德宏傣族景颇族自治州西藏自治区" +
"拉萨市" +
"堆龙德庆县121" +
"西藏自治区拉萨市堆龙德庆县" +
"常州5401251" +
"11"+
"更新";
try {
doc = DocumentHelper.parseText(docStr);
OutputFormat format = new OutputFormat(" ", true);
format.setEncoding("GBK");
StringWriter out = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(out, format);
xmlWriter.write(doc);
xmlWriter.flush();
String s = out.toString();
System.out.println(s);
save("目前的字符串------------>"+s);
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}
public static void main(String args[]){
Document doc=docStr();
// doc = DocumentHelper.parseText(docStr);
System.out.println(doc);
Element ele=doc.getRootElement();
Iterator iter = ele.elementIterator("PatientInfo");
while (iter.hasNext()) {
Element recordEle = (Element) iter.next();
String patientid = recordEle.elementTextTrim("PatientId");
String IccardNo= recordEle.elementTextTrim("IccardNo");
save("解析之后的两个值----------------->");
System.out.println("Patientid-->"+patientid);
save("Patientid-->"+patientid);
System.out.println("IccardNo----------->"+IccardNo);
save("IccardNo----------->"+IccardNo);
System.out.println(JavaCreateDom4j.class.getProtectionDomain().getCodeSource()
.getLocation().getFile().toString());
}
}
private static void save(String str) {
try {
String path = JavaCreateDom4j.class.getProtectionDomain().getCodeSource()
.getLocation().getFile();
path = path.substring(1, path.lastIndexOf("/") + 1) + "Save.txt";
File f1 = new File(path);
if (!f1.exists())
f1.createNewFile();
FileInputStream fis = new FileInputStream(path);
long size = fis.available() / 1000;
if (size > 10240) {
FileWriter writer = new FileWriter(path, false);
writer.write("");
}
FileWriter writer = new FileWriter(path, true);
writer.write(str);
writer.write("\r\n");
writer.close();
} catch (Exception e) {
System.out.print("---");
}
}
}