锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
聽聽 1.SAX: Simple API for XML
聽聽聽聽聽聽聽聽 http://www.saxproject.org/
聽聽聽聽聽 2. DOM:聽 Document Object聽 Model
聽聽聽聽聽聽聽聽聽聽聽聽聽 http://www.w3.org/DOM
聽聽聽聽聽 3. JDOM
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 O鈥橰eilly涓や綅浣滃寮鍙戠殑閽堝Java鐨刋ML瑙f瀽鍖?br />聽聽聽聽聽聽聽聽 http://www.jdom.org
聽聽 鍓嶄袱縐嶇敤璧鋒潵姣旇緝楹葷儲,涓嬮潰鏄敤JDOM鎿嶄綔XML鐨勪富瑕佷唬鐮?
聽聽聽 涓.閲囩敤JDom璇誨彇XML鏂囦歡
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //涓嶴AX閰嶅悎浣跨敤
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 SAXBuilder sax = new SAXBuilder();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //璇誨彇XML鏂囦歡錛屾瀯寤篋ocument瀵硅薄
聽聽聽聽聽聽聽聽聽聽聽聽聽 Document doc = sax.build("test.xml");
聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽 //鑾峰彇鏍瑰厓绱?br />聽聽聽聽聽聽聽聽聽聽聽聽 Element root = doc.getRootElement();
聽聽聽聽聽聽聽聽聽聽聽聽聽 //鑾峰彇鏍瑰厓绱犱笅鐨勫瓙鍏冪礌
聽聽聽聽聽聽聽聽聽聽聽 List list = root.getChildren();
聽聽 浜?閲囩敤JDom鍐欏叆XML鏂囦歡
聽聽聽聽聽聽 Document doc = new Document();
聽聽聽聽聽聽聽聽 //鍒涘緩鏍瑰厓绱爏tudents
聽聽聽聽聽聽 Element root = new Element("students");
聽聽聽聽聽聽聽 Element s1 = new Element("student");
聽聽聽聽聽聽聽 Element s2 = new Element("student");
聽聽聽聽聽聽聽 root.addContent(s1);
聽聽聽聽聽聽聽 root.addContent(s2);
聽聽聽聽聽聽聽 //////////////////////////////////////
聽聽聽聽聽聽聽 Element n1 = new Element("name");
聽聽聽聽聽聽聽 Element sc1 = new Element("score");
聽聽聽聽聽聽聽 s1.addContent(n1);
聽聽聽聽聽聽聽 s1.addContent(sc1);
聽聽聽聽聽聽聽 n1.setText("寮犱笁");
聽聽聽聽聽聽聽 sc1.setText("80");
聽聽聽聽聽聽聽 s1.setAttribute("id", "101");
聽聽聽聽聽聽聽 s1.setAttribute("sex", "鐢?);
聽聽聽聽聽聽聽 ///////////////////////////////////////
聽聽聽聽聽聽聽 Element n2 = new Element("name");
聽聽聽聽聽聽聽 Element sc2 = new Element("score");
聽聽聽聽聽聽聽 Comment c = new Comment("鍥犱負浣滃紛琚彂鐜幫紝鎵i櫎10鍒?);
聽聽聽聽聽聽聽 s2.addContent(n2);
聽聽聽聽聽聽聽 s2.addContent(c);
聽聽聽聽聽聽聽 s2.addContent(sc2);
聽聽聽聽聽聽聽 n2.setText("鏉庡洓");
聽聽聽聽聽聽聽 sc2.setText("40");
聽聽聽聽聽聽聽 s2.setAttribute("id", "102");
聽聽聽聽聽聽聽 doc.addContent(root);
聽聽聽聽聽聽聽 ////////////////////////////////////
聽聽聽聽聽聽聽 XMLOutputter xmlOut = new XMLOutputter();
聽聽聽聽聽聽聽 xmlOut.setFormat(Format.getPrettyFormat());
聽聽聽聽聽聽聽 try {
聽聽聽聽聽聽聽聽聽聽聽 xmlOut.output(doc, new FileOutputStream("test.xml"));
聽聽聽聽聽聽聽 } catch (IOException ex) {
聽聽聽聽聽聽聽聽聽聽聽 ex.printStackTrace();
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽 ////////////////////////////////////
聽聽聽聽聽聽聽 鍒氬紑濮嬪涔燲ML,鏈夋葷粨涓嶅綋涔嬪,璇鋒寚鍑?