DefaultContentHandler builder = new DefaultContentHandler(); Instantiate the FI SAX parser XMLReader saxReader = new SAXDocumentParser(); saxReader.setContentHandler(builder); Parse the fast infoset document InputSource inputSource = new InputSource(stream); saxReader.parse(inputSource); 保存 FastInfoset 文檔
清單 2. 保存 FastInfoset文檔
final java.io.StringReader reader = new java.io.StringReader(saveString); Get the input stream for the XML document InputStream xmlDocument = new InputStream(){ @Override public int read() thro ws IOException { // TODO Auto-generated method stub return reader.read(); } }; Set up output stream for fast infoset document OutputStream fiDocument = new FileOutputStream(new File(filePath)); Create Fast Infoset SAX serializer SAXDocumentSerializer saxDocumentSerializer = new SAXDocumentSerializer(); Set the output stream saxDocumentSerializer.setOutputStream(fiDocument); Instantiate JAXP SAX parser factory SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); /* Set parser to be namespace aware * Very important to do otherwise invalid FI documents will be * created by the SAXDocu mentSerializer */ saxParserFactory.setNamespaceAware(true); Instantiate the JAXP SAX parser SAXParser saxParser = saxParserFactory.newSAXParser(); Set the lexical handler saxParser.setProperty("HTTP://xml.org/sax/properties/lexical-handler", new FastInfosetDefaultHandler()); Parse the XML document and convert to a fast infoset document saxParser.parse(xmlDocument, saxDocumentSerializer);