I. Java class
- PackageCom. java. test;
-
- ImportOrg. W3C. Dom .*;
- ImportJavax. xml. parsers .*;
- ImportJava. Io .*;
-
- Public ClassJavareadxml {
- // Document can be seen as an image of XML in memory. Once this document is obtained
- // Memory operations to perform XML operations. First, obtain the XML-related document
- PrivateDocument Doc =Null;
-
- Public VoidInit (string xmlfile)ThrowsException {
- // It is obvious that this class is a singleton. Obtain and generate the documentbuilder factory first.
- // The factory where a documentbuilder is generated,
- // Documentbuilder is used to generate document
- Documentbuilderfactory DBF = documentbuilderfactory. newinstance ();
- Documentbuilder DB = DBF. newdocumentbuilder ();
- // This document is an image of the XML file in the memory.
- Doc = dB. parse (NewFile (xmlfile ));
- }
-
- // This method is used to display the content of the XML file.
- Public VoidViewxml (string xmlfile)ThrowsException {
- This. INIT (xmlfile );
- // There is only one root element in the XML file. Take the root element out first.
- Element element = Doc. getdocumentelement ();
- System. Out. println ("root element:" + element. gettagname ());
-
- Nodelist = Doc. getelementsbytagname ("person ");
- System. Out. println ("book node chain length:" + nodelist. getlength ());
-
- Node fathernode = nodelist. Item (0 );
- System. Out. println ("parent node:" + fathernode. getnodename ());
-
- // Take out the attributes of the parent node
- Namednodemap attributes = fathernode. getattributes ();
-
- For(IntI = 0; I <attributes. getlength (); I ++ ){
- Node attribute = attributes. item (I );
- System. Out. println ("book attribute name:" + attribute. getnodename ()
- + "The corresponding property value is:" + attribute. getnodevalue ());
- }
-
- Nodelist childnodes = fathernode. getchildnodes ();
- System. Out. println (childnodes. getlength ());
- For(IntJ = 0; j <childnodes. getlength (); j ++ ){
- Node childnode = childnodes. Item (j );
- // If the node belongs to the element, set the value.
- If(ChildnodeInstanceofElement ){
- // System. Out. println ("sub-node name:" + childnode. getnodename () + "the corresponding value is" + childnode. getfirstchild (). getnodevalue ());
- System. Out. println ("subnode name:" + childnode. getnodename ()
- + "The corresponding value is" + childnode. getfirstchild (). getnodevalue ());
- }
- }
-
- }
-
- Public Static VoidMain (string [] ARGs)ThrowsException {
- Javareadxml parse =NewJavareadxml ();
-
- // My XML file
- Parse. viewxml ("person. xml ");
- }
- }
Ii. xml file
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Book>
- <Person>
- <First> Wang </first>
- <Last> laohu </last>
- <Age> 25 </age>
- <Version> China Post and Telecommunications Publishing House </version>
- </Person>
- <Person>
- <First> LI </first>
- <Last> junjia </last>
- <Age> 24 </age>
- <Version> Tsinghua University Press </version>
- </Person>
- </Book>
Iii. output results
The root element is book.
The length of the book node chain: 2
Parent node: person
9
Subnode name: the value corresponding to first is Wang
Child node name: the value corresponding to last is laohu
Subnode name: the value corresponding to age is 25.
Sub-node name: the value corresponding to version is China Post and Telecommunications Press
Source: http://developer.51cto.com/art/200906/128418.htm