是不是需要文件的io操作,请问如何copy文件的一部分to一个新文件?

解决方案 »

  1.   

    这样的功能,不建议你用SAX,用XSLT要简单得多。
      

  2.   

    to wangwenyou(王文友):
    我是新手,可不可以给我个相关的例子?谢谢
      

  3.   

    请问如何在sax中定位元素,即定位<..>的字符位置
    用characters可以实现么?
      

  4.   

    我以前做过类似的东西,如用sax的话建议你先将其解析输出字符串的形式,在字符串中截取,subString()应该不陌生吧,然后将最终字符串在写入xml文件中,这部分可以看看有关流的操作,也很简单。当然,这只是我的想法。
      

  5.   

    to  scropatriot(爱国者) :
       你这个想法我也有(类似),只是不知道是否SAX自带有更好的解决方案,如果没有什么现成的方法,只好用串的方式再通过I/O了。因此我希望听听大家的好想法。谢谢
      

  6.   

    给你个日文的例子,正适合你,稍改一下就行。//////////////////////////////////////////////////////////////////////
    // 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;
      }
    }