码迷,mamicode.com
首页 > 移动开发 > 详细

Dom4jDemo应用-保存手机信息

时间:2019-01-01 21:05:16      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:src   ++   结束   --   doc   als   color   try   writer   

---恢复内容开始---

  1 import java.io.File;
  2 import java.io.FileNotFoundException;
  3 import java.io.FileOutputStream;
  4 import java.io.IOException;
  5 import java.io.OutputStreamWriter;
  6 import java.io.UnsupportedEncodingException;
  7 import java.util.ArrayList;
  8 import java.util.Iterator;
  9 import java.util.List;
 10 
 11 import org.dom4j.Document;
 12 import org.dom4j.DocumentException;
 13 import org.dom4j.Element;
 14 import org.dom4j.io.OutputFormat;
 15 import org.dom4j.io.SAXReader;
 16 import org.dom4j.io.XMLWriter;
 17 
 18 public class Dom4jDemo2 {
 19     private Document document;
 20 
 21     // 获得document对象
 22     public void loadDocument() {
 23         SAXReader saxReader = new SAXReader();  //相当于一个解析器
 24         try {
 25             document = saxReader.read(new File("newInfo.xml"));  //解析获得document对象
 26         } catch (DocumentException e) {
 27             e.printStackTrace();
 28         }
 29     }
 30 
 31     // 显示手机的品牌及型号
 32     public void showPhoneInfo() {
 33         // 获取XML根节点
 34         Element root = document.getRootElement();
 35         // 获取所有<Brand>
 36         Iterator eleBrands = root.elementIterator();
 37         while (eleBrands.hasNext()) {
 38             Element brand = (Element) eleBrands.next();
 39             System.out.println(brand.attributeValue("name"));
 40             //获取所有的<Type>
 41             Iterator eleTypes = brand.elementIterator();
 42             while (eleTypes.hasNext()) {
 43                 Element type = (Element) eleTypes.next();
 44                 System.out.println("\t" + type.attributeValue("name"));
 45             }
 46         }
 47     }
 48 
 49     /**
 50      * 把xml里手机品牌及型号放入集合里输出
 51      * @return
 52      */
 53     public  List<Brand> queryBrand(){
 54         //获取XML根节点
 55         Element root = document.getRootElement();
 56         
 57         List<Element> brands=root.elements();
 58         List<Brand> outBrands=new ArrayList<Brand>();
 59         for (Element eleBrand : brands) {
 60             Brand b=new Brand();
 61             b.setName(eleBrand.attribute("name").getValue());
 62             b.setId(Integer.parseInt(eleBrand.attribute("id").getValue()));
 63             
 64             List<String> types=new ArrayList<String>();
 65             List<Element> eleTypes=eleBrand.elements();
 66             for(Element elType:eleTypes) {
 67                 types.add(elType.attribute("name").getValue());
 68             }
 69             b.setTypes(types);
 70             
 71             outBrands.add(b);
 72         }
 73         
 74         return outBrands;
 75     }
 76     
 77     
 78     /**
 79      * 把集合内容写入XML里
 80      */
 81     public boolean saveBrand(List<Brand> brandList) {
 82         SAXReader reader=new SAXReader();
 83         document=reader.getDocumentFactory().createDocument();  //创建一个新的xml文档
 84         Element root=document.addElement("PhoneInfo");   //新增根元素<PhoneInfo>
 85         for (Brand brand : brandList) {
 86             Element eleBrand=root.addElement("Brand");
 87             eleBrand.addAttribute("name", brand.getName());
 88             eleBrand.addAttribute("id", brand.getId()+"");
 89             for(String type:brand.getTypes()) {
 90                 Element eleTypes=eleBrand.addElement("Type");
 91                 eleTypes.addAttribute("name", type);
 92             }
 93             
 94         }
 95         
 96         try {
 97             XMLWriter writer=new XMLWriter(new FileOutputStream(new File("newInfo.xml")),OutputFormat.createPrettyPrint());
 98             writer.write(document);
 99             writer.close();
100         } catch (UnsupportedEncodingException | FileNotFoundException e) {
101             e.printStackTrace();
102             return false;
103         } catch (IOException e) {
104             e.printStackTrace();
105             return false;
106         }
107         return true;
108     }
109     
110     
111     // 修改节点
112     public void updatePhone() {
113         Element root = document.getRootElement();
114         Iterator eleBrands = root.elementIterator();
115         int id = 0;
116         while (eleBrands.hasNext()) {
117             Element brand = (Element) eleBrands.next();
118             id++;
119             brand.addAttribute("id", id + "");
120         }
121         saveXML("newInfo.xml");
122     }
123     
124     // 增加新的手机品牌信息
125     public void addNewPhone() {
126         // 获得XML根元素
127         Element root = document.getRootElement();
128         // 创建<Brand name="三星">
129         Element elBrand = root.addElement("Brand");
130         elBrand.addAttribute("name", "三星");
131         // 创建<Type name="Note 9"/>
132         Element elType = elBrand.addElement("Type");
133         elType.addAttribute("name", "Note 9");
134         Element elType1 = elBrand.addElement("Type");
135         elType1.addAttribute("name", "S 10");
136         saveXML("newInfo.xml");
137     }
138 
139     // 保存修改到XML文件
140     public void saveXML(String path) {
141         OutputFormat format = OutputFormat.createPrettyPrint();
142         format.setEncoding("GBK");
143         XMLWriter writer = null;
144         try {
145             writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(path)), format);
146             writer.write(document);
147         } catch (FileNotFoundException e) {
148             e.printStackTrace();
149         } catch (IOException e) {
150             e.printStackTrace();
151         } finally {
152             try {
153                 writer.close();
154             } catch (IOException e) {
155                 e.printStackTrace();
156             }
157         }
158     }
159 
160     // 删除节点
161     public void deletePhone() {
162         // 获取XML根节点
163         Element root = document.getRootElement();
164         // 获取所有<Brand>
165         Iterator eleBrands = root.elementIterator();
166         while (eleBrands.hasNext()) {
167             Element brand = (Element) eleBrands.next();
168             if(brand.attributeValue("name").equals("华为")) {
169                 brand.getParent().remove(brand);
170             }
171         }
172         saveXML("newInfo.xml");
173     }
174 
175     public static void main(String[] args) {
176         Dom4jDemo2 domDemo = new Dom4jDemo2();
177         domDemo.loadDocument();
178 //        domDemo.addNewPhone();
179 //        domDemo.updatePhone();
180 //        domDemo.deletePhone();
181 //        domDemo.showPhoneInfo();
182         
183         List<Brand> brandList=domDemo.queryBrand();
184         for (Brand brand : brandList) {
185             System.out.println(brand);
186         }
187         
188         /* List<String> typeList=new ArrayList<String>();
189          typeList.add("小米Mix 2");
190          typeList.add("小米Mix 3");
191          typeList.add("小米Mix 2S");
192          List<String> typeList2=new ArrayList<String>();
193          typeList2.add("Mate 20 S");
194          typeList2.add("Mate 20 Pro");
195          typeList2.add("Mate 20 ");
196          List<String> typeList3=new ArrayList<String>();
197          typeList3.add("iPhone XS");
198          typeList3.add("iPhone XS Max");
199          typeList3.add("iPhone XR ");
200         Brand b1=new Brand(01,"小米",typeList);
201         Brand b2=new Brand(02,"华为",typeList2);
202         Brand b3=new Brand(03,"苹果",typeList3);
203         List<Brand> brandList=new ArrayList<Brand>();
204         brandList.add(b1);
205         brandList.add(b2);
206         brandList.add(b3);
207         boolean c=domDemo.saveBrand(brandList);
208         System.out.println(c);*/
209     }
210 }
 1 import java.util.List;
 2 
 3 public class Brand {
 4     private int id;
 5     private String name;
 6     private List<String>types;
 7     
 8     public Brand() {
 9     }
10     
11     public Brand(int id, String name, List<String> types) {
12         this.id = id;
13         this.name = name;
14         this.types = types;
15     }
16     
17     public int getId() {
18         return id;
19     }
20     public void setId(int id) {
21         this.id = id;
22     }
23     public String getName() {
24         return name;
25     }
26     public void setName(String name) {
27         this.name = name;
28     }
29     public List<String> getTypes() {
30         return types;
31     }
32     public void setTypes(List<String> types) {
33         this.types = types;
34     }
35     @Override
36     public String toString() {
37         return this.name+this.types;
38     }
39     
40     
41 }

技术分享图片

 

---恢复内容结束---

Dom4jDemo应用-保存手机信息

标签:src   ++   结束   --   doc   als   color   try   writer   

原文地址:https://www.cnblogs.com/baichang/p/10205720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!