日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用JDOM2.0.4 操作/解析xml

發(fā)布時間:2023/12/3 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用JDOM2.0.4 操作/解析xml 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載自? ?使用JDOM2.0.4 操作/解析xml

一、解析xml內(nèi)容

xml文件內(nèi)容:

<?xml version="1.0" encoding="utf-8"?> <RETVAL success='true'> <Shop><sid>1</sid><name>北京鑫和易通貿(mào)易有限公司</name></Shop> <Shop><sid>2</sid><name>張家口市金九福汽車服務(wù)有限公司</name></Shop> </RETVAL>

?

解析的java代碼

import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import org.xml.sax.InputSource;public Class Test{public static List test() throws Exception{List result = new ArrayList();SAXBuilder builder = new SAXBuilder();StringReader read = new StringReader(xmlDoc);InputSource source = new InputSource(read);Document document = builder.build(source);Element root = document.getRootElement();// 獲得根節(jié)點String ifSuccess = root.getAttributeValue("success");if (!"true".equals(ifSuccess)) {// 不成功直接返回throw new Exception("從接口獲取全部經(jīng)銷商數(shù)據(jù)時不成功!");}List<Element> childrenList = root.getChildren();for (Element e : childrenList) {Map elementMap = new HashMap<String, String>();elementMap.put("sid", e.getChildText("sid")); // 經(jīng)銷商idelementMap.put("name", e.getChildText("name")); // 經(jīng)銷商名稱 result.add(elementMap);}return result;} }

?


?

二、解析和操作xml文件

xml文件內(nèi)容

<?xml version="1.0" encoding="UTF-8"?> <root><person id="1"><username>張三</username><password>123123</password></person><person id="2"><username>1111111112</username><password>password2</password></person> </root>

?

JDOM構(gòu)建document對象的方法

Document build(java.io.File file) //This builds a document from the supplied filename. Document build(org.xml.sax.InputSource in) //This builds a document from the supplied input source. Document build(java.io.InputStream in) //This builds a document from the supplied input stream. Document build(java.io.InputStream in, java.lang.String systemId) //This builds a document from the supplied input stream. Document build(java.io.Reader characterStream) //This builds a document from the supplied Reader. Document build(java.io.Reader characterStream, java.lang.String systemId) //This builds a document from the supplied Reader. Document build(java.lang.String systemId) //This builds a document from the supplied URI. Document build(java.net.URL url) //This builds a document from the supplied URL.

?

解析、新增、修改、刪除xml節(jié)點屬性的java

package test;import java.io.FileWriter; import java.util.List;import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter;public class Test {/*** @param args* @throws Exception */public static void main(String[] args) throws Exception {SAXBuilder builder = new SAXBuilder();//Document doc = builder.build(new File("src/test.xml"));//Document doc = builder.build(new FileInputStream("src/test.xml"));//Document doc = builder.build(new FileReader("src/test.xml"));//Document doc = builder.build(new URL("http://localhost:8080/jdomTest/test.xml"));Document doc = builder.build("src/test.xml");//readXmlFile(doc);//addXmlElement(doc);//updateXmlElement(doc); deleteXmlElement(doc);}/*** 解析xml文件* @throws Exception*/public static void readXmlFile(Document doc) throws Exception {Element root = doc.getRootElement(); //獲取根元素 System.out.println("---獲取第一個子節(jié)點和子節(jié)點下面的節(jié)點信息------");Element e = root.getChild("person"); //獲取第一個子元素System.out.println("person的屬性id的值:"+e.getAttributeValue("id")); //獲取person的屬性值for(Element el: e.getChildren()){System.out.println(el.getText());//第一次輸入張三 第二次輸出123123System.out.println(el.getChildText("username"));//這里這么寫會是nullSystem.out.println(el.getChildText("password"));//這里這么寫會是null }System.out.println("---直接在根節(jié)點下就遍歷所有的子節(jié)點---");for(Element el: root.getChildren()){System.out.println(el.getText());//這里輸出4行空格System.out.println(el.getChildText("username"));//輸出張三 & 1111111112System.out.println(el.getChildText("password"));//輸出123123 & password2 }}/*** 新增節(jié)點* @throws Exception*/public static void addXmlElement(Document doc) throws Exception {Element root = doc.getRootElement(); //獲取根元素 Element newEle = new Element("person");//設(shè)置新增的person的信息newEle.setAttribute("id","88888");Element chiledEle = new Element("username"); //設(shè)置username的信息chiledEle.setText("addUser");newEle.addContent(chiledEle);Element chiledEle2 = new Element("password"); //設(shè)置password的信息chiledEle2.setText("addPassword");newEle.addContent(chiledEle2);root.addContent(newEle);XMLOutputter out = new XMLOutputter();out.setFormat(Format.getCompactFormat().setEncoding("GBK"));//設(shè)置UTF-8編碼,理論上來說應(yīng)該不會有亂碼,但是出現(xiàn)了亂碼,故設(shè)置為GBKout.output(doc, new FileWriter("src/test.xml")); //寫文件 }/*** 更新節(jié)點* @param doc* @throws Exception*/public static void updateXmlElement(Document doc) throws Exception {Element root = doc.getRootElement(); //獲取根元素//循環(huán)person元素并修改其id屬性的值for(Element el : root.getChildren("person")){el.setAttribute("id","haha");}//循環(huán)設(shè)置username和password的文本值和添加屬性for(Element el: root.getChildren()){el.getChild("username").setAttribute("nameVal", "add_val").setText("update_text"); el.getChild("password").setAttribute("passVal", "add_val").setText("update_text"); }XMLOutputter out = new XMLOutputter();out.setFormat(Format.getCompactFormat().setEncoding("GBK"));//設(shè)置UTF-8編碼,理論上來說應(yīng)該不會有亂碼,但是出現(xiàn)了亂碼,故設(shè)置為GBKout.output(doc, new FileWriter("src/test.xml")); //寫文件 }/*** 刪除節(jié)點和屬性* @param doc* @throws Exception*/public static void deleteXmlElement(Document doc) throws Exception {Element root = doc.getRootElement(); //獲取根元素 List<Element> personList = root.getChildren("person");//循環(huán)person元素,刪除person的id為1的id屬性以及username子節(jié)點for(Element el : personList){if(null!=el.getAttribute("id") && "1".equals(el.getAttribute("id").getValue())){el.removeAttribute("id");el.removeChild("username");}}//循環(huán)person元素,刪除person的id為2的節(jié)點for(int i=0; i<personList.size(); i++){Element el = personList.get(i);if(null!=el.getAttribute("id") && "2".equals(el.getAttribute("id").getValue())){root.removeContent(el);//從root節(jié)點上刪除該節(jié)點//警告:此處刪除了節(jié)點可能會使personList的長度發(fā)生變化而發(fā)生越界錯誤,故不能寫成for(int i=0,len=personList.size(); i<len; i++) }}XMLOutputter out = new XMLOutputter();out.setFormat(Format.getCompactFormat().setEncoding("GBK"));//設(shè)置UTF-8編碼,理論上來說應(yīng)該不會有亂碼,但是出現(xiàn)了亂碼,故設(shè)置為GBKout.output(doc, new FileWriter("src/test.xml")); //寫文件 } }

總結(jié)

以上是生活随笔為你收集整理的使用JDOM2.0.4 操作/解析xml的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。