After 2 days of debugging finally wrote the first BEAN to get XML information with SAX. It has been commissioned to be commissioned before I find a lot of online information. Both fast explosion. Finally gave up directly finding the code to learn, re-editing the example of the Java and XML above, and finally has a little bit of harvest, a simple use of SAX code. (It is much more than DOM.) Is the first step in achieving XML information in Java. Preparation: Go to http://xml.apache.org to download the Xerces parser
Step 1: Write a Java program shairdtest_1.java
/ *
* Created on 2004-11-11
*
* Todo to change the Template for this generated file go to
* WINDOW - Preferences - Java - Code Style - Code Templates
* /
Import java.io.file;
Import java.io.fileinputstream;
Import java.io.ioException;
Import org.xml.sax. *;
Import org.xml.sax.helpers.xmlreaderfactory;
Import shaird.xmlhandler;
/ ** * @Author Shaird * * Todo to change TEMPLATE for this generated type comment go to * window - preferences - java - code style - code templates * / public class shairdtest_1 {
// Get file public void initXML (String XMLURI) throws IOException, SAXException {// dynamically loaded by specifying the name parser parser XMLReader reader = XMLReaderFactory.createXMLReader ( "org.apache.xerces.parsers.SAXParser");
// To process the content Manager Reader.SetContentHandler (New XmlHandler ());
// Analyze InputSource InputSource = New INPUTSOURC (New FileInputStream (New File (XMLuri)); InputSource.setysystemId (XMLuri); Reader.Pars (InputSource);
} Public static void main (string [] args) {try {if (args.length! = 1) {system.out.println ("NEED A XML's URL"); system.exit (0);}
Shairdtest_1 TestXML = New ShairdTest_1 (); TestXml.initXML (Args [0]);} catch (exception e) {E.PrintStackTrace ();}}
}
Step 2: Implement the ContentHandler interface XMLHandler.java
/ * * Created on 2004-11-11 * * Todo to change the Template for this generated file go to * window - preferences - java - code style - code templates * / import java.util.map; import Org.xml.sax .Locator; import org.xml.sax.attributes; import org.xml.sax.contenthandler; import org.xml.sax.saxexception;
/ ** * @author Shaird * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class ShairdTest2 implements ContentHandler {private static String Empid = null; private static String Empname = null; private static string Empdept = null; private static string empage = null;
Private string nowData = ""; / ** Location Information Defineer * / Private Locator Locator; / ** Save the namespace URI to the prefix mapping * / private map namespacemappings; // set and acquire information public void setEmpid (String Emp_ID) {empid = emp_id;} public String getEmpid () {return empid;} public void setEmpName (String emp_name) {EmpName = emp_name;} public String getEmpName () {return EmpName;} public void setEmpDept (String emp_dept) {EmpDept = emp_dept Public string getEmpdept () {return Empdept;} public void set_age;} public string getempage ()} public string getempage ()} public string getri}
Public void setDocumentlocator (locator locator) {}
Public void startdocument () throws saxexception {}
Public void enddocument () throws saxexception {}
Public void startprefixmapping (string prefix, string URI) throws saxexception {}
public void endPrefixMapping (String prefix) throws SAXException {} public void startElement (String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {for (int i = 0; i public void endElement (String namespaceURI, String localName, String qName) throws SAXException {if (localName.equals ( "EMPNAME")) {setEmpName (NowData); System.out.println ( "EMPNAME:" getEmpName ());} IF ("EmpDept")) {setEmpdept (nowData); System.out.Println ("EmpDept:" geteMpdept ());});});})) {selection System.out.println ("Empage:" getempage ()); } Public void characters (char [] ch, int start, int length) throws saxexception {nowData = new string (ch, start, length) .trim (); Public void ignorablewhitespace (char [] ch, int start, int tent) throws saxception { } Public void processinginstruction (String Target, string data) throws saxexception { } Public void Skippedentity (String Name) throws saxexception { } All right. Try with the console. Java Shairdtest_1 TRY.XML