這篇文章主要介紹“JDom如何新建和讀取xml”,在日常操作中,相信很多人在JDom如何新建和讀取xml問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JDom如何新建和讀取xml”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
公司主營業(yè)務:成都網(wǎng)站設計、網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出曲水免費做網(wǎng)站回饋大家。
//package xml;
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
//import org.jdom.*;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class test {
private static String titlename;
private String chapter;
private String topic;
public static void main(String [] msg){
wriXml();
// try{
readXml();
// }catch (Exception e){e.printStackTrace();}
}
public static void readXml() {
SAXBuilder builder=new SAXBuilder();
try{
Document document=builder.build(new File("contents.xml"));
Element root=document.getRootElement();
Element title=root.getChild("title");
System.out.println(title.getText());
Element contents=root.getChild("contents");
List lis=contents.getChildren("chapter");
Iterator it=lis.iterator();
while(it.hasNext()){
Element chapter=(Element) it.next();
System.out.println(chapter.getAttribute("title"));
}
}catch(Exception e){e.printStackTrace();}
}
public static void wriXml(){
Element ele=new Element("boot");
Document doc=new Document(ele);
ele.addAttribute(new Attribute("vin","123456789"));
ele.addContent(new Element("first").addContent("guoxb"));
ele.addContent(new Element("second").addContent("30"));
ele.addContent(new Element("third").addContent("univercity"));
ele.addContent(new Element("first").addContent("girlfri").addAttribute("name","22"));
ele.addContent(new Element("right"));
ele.addContent(new Comment("這是一條主釋"));
Element gg=ele.getChild("right");
gg.addContent(new Element("pkg").addContent("thhhhhhhh"));
//訪問子元素
Element fir=ele.getChild("first");
//去處子元素
boolean remo=ele.removeChild("first");
//打印輸出xml
try{
XMLOutputter out=new XMLOutputter(" ",true);
out.setEncoding("gb2312");
out.output(doc,System.out);
//保存到xml文檔中
FileWriter fi=new FileWriter("./gxb.xml");
out.output(doc,fi);
fi.close(); }catch (Exception e){e.printStackTrace();}
}
}
[@more@]
到此,關于“JDom如何新建和讀取xml”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
當前名稱:JDom如何新建和讀取xml
網(wǎng)站URL:http://vcdvsql.cn/article40/iijieo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、網(wǎng)站設計公司、網(wǎng)頁設計公司、虛擬主機、搜索引擎優(yōu)化、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)