Document directory
- 1. Get Factory
- 2. Obtain the Builder
- 3. parse to Document Object
- 1. Get Factory
- 2. Get parser
- 3. Start Parsing
Two XML parsing techniques in Java: Dom and sax target files: D:/meno. xml
<Memo> <Note type = "Birthday"> <Name> Wang Ping </Name> <sex> male </sex> <date> August 1, December 23 </date> <address> Baishan City, Jilin Province eighth Middle School </address> <telphone> 0439-31344532 </telphone> <email> wangping@sina.com </Email> <body> today is Wang Ping's birthday </body> </Note> <note type = "Shop"> <Name> Oriental snow </Name> <sex> female </sex> <date> August 23 </date> <address> Zhongguancun eWorld, Haidian District, Beijing </ address> <telphone> 15984561235 </telphone> <email> eworld@vip.com </Email> <body> today is the day when the Oriental snow store opened </body> </Note> </Memo>
Dom parsing steps: 1. Get factory documentbuilderfactory builderfactory = documentbuilderfactory. newinstance ();
2. Obtain Builder = builderfactory. newdocumentbuilder ();
3. parse it into the Document Object document = builder. parse (xmlfile );
Import Java. io. file; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. node; import Org. w3C. dom. nodelist; public class domtest {public static void main (string [] ARGs) {file xmlfile = new file ("D: \ memo. XML "); documentbuilder builder = NULL; documentbuilderfactory builderfactory = documentbuil Derfactory. newinstance (); try {builder = builderfactory. newdocumentbuilder (); document = builder. parse (xmlfile); // parse the file element root = document. getdocumentelement (); // obtain the root element system. out. println ("root element:" + root. getnodename (); nodelist childnodes = root. getchildnodes (); // obtain the subnode under the root element for (INT I = 0; I <childnodes. getlength (); I ++) {// traverse these subnodes node = childnodes. item (I); If ("NOTE ". equals (node. getnoden Ame () {// If the node name is "Node", output the note element attribute typesystem. Out. println ("\ r \ n to find a diary. Category: "+ node. getattributes (). getnameditem ("type "). getnodevalue () + ". "); nodelist nodedetail = node. getchildnodes (); For (Int J = 0; j <nodedetail. getlength (); j ++) {node detail = nodedetail. item (j); If ("name ". equals (detail. getnodename () system. out. println ("name:" + detail. gettextcontent (); else if ("sex ". equals (detail. getnodename () system. out. println ("Gender:" + detail. gettextcontent (); else if ("date ". equals (detail. getnodename () system. out. println ("Date:" + detail. gettextcontent (); else if ("Address :". equals (detail. getnodename () system. out. println ("Residential Address:" + detail. gettextcontent (); else if ("telephone ". equals (detail. getnodename () system. out. println ("phone number:" + detail. gettextcontent (); else if ("email ". equals (detail. getnodename () system. out. println ("E-mail:" + detail. gettextcontent (); else if ("body ". equals (detail. getnodename () system. out. println ("topic:" + detail. gettextcontent () ;}}} catch (exception e) {e. printstacktrace ();}}}
Sax Parsing
Step: 1. Obtain factory saxparserfactory factory = saxparserfactory. newinstance ();
2. Get parser saxparser parser = factory. newsaxparser ();
3. Start parsing parser. parse (xmlfile, new mysaxlistener ());
import java.io.File;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;public class SaxTest {public static void main(String[] args) {File xmlFile = new File("D:\\memo.xml");SAXParserFactory factory = SAXParserFactory.newInstance();try {SAXParser parser = factory.newSAXParser();parser.parse(xmlFile,new MySaxListener());} catch(Exception e) {e.printStackTrace();}}}
Import Org. XML. sax. helpers. defaulthandler; import Java. text. dateformat; import Java. text. simpledateformat; import Org. XML. sax. attributes; import Org. XML. sax. saxexception; public class mysaxlistener extends defaulthandler {private string content; Public void characters (char [] CH, int start, int length) throws saxexception {content = new string (CH, start, length);} public void endelement (string Uri, string localname, string QNAME) throws saxexception {If ("name ". equals (QNAME) {system. out. println ("name:" + content);} else if ("sex ". equals (QNAME) {system. out. println ("Gender:" + content);} else if ("date ". equals (QNAME) {system. out. println ("Date:" + content);} else if ("Address ". equals (QNAME) {system. out. println ("Residential Address:" + content);} else if ("telephone ". equals (QNAME) {system. out. println ("phone number:" + content);} else if ("email ". equals (QNAME) {system. out. println ("Email:" + content);} else if ("body ". equals (QNAME) {system. out. println ("topic:" + content) ;}} public void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception {If ("NOTE ". equals (QNAME) {system. out. println ("The following is a method for parsing sax:"); system. out. println ("\ r \ n find an article. category: "+ attributes. getvalue ("type") + ". ");}}}
Resolution result