
samples.sax.SAXSerializingSample Maven / Gradle / Ivy
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2004, 2023 Oracle and/or its affiliates. All rights reserved.
*
* Oracle licenses this file to You 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
*
* https://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 samples.sax;
import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.xml.sax.helpers.AttributesImpl;
/** Writing a FI document directly with SAXDocumentSerializer.
* This sample demonstrates the use of SAXDocumentSerializer to write out an FI document
* with following content:
* {@code
*
*
* 2003-03-13T13:13:32-08:00
* 15570720
* 691
*
*
* }
*
* You may use tool "fitosaxtoxml" provided in the FastInfoset package to verify the result.
*/
public class SAXSerializingSample {
/** Creates a new instance of FastInfosetSerializer */
public SAXSerializingSample() {
}
public static void main(String[] args) {
if (args.length != 1) {
displayUsageAndExit();
}
try {
File output = new File(args[0]);
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(output));
//create an instance of FastInfoset SAX Serializer and set output stream
SAXDocumentSerializer s = new SAXDocumentSerializer();
s.setOutputStream(fos);
AttributesImpl attributesImpl = new AttributesImpl();
String temp;
//start FastInfoset document
s.startDocument();
//
String namespaceURI = "http://www.sun.com/schema/spidermarkexpress/sm-inv";
String prefix = "ns1";
String localPart = "invoice";
//namespace must be indexed before calling startElement
s.startPrefixMapping(prefix, namespaceURI);
s.startElement(namespaceURI, localPart, "ns1:invoice", attributesImpl);
//
temp = "\n\t";
s.characters(temp.toCharArray(), 0, temp.length());
s.startElement("", "header", "header", attributesImpl);
// 2003-03-13T13:13:32-08:00
temp = "\n\t\t";
s.characters(temp.toCharArray(), 0, temp.length());
s.startElement("", "IssueDateTime", "IssueDateTime", attributesImpl);
temp = "2003-03-13T13:13:32-08:00";
s.characters(temp.toCharArray(), 0, temp.length());
s.endElement("", "IssueDateTime", "IssueDateTime");
// 15570720
temp = "\n\t\t";
s.characters(temp.toCharArray(), 0, temp.length());
attributesImpl.clear();
attributesImpl.addAttribute("", "schemeAgencyName", "schemeAgencyName", "", "ISO");
attributesImpl.addAttribute("", "schemeName", "schemeName", "", "Invoice");
s.startElement("", "Identifier", "Identifier", attributesImpl);
temp = "15570720";
s.characters(temp.toCharArray(), 0, temp.length());
s.endElement("", "Identifier", "Identifier");
// 691
temp = "\n\t\t";
s.characters(temp.toCharArray(), 0, temp.length());
attributesImpl.clear();
attributesImpl.addAttribute("", "schemeName", "schemeName", "", "Generic");
attributesImpl.addAttribute("", "schemeAgencyName", "schemeAgencyName", "", "ISO");
s.startElement("", "POIdentifier", "POIdentifier", attributesImpl);
temp = "691";
s.characters(temp.toCharArray(), 0, temp.length());
s.endElement("", "POIdentifier", "POIdentifier");
//
temp = "\n\t";
s.characters(temp.toCharArray(), 0, temp.length());
s.endElement("", "header", "header");
//
temp = "\n";
s.characters(temp.toCharArray(), 0, temp.length());
s.endElement("", "invoice", "ns1:invoice");
s.endDocument();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void displayUsageAndExit() {
System.err.println("Usage: ant FISAXSerializer or samples.sax.FastInfosetSerializer FI_output_file");
System.exit(1);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy