Jdom操作、創建和解析XML
需要導入Jdom的一個jar包;1、Jdom創建XML
import java.io.FileWriter;
import org.jdom.Attribute; import org.jdom.Comment; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.Format; import org.jdom.output.XMLOutputter;
public class JDomTest1 { public static void main(String[] args) throws Exception { //創建文檔 Document document = new Document(); //創建文檔根元素 Element root = new Element("root"); //向文檔呢添加根元素 document.addContent(root); //添加注釋 Comment comment = new Comment("This is my comments"); //在根元素下添加注釋 root.addContent(comment); //創建一個元素 Element e = new Element("hello"); //給元素添加屬性的第一種方式,它有返回值,返回一個Element對象 e.setAttribute("sohu", "www.sohu.com"); //給根元素添加屬性 root.addContent(e);
Element e2 = new Element("world"); //創建元素屬性的第二種方式 Attribute attr = new Attribute("test", "hehe"); //給元素添加屬性 e2.setAttribute(attr); //將元素添加到根元素下 e.addContent(e2); //添加元素屬性和文本內容,屬于方法鏈的編程風格 e2.addContent(new Element("aaa").setAttribute("a", "b") .setAttribute("x", "y").setAttribute("gg", "hh").setText("text content"));
//設置其格式,一般還有一個沒有縮進與空格的格式getRawFormat,主要用于XML的網絡傳輸,因為它沒有空格,減少網絡傳輸的數據量。 Format format = Format.getPrettyFormat();
//設置元素前面空格 format.setIndent(" "); //設置編碼方式 // format.setEncoding("gbk"); //將XML文檔輸出 XMLOutputter out = new XMLOutputter(format); out.output(document, new FileWriter("jdom.xml"));
} }
2、Jdom解析XML
import java.io.File; import java.io.FileOutputStream; import java.util.List;
import org.jdom.Attribute; 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 JDomTest2 { public static void main(String[] args) throws Exception { //JDOM從XML中解析 SAXBuilder builder = new SAXBuilder(); //獲得XML文檔對象 Document doc = builder.build(new File("jdom.xml")); //得到文檔根元素 Element element = doc.getRootElement();
System.out.println(element.getName()); //得到根元素下的hello元素 Element hello = element.getChild("hello");
System.out.println(hello.getText()); //得到hello子元素下的屬性 List list = hello.getAttributes();
//得到hello元素下屬性的名字和值 for(int i = 0 ;i < list.size(); i++) { Attribute attr = (Attribute)list.get(i);
String attrName = attr.getName(); String attrValue = attr.getValue();
System.out.println(attrName + "=" + attrValue); } //刪除hello下的world子元素 hello.removeChild("world");
//將文檔保存到另一個文件 XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent(" "));
out.output(doc, new FileOutputStream("jdom2.xml"));
} }
3、綜合實例
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List;
import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter;
public class XMLTest { public static void main(String[] args) { modifyXML(); }
public static void createXML() { Element root = new Element("學生信息"); Element stu1 = new Element("學生"); Element stu2 = new Element("學生"); Element name1 = new Element("姓名"); Element sex1 = new Element("性別"); Element age1 = new Element("年齡"); Element name2 = new Element("姓名"); Element sex2 = new Element("性別"); Element age2 = new Element("年齡");
name1.setText("小明"); sex1.setText("男"); age1.setText("19");
name2.setText("小花"); sex2.setText("女"); age2.setText("20");
root.addContent(stu1); root.addContent(stu2); stu1.addContent(name1); stu1.addContent(sex1); stu1.addContent(age1); stu2.addContent(name2); stu2.addContent(sex2); stu2.addContent(age2);
Document doc = new Document(root); Format f = Format.getPrettyFormat(); f.setEncoding("gb2312"); XMLOutputter out = new XMLOutputter(f);
try { out.output(doc, new FileOutputStream(new File("c:/myxml.xml"))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
}
public static void searchXML() { SAXBuilder build = new SAXBuilder(); try { Document doc = build.build("c:/myxml.xml"); Element e = doc.getRootElement();
List<Element> list = e.getChildren(); for (Element ele : list) { System.out.println(ele.getChild("姓名").getName() + ":"
+ ele.getChild("姓名").getText()); System.out.println(ele.getChild("性別").getName() + ":" + ele.getChild("性別").getText()); System.out.println(ele.getChild("年齡").getName() + ":" + ele.getChild("年齡").getText());
} } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public static void addElement() { SAXBuilder build=new SAXBuilder(); try { Document doc=build.build("c:/myxml.xml"); Element root=doc.getRootElement(); Element stu=root.getChild("學生"); Element cla=new Element("班級"); cla.addContent("2班"); stu.addContent(cla); XMLOutputter out=new XMLOutputter(); out.output(doc,new FileOutputStream(new File("c:/myxml.xml")));
} catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
}
public static void modifyXML() { SAXBuilder build=new SAXBuilder(); Document doc; try { doc = build.build("c:/myxml.xml"); Element root=doc.getRootElement(); Element stu=root.getChild("學生"); Element age=stu.getChild("年齡2"); age.setName("年齡"); age.setText("10"); List<Element> list=stu.getChildren();
for(Element e:list) { if(e.getName().equals("")); // { //// Element height=new Element("身高"); //// height.setText("175厘米"); //// root.addContent(height); // stu.removeChild(str); // } } XMLOutputter out=new XMLOutputter(); out.output(doc,new FileOutputStream(new File("c:/myxml.xml"))); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
}</pre>