public class XMLParse3 {
1. Create a parser factory documentbuilerfactory
2. Creating the parser (created by the parsing factory) is the Documentbuilder object
3. Parsing by specifying an XML file for the parser object, constructing the DOM tree, creating the Document object
4. The Document object to the DOM tree node operations, such as adding and deleting
Document doc = null;
public void GetDocument () {
Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
try {
Documentbuilder builder = Dbf.newdocumentbuilder ();
doc = Builder.parse ("collection information. xml"); ---> Parse xml into a tree-like structure
} 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. Create a conversion factory
try {
2. Create a converter
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. Set the encoding type
Former.setoutputproperty (outputkeys.encoding, "GBK");
4. Convert the DOM tree to an XML file
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. Create a new node and set the Name property
Element Newele = doc.createelement ("Brand");
Newele.setattribute ("name", "Samsung");
Create a Type node
Element NewType = doc.createelement ("Type");
Newtype.setattribute ("name", "Note7");
Place the child node Newtype on the parent node Newele
Newele.appendchild (NewType);
Element phoneelement = (Element) doc.getelementsbytagname ("Phoneinfo")
. Item (0);
2. Add a node to its parent node
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 ("millet")) {
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 ("Samsung")) {
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 ();
}
}
Use the DOM to maintain phone collection information