是不是需要文件的io操作,请问如何copy文件的一部分to一个新文件?
解决方案 »
- 一个普通测试EJB stateless的问题,很迷惑不解,请高手解决!!感激不尽!!!
- 一个老问题,但一直解决不掉
- 头像识别系统~
- 诡异的事情,奇怪的报错,在线等,急,先100,后可以追加40
- iframe惹得祸
- Spring 设置JBOSS4.0 JNDI 数据源的问题. 实在不解到底是哪错了..
- 无法加载 struts。HTTP Status 404 错误, 无语的节奏吖。
- 急!weblogic的服务器,突然不读hosts文件了
- 可不可以给一点点WMI(Windows Management Instrumentation)的建议。
- jbuilder连MS-SQL出现问题
- 请大家推荐一些XML方面的书!
- 如何配置环境变量——j2ee一问
我是新手,可不可以给我个相关的例子?谢谢
用characters可以实现么?
你这个想法我也有(类似),只是不知道是否SAX自带有更好的解决方案,如果没有什么现成的方法,只好用串的方式再通过I/O了。因此我希望听听大家的好想法。谢谢
// Copyright (c) 2000
// Oracle Japan
//////////////////////////////////////////////////////////////////////
package jvm.ch15;import java.io.*;
import java.util.*;
import oracle.xml.parser.v2.*;
import org.xml.sax.*;public class BookBySAX extends HandlerBase { Locator locator;
Stack elements;
int bookid = -1;
String title;
String author;
int categid; static public void main(String[] argv) {
try {
InputStream theStreamToParse = null; // 「Book.xml」を読み込む
theStreamToParse = new FileInputStream("Book.xml");
// パーサー用に新しくハンドラーを生成
BookBySAX sample = new BookBySAX(); // SAXパーサーをインスタンス化
SAXParser parser = new SAXParser(); // パーサーのオプションのセット:
// エラーメッセージを日本語、妥当性の検証なし
parser.setLocale(Locale.JAPAN);
parser.setValidationMode(false); // 使用するハンドラーをパーサーに登録
parser.setDocumentHandler(sample);
parser.setErrorHandler(sample); // XML文書を解析
parser.parse(theStreamToParse); // 各インスタンス変数を出力
System.out.println("22222"); // idを出力
System.out.println(sample.title);
System.out.println(sample.author);
System.out.println(sample.categid);
}
catch (Exception e) {
e.printStackTrace();
}
} //////////////////////////////////////////////////////////////////////
// DocumentHandlerインターフェースの実装
////////////////////////////////////////////////////////////////////// public void setDocumentLocator (Locator locator) {
this.locator = locator;
} public void startDocument() {
// スタックを生成
elements = new Stack();
} public void endDocument() {
} public void startElement(String name, AttributeList atts)
throws SAXException {
// 要素名をスタックに追加
elements.push(name);
if (name.equals("アイテム")) {
for (int i=0;i<atts.getLength();i++) {
String aname = atts.getName(i);
if (aname.equals("id")) {
// 「id」の値を保存
bookid = Integer.parseInt(atts.getValue(i));
}
}
}
} public void endElement(String name) {
// 要素をスタックから出す
elements.pop();
if (name.equals("アイテム")) {
bookid = -1;
}
} public void characters(char[] cbuf, int start, int len) {
// 指定の値の「id」のときだけの処理
if (bookid == 22222) {
// 要素ごとに要素の内容をインスタンス変数に格納
String text = new String(cbuf,start,len);
if (elements.peek().equals("タイトル")) {
title = text;
}
else if (elements.peek().equals("筆者")) {
author = text;
}
else if (elements.peek().equals("カテゴリ")) {
categid = Integer.parseInt(text);
}
}
} //////////////////////////////////////////////////////////////////////
// ErrorHandlerインターフェースの実装
////////////////////////////////////////////////////////////////////// public void warning (SAXParseException e) {
System.out.println("Warning:"+e.getMessage()
+ "line:" + locator.getLineNumber()
+ ", column:" + locator.getColumnNumber());
} public void error (SAXParseException e)
throws SAXException {
System.out.println("Error:"+e.getMessage()
+ "line:" + locator.getLineNumber()
+ ", column:" + locator.getColumnNumber());
throw e;
}
public void fatalError (SAXParseException e)
throws SAXException {
System.out.println("Fatal error:"+e.getMessage()
+ "line:" + locator.getLineNumber()
+ ", column:" + locator.getColumnNumber());
throw e;
}
}