標籤:cat rate 子節點 增刪改查 .sh xml檔案 main out creat
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();
}
}
使用DOM維護手機收藏資訊