The XML format is as follows: <? XML version = "1.0" encoding = "UTF-8"?>
<! -- Cyplate simulator configuration -->
<Cyplate>
<Backgroundchar> ◇ </backgroundchar>
<Foregroundchar> ◆ </foregroundchar>
<Templatewidth> 170 </templatewidth>
<Templateheigth> 72 </templateheigth>
<Waittimes> 2000 </waittimes>
<Maid> 1000 </intervaltimes>
<Fontname> default </fontname>
<Fontsize> 8 </fontsize>
</Cyplate> Implementation Method: documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Factory. setignoringelementcontentwhitespace (true );
Factory. setnamespaceaware (false );
Factory. setvalidating (false );
Try {
Documentbuilder builder = factory. newdocumentbuilder ();
File xmlfile = new file ("/home/creat/sys/cyplate/config ");
Document Doc = builder. parse (xmlfile );
Element root = Doc. getdocumentelement ();
Backgroundchar = root. getelementsbytagname ("backgroundchar"). Item (0). getfirstchild (). getnodevalue (); // backgroundchar
Foregroundchar = root. getelementsbytagname ("foregroundchar"). Item (0). getfirstchild (). getnodevalue (); // foregroundchar
Templatewidth = integer. valueof (root. getelementsbytagname ("templatewidth"). Item (0). getfirstchild (). getnodevalue (); // templatewidth
Templateheigth = integer. valueof (root. getelementsbytagname ("templateheigth"). Item (0). getfirstchild (). getnodevalue (); // templateheigth
Waittimes = integer. valueof (root. getelementsbytagname ("waittimes"). Item (0). getfirstchild (). getnodevalue (); // waittimes
Intervaltimes = integer. valueof (root. getelementsbytagname ("intervaltimes"). Item (0). getfirstchild (). getnodevalue (); // intervaltimes
Fontname = root. getelementsbytagname ("fontname"). Item (0). getfirstchild (). getnodevalue (); // fontname
Fontsize = integer. valueof (root. getelementsbytagname ("fontsize"). Item (0). getfirstchild (). getnodevalue (); // fontsize
} Catch (exception e ){
// Todo: handle exception
E. printstacktrace ();
} The XML format is as follows: 2: <? XML version = "1.0" encoding = "gb2312" standalone = "no"?>
<Books>
<Book email = "zhoujunhui">
<Name> zhangzwon </Name>
<Price> jjjjjj </price>
</Book>
</Books> documentbuilderfactory domfac = documentbuilderfactory. newinstance ();
Try {
Documentbuilder dombuilder = domfac. newdocumentbuilder ();
Inputstream is = new fileinputstream (new file ("D:/test1.xml "));
Document Doc = dombuilder. parse (is );
Element root = Doc. getdocumentelement ();
Nodelist books = root. getchildnodes ();
If (books! = NULL ){
For (INT I = 0; I <books. getlength (); I ++ ){
Node book = books. item (I );
If (book. getnodetype () = node. element_node ){
// (7) obtain the node Attribute Value
String email = book. getattributes (). getnameditem ("email"). getnodevalue ();
System. Out. println (email );
// Note that the attributes of a node are also its subnodes. Its node type is also node. element_node
// (8) Round Robin subnode
For (node = book. getfirstchild (); node! = NULL; node = node. getnextsibling ()){
If (node. getnodetype () = node. element_node ){
If (node. getnodename (). Equals ("name ")){
String name = node. getnodevalue ();
String name1 = node. getfirstchild (). getnodevalue ();
System. Out. println (name );
System. Out. println (name1 );
}
If (node. getnodename (). Equals ("price ")){
String price = node. getfirstchild (). getnodevalue ();
System. Out. println (price );
}
}
}
}
}
}
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
1) Get the factory instance of the DOM parser
Documentbuilderfactory domfac = documentbuilderfactory. newinstance ();
2) then get the DOM parser from the DOM Factory
Documentbuilder dombuilder = domfac. newdocumentbuilder ();
3) convert the XML document to be parsed into an input stream so that the DOM parser can parse it
Inputstream is = new fileinputstream ("test1.xml ");
(4) parse the input stream of the XML document to obtain a document
Document Doc = dombuilder. parse (is );
(5) Get the root node of the XML document
Element root = Doc. getdocumentelement ();
(6) obtain the subnode of the node
Nodelist books = root. getchildnodes ();