第一篇博客,不知道怎么布局,囧。
需要jar外部jar——jdom.2.0.4.jar
下载地址:http://download.csdn.net/download/liu119361940/5348593
好了直接上代码
package com.vision.backktv.xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.ProcessingInstruction;
import org.jdom2.Text;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class XMLWriterAndReader {
public static void main(String[] args) throws FileNotFoundException, IOException, JDOMException, URISyntaxException{
// String xmlCreatePath = "jdom.xml";
// String xmlEditPath = "jdomExit.xml";
String xmlCreatePath = "D:/jdom.xml";
String xmlEditPath = "D:/jdomExit.xml";
createXml(xmlCreatePath); //创建xml
System.out.println("创建后直接读取》》》》》》》》》》》》》");
System.out.println("创建后直接读取》》》》》》》》》》》》》");
System.out.println("创建后直接读取》》》》》》》》》》》》》");
readXml(xmlCreatePath); //读取xml
System.out.println("修改后读取》》》》》》》》》》》》》");
System.out.println("修改后读取》》》》》》》》》》》》》");
System.out.println("修改后读取》》》》》》》》》》》》》");
exitXml(xmlCreatePath,xmlEditPath); //修改并读取xml
}
/**
* 创建xml
* @param xmlCreatePath
* @throws FileNotFoundException
* @throws IOException
*/
public static void createXml(String xmlCreatePath) throws FileNotFoundException, IOException{
// 创建根节点 list;
Element root = new Element("list");
// 根节点添加到文档中;
Document Doc = new Document(root);
// 此处 for 循环可替换成 遍历 数据库表的结果集操作;
for (int i = 0; i < 2; i++) {
// 创建节点 user;
Element elements = new Element("user");
// 给 user 节点添加属性 id;
elements.setAttribute("id", "" + i);
elements.addContent("我是根"+i);//在根节点处添加内容
// 给 user 节点添加子节点并赋值;
// new Element("name")中的 "name" 替换成表中相应字段,setText("xuehui")中 "xuehui 替换成表中记录值;
elements.addContent(new Element("name").setText("xuehui"));
elements.addContent(new Element("age").setText("28"));
elements.addContent(new Element("sex").setText("Male"));
// elements.addContent((new Element("new1").setText("新1")).addContent((new Element("new11")).setText("新1中的新11")));
// elements.addContent(((new Element("new1")).setAttribute("new1Attr","新1中的attr苹果").setText("新1")).addContent((new Element("new11")).setText("新1中的苹果apple")));
// 给父节点list添加user子节点;
//设置输出的格式及编码
root.addContent(elements);
}
XMLOutputter XMLOut = new XMLOutputter();
Format format = Format.getCompactFormat(); //将xml格式化
format.setEncoding("UTF-8");
// format.setIndent("\n"); //缩进2个空格后换行,空格数自己设
format.setIndent("\t");
XMLOut.setFormat(format);
XMLOut.output(Doc, new FileOutputStream(xmlCreatePath));
}
/**
* 读取xml
* @param xmlCreatePath
* @throws FileNotFoundException
* @throws JDOMException
* @throws IOException
*/
public static void readXml(String xmlCreatePath) throws FileNotFoundException, JDOMException, IOException{
/*
* 用无变元构造函数构造一个SAXBuilder对象, 用sax解析器从文件中构造文档,
* SAXBuilder侦听sax事件并从内存中建立一个相应的文档
*/
SAXBuilder sb = new SAXBuilder();
// 创建文档
Document doc = sb.build(new FileInputStream(xmlCreatePath));
// 获得这个文档的根元素
Element root = doc.getRootElement();
repeat(root);
// repeat2(root);
}
/**
* 编辑xml
* @param xmlCreatePath
* @param xmlEditPath
* @throws FileNotFoundException
* @throws JDOMException
* @throws IOException
* @throws URISyntaxException
*/
public static void exitXml(String xmlCreatePath,String xmlEditPath) throws FileNotFoundException, JDOMException, IOException, URISyntaxException{
/*
* 用无变元构造函数构造一个SAXBuilder对象, 用sax解析器从文件中构造文档,
* SAXBuilder侦听sax事件并从内存中建立一个相应的文档
*/
SAXBuilder sb = new SAXBuilder();
// 创建文档
Document doc = sb.build(new FileInputStream(xmlCreatePath));
// 加入一条处理指令
ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", "href=\"bookList.html.xsl\" type=\"text/xsl\"");
// 把这条处理指令,加入文档中
doc.addContent(pi);
// 获得这个文档的根元素
Element root = doc.getRootElement();
//在根节点添加节点
root.addContent((new Element("新加入的节点1")).setText("新加成功1桃子"));
root.addContent((new Element("新加入的节点2")).setText("新加成功2桃子"));
//在user节点添加属性
List<Element> eles = root.getChildren("user");
Element e1 = eles.get(0);
e1.addContent((new Element("editXml")).setAttribute("属性key", "value").setText("节点值"));
Element e2 = eles.get(1);
e2.getChild("sex").setAttribute("性别","请填写性别").setText((e2.getChild("sex")).getText()+"女");
XMLOutputter XMLOut = new XMLOutputter();
Format format = Format.getCompactFormat(); //将xml格式化
format.setEncoding("UTF-8");
// format.setIndent("\n"); //缩进2个空格后换行,空格数自己设
format.setIndent("\t");
XMLOut.setFormat(format);
XMLOut.output(doc, new FileOutputStream(xmlEditPath));
// 创建文档,重新读取
Document doc2 = sb.build(new FileInputStream(xmlEditPath));
// 获得这个文档的根元素
Element root2 = doc.getRootElement();
repeat(root2);
// repeat2(root2);
}
/**
* 递归取出xml中的值方法1
*/
public static void repeat(Element ele){
if(checkChild(ele)){
System.out.println("Element: " + ele.getName());
List<Attribute> attr = ele.getAttributes();
for(int n=0;n<attr.size();n++){
System.out.println("Attribute: " + (attr.get(n)).getName()+"=="+(attr.get(n)).getValue());
// System.out.println("Text1: " + ele.getTextTrim());
System.out.println("Text1: " + ele.getTextNormalize());
}
List<Element> list = getChildren(ele);
Iterator it = list.iterator();
while (it.hasNext()) {
Element element = (Element) it.next();
repeat(element);
}
}else{
System.out.println("Element: " + ele.getName());
System.out.println("Text2: " + ele.getTextNormalize());
List<Attribute> attr = ele.getAttributes();
for(int n=0;n<attr.size();n++){
System.out.println("Attribute: " + (attr.get(n)).getName()+"=="+(attr.get(n)).getValue());
}
}
}
/**
* 递归取出xml中的值方法2
*/
public static void repeat2(Element ele){
if(checkChild(ele)){
System.out.println("Element: " + ele.getName());
List<Attribute> attr = ele.getAttributes();
for(int n=0;n<attr.size();n++){
System.out.println("Attribute1: " + (attr.get(n)).getName()+"=="+(attr.get(n)).getValue());
System.out.println("Attribute1: " + (attr.get(n)).getName()+"=="+ele.getAttributeValue((attr.get(n)).getName()));
// System.out.println("Text1: " + ele.getTextTrim());
System.out.println("Text1: " + getText(ele));
}
List<Element> list = getChildren(ele);
Iterator it = list.iterator();
while (it.hasNext()) {
Element element = (Element) it.next();
repeat2(element);
}
}else{
System.out.println("Element: " + ele.getName());
System.out.println("Text2: " + getText(ele));
List<Attribute> attr = ele.getAttributes();
for(int n=0;n<attr.size();n++){
System.out.println("Attribute2: " + (attr.get(n)).getName()+"=="+(attr.get(n)).getValue());
System.out.println("Attribute2: " + (attr.get(n)).getName()+"=="+ele.getAttributeValue((attr.get(n)).getName()));
}
}
}
/**
* repeat备份
* @param element
*/
public static void repeat备份(Element element){
List<Element> list = getChildren(element);
Iterator it = list.iterator();
while (it.hasNext()) {
Object o = it.next();
if (o instanceof Text){/*使用instanceof 来获得所需要的内容*/
Text t=(Text)o;
System.out.println("Text: " + t.getText());
}else if(o instanceof Attribute){
System.out.println("Attribute: " + o);
}else if (o instanceof Element){
if(checkChild((Element)o)){
System.out.println("Element: " + ((Element) o).getName());
List<Attribute> attr = ((Element) o).getAttributes();
for(int n=0;n<attr.size();n++){
System.out.println("Attribute: " + (attr.get(n)).getName()+"=="+(attr.get(n)).getValue());
}
repeat((Element)o);
}else{
System.out.println("Element: " + ((Element) o).getName());
System.out.println("Text2: " + ((Element) o).getText());
List<Attribute> attr = ((Element) o).getAttributes();
for(int n=0;n<attr.size();n++){
System.out.println("Attribute: " + (attr.get(n)).getName()+"=="+(attr.get(n)).getValue());
}
}
}
}
}
/**
* 取得节点的子节点list
* @param element
* @return
*/
public static List<Element> getChildren(Element element){
List<Element> list=element.getChildren();
if(!list.isEmpty()){
return list;
}else{
return null;
}
}
/**
* 检测节点是否还有子节点
* @param element
* @return
*/
public static boolean checkChild(Element element){
if(!(element.getChildren()).isEmpty()){
return true;
}else{
return false;
}
}
/**
* 取得节点的值
* @param element
* @return
*/
public static String getText(Element element){
return element.getTextNormalize();
}
/**
* 取得某个节点某个属性的值
* @param element
* @param attname
* @return
*/
public static String getAttribute(Element element,String attname){
return element.getAttributeValue(attname);
}
}