145、编程用JAVA解析XML的方式.
答:用SAX方式解析XML,XML文件如下:
<?xml version="1.0" encoding="gb2312"?>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955年生,博士,95年调入海南大学</notes>
</person>
事件回调类SAXHandler.java
import java.io.*;
import java.util.Hashtable;
import org.xml.sax.*;
public class SAXHandler extends HandlerBase
{
private Hashtable table = new Hashtable();
private String currentElement = null;
private String currentValue = null;
public void setTable(Hashtable table)
{
this.table = table;
}
public Hashtable getTable()
{
return table;
}
public void startElement(String tag, AttributeList attrs)
throws SAXException
{
currentElement = tag;
}
public void characters(char[] ch, int start, int length)
throws SAXException
{
currentValue = new String(ch, start, length);
}
public void endElement(String name) throws SAXException
{
if (currentElement.equals(name))
table.put(currentElement, currentValue);
}
}
JSP内容显示源码,SaxXml.jsp:
<HTML>
<HEAD>
<TITLE>剖析XML文件people.xml</TITLE>
</HEAD>
<BODY>
<%@ page errorPage="ErrPage.jsp"
contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="org.xml.sax.*" %>
<%@ page import="javax.xml.parsers.SAXParserFactory" %>
<%@ page import="javax.xml.parsers.SAXParser" %>
<%@ page import="SAXHandler" %>
<%
File file = new File("c:\\people.xml");
FileReader reader = new FileReader(file);
Parser parser;
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
SAXHandler handler = new SAXHandler();
sp.parse(new InputSource(reader), handler);
Hashtable hashTable = handler.getTable();
out.println("<TABLE BORDER=2><CAPTION>教师信息表</CAPTION>");
out.println("<TR><TD>姓名</TD>" + "<TD>" +
(String)hashTable.get(new String("name")) + "</TD></TR>");
out.println("<TR><TD>学院</TD>" + "<TD>" +
(String)hashTable.get(new String("college"))+"</TD></TR>");
out.println("<TR><TD>电话</TD>" + "<TD>" +
(String)hashTable.get(new String("telephone")) + "</TD></TR>");
out.println("<TR><TD>备注</TD>" + "<TD>" +
(String)hashTable.get(new String("notes")) + "</TD></TR>");
out.println("</TABLE>");
%>
</BODY>
</HTML>
146、EJB的基本架构
答:一个EJB包括三个部分:
Remote Interface 接口的代码
package Beans;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Add extends EJBObject
{
//some method declare
}
Home Interface 接口的代码
package Beans;
import java.rmi.RemoteException;
import jaax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface AddHome extends EJBHome
{
//some method declare
}
EJB类的代码
package Beans;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javx.ejb.SessionContext;
public class AddBean Implements SessionBean
{
//some method declare
}
147、如何校验数字型?
var re=/^\d{1,8}$|\.\d{1,2}$/;
var str=document.form1.all(i).value;
var r=str.match(re);
if (r==null)
{
sign=-4;
break;
}
else{
document.form1.all(i).value=parseFloat(str);
}
148、将一个键盘输入的数字转化成中文输出
(例如:输入:1234567 输出:一百二拾三万四千五百六拾七)
用java语言实现,,请编一段程序实现!
public class Reader {
private String strNum;
private String strNumChFormat;
private String strNumTemp;
private int intNumLen;
private String strBegin;
public Reader(String strNum) {
this.strNum = strNum;
}
public boolean check(String strNum) {
boolean valid = false;
if (strNum.substring(0,1).equals("0")){
this.strNum = strNum.substring(1);
}
try {
new Double(strNum);
valid = true;
}
catch (NumberFormatException ex) {
System.out.println("Bad number format!");
}
return valid;
}
public void init() {
strNumChFormat = "";
intNumLen = strNum.length();
strNumTemp = strNum;
strNumTemp = strNumTemp.replace('1', '一');
strNumTemp = strNumTemp.replace('2', '二');
strNumTemp = strNumTemp.replace('3', '三');
strNumTemp = strNumTemp.replace('4', '四');
strNumTemp = strNumTemp.replace('5', '五');
strNumTemp = strNumTemp.replace('6', '六');
strNumTemp = strNumTemp.replace('7', '七');
strNumTemp = strNumTemp.replace('8', '八');
strNumTemp = strNumTemp.replace('9', '九');
strNumTemp = strNumTemp.replace('0', '零');
strNumTemp = strNumTemp.replace('.', '点');
strBegin = strNumTemp.substring(0, 1);
}
public String readNum() {
if (check(strNum)) {
init();
try {
for (int i = 1, j = 1, k = 1; i < intNumLen; i++) {
if (strNumTemp.charAt(intNumLen - 1) == '零' && i == 1) {
strNumChFormat = "位";
}
else if (strNumTemp.charAt(intNumLen - i) == '零' && j == 1) {
strNumChFormat = "位" + strNumChFormat;
}
else if (strNumTemp.charAt(intNumLen - i) == '点') {
j = 1;
k = 1;
strNumChFormat = strNumTemp.charAt(intNumLen - i) + strNumChFormat;
continue;
}
else {
strNumChFormat = strNumTemp.charAt(intNumLen - i) + strNumChFormat;
}
if (strNumTemp.charAt(intNumLen - i - 1) != '位' &&
strNumTemp.charAt(intNumLen - i - 1) != '零') {
if (j == 1 && i < intNumLen) {
strNumChFormat = '拾' + strNumChFormat;
}
else if (j == 2 && i < intNumLen) {
strNumChFormat = '百' + strNumChFormat;
}
else if (j == 3 && i < intNumLen) {
strNumChFormat = '千' + strNumChFormat;
}
}
if (j == 4 && i < intNumLen) {
j = 0;
}
if (k == 4 && i < intNumLen) {
strNumChFormat = '万' + strNumChFormat;
}
else if (k == 8 && i < intNumLen) {
k = 0;
strNumChFormat = '亿' + strNumChFormat;
}
j++;
k++;
}
while (strNumChFormat.indexOf("位") != -1) {
strNumChFormat = strNumChFormat.replaceAll("位", " ");
}
if (strNumChFormat.substring(0, 2) == "一拾") {
strNumChFormat = strNumChFormat.substring(1, strNumChFormat.length());
}
if (strNumChFormat.indexOf("点") >= 0) {
String rebegin = strNumChFormat.substring(0,
strNumChFormat.indexOf("点"));
String relast = strNumChFormat.substring(strNumChFormat.indexOf("点"),
strNumChFormat.length());
for (int i = 1; i <= relast.length(); i++) {
relast = relast.replaceAll("拾", "");
relast = relast.replaceAll("百", "");
relast = relast.replaceAll("千", "");
relast = relast.replaceAll("万", "");
relast = relast.replaceAll("亿", "");
}
strNumChFormat = rebegin + relast;
}
}
catch (ArrayIndexOutOfBoundsException ex) {
ex.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
}
int off = strNumChFormat.indexOf("点");
strNumChFormat = strBegin + strNumChFormat.substring(0);
}
else {
strNumChFormat = "";
}
return strNumChFormat;
}
public static void main(String args[]) {
try {
String number = args[0].toString();
System.out.println("The number is: " + number);
Reader reader = new Reader(number);
System.out.println("Output String: " + reader.readNum());
}
catch (Exception ex) {
System.out.println("Please input like that: javac Reader <number>");
}
}
}
149、JAVA代码查错
1.
abstract class Name {
private String name;
public abstract boolean isStupidName(String name) {}
}
大侠们,这有何错误?
答案: 错。abstract method必须以分号结尾,且不带花括号。
2.
public class Something {
void doSomething () {
private String s = "";
int l = s.length();
}
}
有错吗?
答案: 错。局部变量前不能放置任何访问修饰符 (private,public,和protected)。final可以用来修饰局部变量
(final如同abstract和strictfp,都是非访问修饰符,strictfp只能修饰class和method而非variable)。
3.
abstract class Something {
private abstract String doSomething ();
}
这好像没什么错吧?
答案: 错。abstract的methods不能以private修饰。abstract的methods就是让子类implement(实现)具体细节的,怎么可以用private把abstract
method封锁起来呢? (同理,abstract method前不能加final)。
4.
public class Something {
public int addOne(final int x) {
return ++x;
}
}
这个比较明显。
答案: 错。int x被修饰成final,意味着x不能在addOne method中被修改。
5.
public class Something {
public static void main(String[] args) {
Other o = new Other();
new Something().addOne(o);
}
public void addOne(final Other o) {
o.i++;
}
}
class Other {
public int i;
}
和上面的很相似,都是关于final的问题,这有错吗?
答案: 正确。在addOne method中,参数o被修饰成final。如果在addOne method里我们修改了o的reference
(比如: o = new Other();),那么如同上例这题也是错的。但这里修改的是o的member vairable
(成员变量),而o的reference并没有改变。
6.
class Something {
int i;
public void doSomething() {
System.out.println("i = " + i);
}
}
有什么错呢? 看不出来啊。
答案: 正确。输出的是"i = 0"。int i属於instant variable (实例变量,或叫成员变量)。instant variable有default value。int的default value是0。
7.
class Something {
final int i;
public void doSomething() {
System.out.println("i = " + i);
}
}
和上面一题只有一个地方不同,就是多了一个final。这难道就错了吗?
答案: 错。final int i是个final的instant variable (实例变量,或叫成员变量)。final的instant variable没有default value,必须在constructor (构造器)结束之前被赋予一个明确的值。可以修改为"final int i = 0;"。
8.
public class Something {
public static void main(String[] args) {
Something s = new Something();
System.out.println("s.doSomething() returns " + doSomething());
}
public String doSomething() {
return "Do something ...";
}
}
看上去很完美。
答案: 错。看上去在main里call doSomething没有什么问题,毕竟两个methods都在同一个class里。但仔细看,main是static的。static method不能直接call non-static methods。可改成"System.out.println("s.doSomething() returns " + s.doSomething());"。同理,static method不能访问non-static instant variable。
9.
此处,Something类的文件名叫OtherThing.java
class Something {
private static void main(String[] something_to_do) {
System.out.println("Do something ...");
}
}
这个好像很明显。
答案: 正确。从来没有人说过Java的Class名字必须和其文件名相同。但public class的名字必须和文件名相同。
10.
interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}
}
答案:错误。在编译时会发生错误(错误描述不同的JVM有不同的信息,意思就是未明确的x调用,两个x都匹配(就象在同时import java.util和java.sql两个包时直接声明Date一样)。