SAX的使用方法简介(三)
用SAX来写入XML文件,程序的目标是在上一节的XML文件中,在每一个<senator>节点的最下面插入一个节点<elected>2004</elected>,并用new.xml把文件存下来。
请看源程序:
import java.io.*; import org.xml.sax.*; import javax.xml.parsers.*; import org.xml.sax.helpers.DefaultHandler; public class ch17_07 extends DefaultHandler { static String displayText[] = new String[1000]; static int numberLines = 0; static String indentation = ""; public static void main(String args[]) { ch17_07 obj = new ch17_07(); obj.childLoop(args[0]); //和上一个程序一样,我们把XML文件的内容在遍历以后存到数组里面,现在我们把它写到硬盘上 try { FileWriter filewriter = new FileWriter("new.xml"); for(int loopIndex = 0; loopIndex < numberLines; loopIndex++){ filewriter.write(displayText[loopIndex].toCharArray()); filewriter.write('/n'); } filewriter.close(); } catch (Exception e) { e.printStackTrace(System.err); } } public void childLoop(String uri) { DefaultHandler handler = this; SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser saxParser = factory.newSAXParser(); saxParser.parse(new File(uri), handler); } catch (Throwable t) {} } public void startDocument() { displayText[numberLines] = indentation; displayText[numberLines] += ""1.0/" encoding=/""+ "UTF-8" + "/"?>"; numberLines++; } public void processingInstruction(String target, String data) { displayText[numberLines] = indentation; displayText[numberLines] += "; displayText[numberLines] += target; if (data != null && data.length() > 0) { displayText[numberLines] += ' '; displayText[numberLines] += data; } displayText[numberLines] += "?>"; numberLines++; } public void startElement(String uri, String localName, String qualifiedName, Attributes attributes) { displayText[numberLines] = indentation; indentation += " "; displayText[numberLines] += '<'; displayText[numberLines] += qualifiedName; if (attributes != null) { int numberAttributes = attributes.getLength(); for (int loopIndex = 0; loopIndex < numberAttributes; loopIndex++){ displayText[numberLines] += ' '; displayText[numberLines] += attributes.getQName(loopIndex); displayText[numberLines] += "=/""; displayText[numberLines] += attributes.getValue(loopIndex); displayText[numberLines] += '"'; } } displayText[numberLines] += '>'; numberLines++; } public void characters(char characters[], int start, int length) { String characterData = (new String(characters, start, length)).trim(); if(characterData.indexOf("/n") < 0 && characterData.length() > 0) { displayText[numberLines] = indentation; displayText[numberLines] += characterData; numberLines++; } } public void ignorableWhitespace(char characters[], int start, int length) { //characters(characters, start, length); } public void endElement(String uri, String localName, String qualifiedName) { indentation = indentation.substring(0, indentation.length() - 4) ; displayText[numberLines] = indentation; displayText[numberLines] += "; displayText[numberLines] += qualifiedName; displayText[numberLines] += '>'; numberLines++; //遇到了我们要增加节点的位置,增加一个节点 if (qualifiedName.equals("lastName")) { startElement("", "elected", "elected", null); characters("2004".toCharArray(), 0, "2004".length()); endElement("", "elected", "elected"); } } public void warning(SAXParseException exception) { System.err.println("Warning: " + exception.getMessage()); } public void error(SAXParseException exception) { System.err.println("Error: " + exception.getMessage()); } public void fatalError(SAXParseException exception) { System.err.println("Fatal error: " + exception.getMessage()); } }