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

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter; public class Dom4jDemo2 {
private Document document; // 获得document对象
public void loadDocument() {
SAXReader saxReader = new SAXReader(); //相当于一个解析器
try {
document = saxReader.read(new File("newInfo.xml")); //解析获得document对象
} catch (DocumentException e) {
e.printStackTrace();
}
} // 显示手机的品牌及型号
public void showPhoneInfo() {
// 获取XML根节点
Element root = document.getRootElement();
// 获取所有<Brand>
Iterator eleBrands = root.elementIterator();
while (eleBrands.hasNext()) {
Element brand = (Element) eleBrands.next();
System.out.println(brand.attributeValue("name"));
//获取所有的<Type>
Iterator eleTypes = brand.elementIterator();
while (eleTypes.hasNext()) {
Element type = (Element) eleTypes.next();
System.out.println("\t" + type.attributeValue("name"));
}
}
} /**
* 把xml里手机品牌及型号放入集合里输出
* @return
*/
public List<Brand> queryBrand(){
//获取XML根节点
Element root = document.getRootElement(); List<Element> brands=root.elements();
List<Brand> outBrands=new ArrayList<Brand>();
for (Element eleBrand : brands) {
Brand b=new Brand();
b.setName(eleBrand.attribute("name").getValue());
b.setId(Integer.parseInt(eleBrand.attribute("id").getValue())); List<String> types=new ArrayList<String>();
List<Element> eleTypes=eleBrand.elements();
for(Element elType:eleTypes) {
types.add(elType.attribute("name").getValue());
}
b.setTypes(types); outBrands.add(b);
} return outBrands;
} /**
* 把集合内容写入XML里
*/
public boolean saveBrand(List<Brand> brandList) {
SAXReader reader=new SAXReader();
document=reader.getDocumentFactory().createDocument(); //创建一个新的xml文档
Element root=document.addElement("PhoneInfo"); //新增根元素<PhoneInfo>
for (Brand brand : brandList) {
Element eleBrand=root.addElement("Brand");
eleBrand.addAttribute("name", brand.getName());
eleBrand.addAttribute("id", brand.getId()+"");
for(String type:brand.getTypes()) {
Element eleTypes=eleBrand.addElement("Type");
eleTypes.addAttribute("name", type);
} } try {
XMLWriter writer=new XMLWriter(new FileOutputStream(new File("newInfo.xml")),OutputFormat.createPrettyPrint());
writer.write(document);
writer.close();
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
} // 修改节点
public void updatePhone() {
Element root = document.getRootElement();
Iterator eleBrands = root.elementIterator();
int id = 0;
while (eleBrands.hasNext()) {
Element brand = (Element) eleBrands.next();
id++;
brand.addAttribute("id", id + "");
}
saveXML("newInfo.xml");
} // 增加新的手机品牌信息
public void addNewPhone() {
// 获得XML根元素
Element root = document.getRootElement();
// 创建<Brand name="三星">
Element elBrand = root.addElement("Brand");
elBrand.addAttribute("name", "三星");
// 创建<Type name="Note 9"/>
Element elType = elBrand.addElement("Type");
elType.addAttribute("name", "Note 9");
Element elType1 = elBrand.addElement("Type");
elType1.addAttribute("name", "S 10");
saveXML("newInfo.xml");
} // 保存修改到XML文件
public void saveXML(String path) {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter writer = null;
try {
writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(path)), format);
writer.write(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // 删除节点
public void deletePhone() {
// 获取XML根节点
Element root = document.getRootElement();
// 获取所有<Brand>
Iterator eleBrands = root.elementIterator();
while (eleBrands.hasNext()) {
Element brand = (Element) eleBrands.next();
if(brand.attributeValue("name").equals("华为")) {
brand.getParent().remove(brand);
}
}
saveXML("newInfo.xml");
} public static void main(String[] args) {
Dom4jDemo2 domDemo = new Dom4jDemo2();
domDemo.loadDocument();
// domDemo.addNewPhone();
// domDemo.updatePhone();
// domDemo.deletePhone();
// domDemo.showPhoneInfo(); List<Brand> brandList=domDemo.queryBrand();
for (Brand brand : brandList) {
System.out.println(brand);
} /* List<String> typeList=new ArrayList<String>();
typeList.add("小米Mix 2");
typeList.add("小米Mix 3");
typeList.add("小米Mix 2S");
List<String> typeList2=new ArrayList<String>();
typeList2.add("Mate 20 S");
typeList2.add("Mate 20 Pro");
typeList2.add("Mate 20 ");
List<String> typeList3=new ArrayList<String>();
typeList3.add("iPhone XS");
typeList3.add("iPhone XS Max");
typeList3.add("iPhone XR ");
Brand b1=new Brand(01,"小米",typeList);
Brand b2=new Brand(02,"华为",typeList2);
Brand b3=new Brand(03,"苹果",typeList3);
List<Brand> brandList=new ArrayList<Brand>();
brandList.add(b1);
brandList.add(b2);
brandList.add(b3);
boolean c=domDemo.saveBrand(brandList);
System.out.println(c);*/
}
}
 import java.util.List;

 public class Brand {
private int id;
private String name;
private List<String>types; public Brand() {
} public Brand(int id, String name, List<String> types) {
this.id = id;
this.name = name;
this.types = types;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getTypes() {
return types;
}
public void setTypes(List<String> types) {
this.types = types;
}
@Override
public String toString() {
return this.name+this.types;
} }

Dom4jDemo应用-保存手机信息-LMLPHP

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

05-11 15:39