📄 documentparser.java
字号:
/** * Copyright 2004 Carlos Silva A. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.csa.lib.xml.dom;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import org.xml.sax.AttributeList;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import uk.co.wilson.xml.MinML;/** * DocumentParser * * dp.parse( new InputStreamReader(new BufferedInputStream(System.in, 1024)); * * No soporta datos mezclados (elementos + texto) y solo procesa UTF-8 * <p> * $Date: 2004/12/05 04:06:07 $ * </p> * * @version $Revision: 1.1 $ * @author Carlos Silva */public class DocumentParser extends MinML { Node mainElement; Node currentElement; public void parse(File f) throws SAXException, IOException { FileInputStream fis = new FileInputStream(f); parse(fis); fis.close(); } public void parse(InputStream fis) throws SAXException, IOException { InputStreamReader isr = new InputStreamReader(fis, "GB2312"); BufferedReader br = new BufferedReader(isr); // String thisLine = ""; // try { // while ((thisLine = br.readLine()) != null) { // while loop begins // here // System.out.println(thisLine); // } // end while // } // end try // catch (IOException e) { // System.err.println("Error: " + e); // } parse(br); br.close(); isr.close(); } public Node getMainElement() { return mainElement; } public void startDocument() { currentElement = mainElement = new Node("DOCUMENT"); } public void startElement(String name, AttributeList attributes) { Node e = new Node(name, attributes); currentElement.appendChild(e); currentElement = e; } public void endElement(String name) { currentElement = currentElement.parent; } public void characters(char ch[], int start, int length) { currentElement.addValue(new String(ch, start, length)); } public void fatalError(SAXParseException e) throws SAXException { throw e; } /** * Test lee y escribe un xml * * @param args * @throws Exception */ public static void main(String args[]) throws Exception { DocumentParser dp = new DocumentParser(); dp.parse(new File("c:\\project.xml")); Node main = dp.getMainElement(); DocumentWriter dw = new DocumentWriter(main); dw.write(new File("c:\\project2.xml")); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -