js|xml| interaction
Here we write a MyHandler class that implements the Handlerbase interface and overwrites the three main methods of the interface startelement (String tag, attributelist attrs), characters ( char[] ch, int start, int length), endelement (String name) so that the XML file can be manipulated.
In order to save the data so that the external program can call the data in this class, we use a hash table structure to save all the node names after parsing the XML file and the data values of the nodes, Startelement () method is mainly used to read the node names in the XML file, characters () The method is mainly used to read the node data according to the node name, and the EndElement () method is mainly to store the node name and node data in the hash table after an XML node has been processed.
In the external program we only need to call the GetTable () method to return a hash table object to read all node and node data values.
The code is not much, and I added a lot of comments below, it should be very good to understand.
Myhandler.java file
file://files are placed in the package com.jsp21.www
Package com.jsp21.www;
file://import of related Java APIs
Import java.io.*;
Import org.w3c.dom.*;
Import org.xml.sax.*;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import java.util.Hashtable;
Import java.util.Enumeration;
The MyHandler class implements the Handlerbase interface;
public class MyHandler extends Handlerbase {
Private String myelement = null; The tag name in the File://XML file;
Private String myvalue = null; File://XML the corresponding value in the file;
Private Hashtable mytable = new Hashtable (); FILE://is used to save all the data in the XML file;
file://gets a hash table hashtable with XML data stored;
Public Hashtable gettable () {
return mytable;
}
file://overrides the Startelement method in the Handlerbase interface to execute this method when reading the start tag of a row of XML data;
Tag represents the markup in XML, such as the name age in the preceding XML file;
public void startelement (String tag, attributelist attrs)
Throws Saxexception {
MyElement = tag;
}
The file://covers the characters method in the Handlerbase interface, which is mainly used to obtain and the specific data between them;
When the corresponding MyElement mark finds the value, that is, after the Startelement method is executed, the characters method is triggered to get the concrete numerical value.
file://such as myelement= "name" time, myvalue will be equal to "Liu Yufeng";
public void characters (char[] ch, int start, int length)
Throws Saxexception {
myvalue = new String (CH, start, length);
}
file://covers the EndElement method in the Handlerbase interface, which is mainly used for the processing of one row of XML data after reading.
This endelement method is triggered when a row of tags in an XML file is read;
file://if there is a corresponding closing tag, the preceding token myelement and value myvalue
file://into the mytable hash table;
public void EndElement (String name) throws Saxexception {
if (myelement.equals (name)) {
Mytable.put (MyElement, myvalue);
}
}
}
Well, compile this class and place the Myhandler.class file in a path that classpath can find, such as the Web-inf\classes\com\jsp21\www directory of the application in Tomcat.
XML and JSP Interaction Technology (4)
5 JSP program calls XML
Here, we write a relatively simple JSP program to call the previous MyHandler class, and through this class to read the contents of the Personal.xml file, and finally displayed in the JSP page.
First we create a saxparserfactory instance of SAXPF, and then through this instance, we create a SAXParser instance Saxpser (which can be used to parse the XML file content) and, of course, create instances of the MyHandler class, Finally, the XML file is associated with the MyHandler class instance through the Saxpser.parse () method (this will perform several event-handling methods in MyHandler).
(Note: SAXParserFactory is an abstract class that defines a factory API that allows Java applications to be configured or to obtain a SAX parser (SAX parser). SAXParser is also an abstract class that enables you to parse the contents of an XML file. )
By this time, all XML content is stored in the hash table in the MyHandler class instance, and finally the hash table object is obtained by the GetTable () method, and the keys in the hash tables are saved in the collection enumeration. Use a loop to read data from the Harlem table and display it on the JSP page. The purpose of our collection is to make this JSP program as versatile as possible, passing an XML file name to read all the content, and of course you can read a particular node value without using a collection instead of using a method such as "name" ("Hshtable.get").
The following is the JSP file code, please save it as a jspxml.jsp file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page language= "java" import= "com.jsp21.www.*,java.io.*"%>
<%@ page language= "java" import= "org.w3c.dom.*,org.xml.sax.*"%>
<%@ page language= "java" import= "Javax.xml.parsers.SAXParser"%>
<%@ page language= "java" import= "Javax.xml.parsers.SAXParserFactory"%>
<%@ page language= "java" import= "java.util.*"%>
<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
This is a xml+jsp example of reading the contents of an XML file
try {
file://get the file name requested by the client
String newfile=request.getparameter ("file");
file://If there is no file value, print the information and return
if (newfile==null| | Newfile.equals (""))
{out.println ("Please use parameters such as Jspxml.jsp?file=e:\\\\personal.xml");
Return
}
String xmlFile = "File:" + new file (NewFile);
FILE://create an instance of SAXParserFactory SAXPF
SAXParserFactory SAXPF = Saxparserfactory.newinstance ();
FILE://creates an instance of SAXParser through SAXPF Saxpser
SAXParser Saxpser = Saxpf.newsaxparser ();
FILE://create an instance of a MyHandler object
MyHandler handler = new MyHandler ();
associating XML files with handler events, using handler to parse content in XML files