国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

dom4j 復(fù)制xml文件/復(fù)制xml文件中選中節(jié)點(diǎn)到另一個(gè)xml文件

2019-11-08 03:23:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
引入pom依賴:<dependency>   <groupId>dom4j</groupId>   <artifactId>dom4j</artifactId>   <version>1.6.1</version></dependency>1、xml文件復(fù)制:public void copyXml(String sourcefile,String destfile){try { File source = new File(sourcefile);if(source.exists() && source.isFile()){File dest=new File(destfile);if(dest.exists()){dest.delete();}dest.createNewFile();SAXReader saxReader=new SAXReader();Document document=saxReader.read(source);FileOutputStream outputStream=new FileOutputStream(destfile);//漂浪格式,有格式,有空格OutputFormat format=OutputFormat.createPRettyPrint();//緊湊格式,無(wú)空格換行//OutputFormat format=OutputFormat.createCompactFormat();format.setEncoding("UTF-8");XMLWriter writer=new XMLWriter(outputStream, format);writer.write(document);writer.close();} } catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("xml文件拷貝異常");e.printStackTrace();} }調(diào)用:copyXml("C:/test.xml", "C:/test_cp.xml");2、xml文件拷貝一個(gè)xml中某個(gè)節(jié)點(diǎn)到另一個(gè)xml文件public void copyNodeToOther(){//目標(biāo)xml文件File resxmlfile=new File("C://Users//Administrator//Desktop//test//test.xml"); SAXReader reader2=new SAXReader();Document document2=reader2.read(resxmlfile);Element root2= document2.getRootElement();//源xml文件File batfile=new File("C://Users//Administrator//Desktop//test//test_bat.xml"); SAXReader reader=new SAXReader();Document document=reader.read(batfile);Element root= document.getRootElement();List<Element> scenelist=root.elements("scene");for(Element scenenode:scenelist){System.out.println("節(jié)點(diǎn)名稱:"+scenenode.getName());Attribute attribute=scenenode.attribute("title");System.out.println(attribute.getName());System.out.println(attribute.getNodeTypeName());System.out.println(attribute.getValue());System.out.println(attribute.getQName());String val=attribute.getValue();if(val.startsWith("111_")){Element sceneClone=(Element) scenenode.clone();root2.add(sceneClone);} }FileOutputStream outputStream=new FileOutputStream(resxmlfile); OutputFormat format=OutputFormat.createPrettyPrint();format.setEncoding("UTF-8");XMLWriter writer=new XMLWriter(outputStream, format);writer.write(document);writer.close();}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 慈溪市| 芮城县| 沐川县| 尉氏县| 博兴县| 乳源| 同德县| 涪陵区| 吉木乃县| 惠来县| 英德市| 云阳县| 鹤山市| 南康市| 邵阳县| 玉山县| 乌拉特前旗| 太白县| 新闻| 岳西县| 库尔勒市| 汝阳县| 江津市| 武川县| 翁源县| 改则县| 霍山县| 松潘县| 黄陵县| 南木林县| 三门峡市| 阳东县| 宝山区| 化州市| 本溪市| 屏山县| 唐山市| 吉首市| 麻阳| 南和县| 衡阳市|