public class XMLParse3 {
// 1.创建解析器工厂 DocumentBuilerFactory
// 2.创建解析器 (由解析工厂创建) 即为DocumentBuilder对象
// 3.由解析器对象指定XML文件进行解析, 构造DOM树,创建Document对象
// 4. 以Document对象对DOM树节点的操作, 比如 增删改查
Document doc = null;
public void getDocument() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse("收藏信息.xml"); // ---> 把xml解析为一个树状的结构
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void showInfo() {
NodeList brandlist = doc.getElementsByTagName("Brand");
for (int i = 0; i < brandlist.getLength(); i++) {
Node brand = brandlist.item(i);
Element element = (Element) brand;
String brandStr = element.getAttribute("name");
// System.out.println(brandStr);
NodeList typelist = element.getChildNodes();
for (int j = 0; j < typelist.getLength(); j++) {
Node nodetype = typelist.item(j);
if (nodetype.getNodeType() == Node.ELEMENT_NODE) {
Element elementtype = (Element) nodetype;
String typestr = elementtype.getAttribute("name");
System.out.println(brandStr + " : " + typestr);
}
}
}
}
public void saveInfo() {
// 1.创建转换工厂
try {
// 2.创建转换器
Transformer former = TransformerFactory.newInstance()
.newTransformer();
DOMSource xmlSource = new DOMSource(doc);
OutputStreamWriter os = null;
try {
os = new OutputStreamWriter(new FileOutputStream("新的收藏信息.xml"),
"GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StreamResult outputTarget = new StreamResult(os);
// 3.设置编码类型
former.setOutputProperty(OutputKeys.ENCODING, "GBK");
// 4.把DOM树转换为XML文件
former.transform(xmlSource, outputTarget);
System.out.println("save successfully");
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addInfo() {
// 1.创建新节点,并设置name属性
Element newEle = doc.createElement("Brand");
newEle.setAttribute("name", "三星");
// 创建Type节点
Element newType = doc.createElement("Type");
newType.setAttribute("name", "Note7");
// 将子节点newtype放到 父节点newEle
newEle.appendChild(newType);
Element phoneElement = (Element) doc.getElementsByTagName("PhoneInfo")
.item(0);
// 2.把节点加到其父节点上
phoneElement.appendChild(newEle);
}
public void modification(){
NodeList list=doc.getElementsByTagName("Brand");
for(int i=0;i<list.getLength();i++){
Element branElement=(Element)list.item(i);
String brandName=branElement.getAttribute("name");
if(brandName.equals("小米")){
branElement.setAttribute("name", "xiaomi");
}
}
}
public void dele(){
NodeList list=doc.getElementsByTagName("Brand");
for(int i=0;i<list.getLength();i++){
Element branElement=(Element)list.item(i);
String brandName=branElement.getAttribute("name");
if(brandName.equals("三星")){
branElement.getParentNode().removeChild(branElement);
}
}
}
public static void main(String[] args) {
XMLParse3 x3 = new XMLParse3();
x3.getDocument();
x3.addInfo();
x3.showInfo();
x3.saveInfo();
x3.modification();
x3.showInfo();
x3.dele();
x3.showInfo();
}
}