package com.donson.common;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.FileInputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class XmlOperate {
public static void main(String[] args) {
XmlOperate xo= new XmlOperate ();
xo.add();
}
//访问xml文件
private void printXmlContent(){
SAXBuilder builder = new SAXBuilder();
String fileContent = getXmlContent();
try{
Reader readStr = new StringReader(fileContent);
Document doc = builder.build(readStr);
Element root = doc.getRootElement();
List childList = root.getChildren();
System.out.println("list="+childList.size());
for(int i=0;i Element child = (Element)childList.get(i);
System.out.println("ok="+child.getAttributeValue("ok"));
System.out.println("id="+child.getChildTextTrim("id"));
System.out.println("address="+child.getChildTextTrim("address"));
System.out.println("text="+child.getTextTrim());
Element cd = child.getChild("subx");//获取该节点下面的子节点
if(cd!=null){
System.out.println("cd-id="+cd.getChildText("id"));
System.out.println("cd-value="+cd.getChildText("value"));
}
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//添加操作
private void add(){
SAXBuilder builder = new SAXBuilder();
String fileContent = getXmlContent();
try{
Reader readStr = new StringReader(fileContent);
Document doc = builder.build(readStr);
Element root = doc.getRootElement();
List childList = root.getChildren();
System.out.println("list="+childList.size());
Element el = new Element("subroot");
el.setAttribute("ok","true");
el.setText("xxxx");
Element id = new Element("id");
id.setText("25");
el.addContent(id);
Element name = new Element("name");
name.setText("guolp25");
el.addContent(name);
Element address = new Element("address");
address.setText("深圳25");
Element sex = new Element("sex");
sex.setText("F25");
el.addContent(sex);
Element subx = new Element("subx");
Element sid = new Element("id");
sid.setText("s11");
subx.addContent(sid);
Element scontent = new Element("content");
scontent.setText("最好操作");
subx.addContent(scontent);
subx.setAttribute("ok","true");
el.addContent(subx);
childList.add(el);
Format format = Format.getCompactFormat();
format.setEncoding("gbk");
format.setIndent(" ");
XMLOutputter XMLOut = new XMLOutputter(format);
FileOutputStream writer = new FileOutputStream("d:\\test.xml");
XMLOut.output(doc, writer);
writer.close();
readStr.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
private String getXmlContent(){
String xmlfilepath = "d:\\test.xml";
File file = new File(xmlfilepath);
if(!file.exists() || file.length()<=0){
return null;
}else{
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
long fileLength = file.length();
byte[] fileBytes = new byte[(int)fileLength];
fis.read(fileBytes);
String fileData = new String(fileBytes);
return fileData;
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
try{
if(fis != null){
fis.close();
fis = null;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}