类来解析xml元素

superme 2005-03-02 06:16:01
下了个通过类来解析xml元素的程序,但是有错,提示
org.xml.sax.SAXException: System property org.xml.sax.driver not specified

at org.xml.sax.helpers.XMLReaderFactory.createXMLReader(XMLReaderFactory.java:90)
请各位高手指教
Custormer.java
public class Customer {

// Customer member variables.
public String firstName = "";
public String lastName = "";
public String custId = "";

public void print( PrintStream out ) {
out.println( "Customer: " );
out.println( " First Name -> " + firstName );
out.println( " Last Name -> " + lastName );
out.println( " Customer Id -> " + custId );
}

}

Test.java

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;

public class Test
extends DefaultHandler {
private Customer cust = new Customer();
private CharArrayWriter contents = new CharArrayWriter();
public void startElement(String namespaceURI,
String localName,
String qName,
Attributes attr) throws SAXException {

contents.reset();

}

public void endElement(String namespaceURI,
String localName,
String qName) throws SAXException {

if (localName.equals("FirstName")) {
cust.firstName = contents.toString();
}

if (localName.equals("LastName")) {
cust.lastName = contents.toString();
}

if (localName.equals("CustId")) {
cust.custId = contents.toString();
}

}

public void characters(char[] ch, int start, int length) throws
SAXException {

contents.write(ch, start, length);

}

public Customer getCustomer() {
return cust;
}

public static void main(String[] argv) {

System.out.println("xmlhomework3:");
try {
parser.parse(new InputSource(new FileReader("test.xml")), new Test());
Test t = new Test();
xr.setContentHandler( t );
Customer cust = t.getCustomer();
cust.print(System.out);

}
catch (Exception e) {
e.printStackTrace();
}

}

}
...全文
91 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuai002 2005-08-22
  • 打赏
  • 举报
回复
mysohu 2005-08-22
  • 打赏
  • 举报
回复
用JDOM吧
goodmanh 2005-08-22
  • 打赏
  • 举报
回复
从提示org.xml.sax.driver 上看,应该是sax解析器没指定好,或者没找到!
zgysc 2005-08-22
  • 打赏
  • 举报
回复
用jdom吧,狠方便的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧