1 38 39 package samples.sax; 40 41 import java.io.File ; 42 import java.io.InputStream ; 43 import java.io.FileInputStream ; 44 import java.io.FileOutputStream ; 45 import java.io.BufferedInputStream ; 46 import java.io.BufferedOutputStream ; 47 import java.io.FileNotFoundException ; 48 49 import javax.xml.stream.XMLStreamWriter; 50 import javax.xml.stream.XMLOutputFactory; 51 import javax.xml.stream.XMLStreamException; 52 53 import com.sun.xml.fastinfoset.QualifiedName; 54 import com.sun.xml.fastinfoset.sax.AttributesHolder; 55 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer; 56 57 70 71 public class FastInfosetSerializer { 72 73 74 public FastInfosetSerializer() { 75 } 76 77 public static void main(String [] args) { 78 if (args.length != 1) { 79 displayUsageAndExit(); 80 } 81 82 try { 83 File output = new File (args[0]); 84 BufferedOutputStream fos = new BufferedOutputStream (new FileOutputStream (output)); 85 86 SAXDocumentSerializer s = new SAXDocumentSerializer(); 88 s.setOutputStream(fos); 89 90 AttributesHolder attributes = new AttributesHolder(); 91 String temp = null; 92 93 s.startDocument(); 95 96 String namespaceURI = "http://www.sun.com/schema/spidermarkexpress/sm-inv"; 98 String prefix = "ns1"; 99 String localPart = "invoice"; 100 101 s.startPrefixMapping(prefix, namespaceURI); 103 s.startElement(namespaceURI, localPart, "ns1:invoice", attributes); 104 105 temp = "\n\t"; 107 s.characters(temp.toCharArray(), 0, temp.length()); 108 s.startElement("", "header", "header", attributes); 109 110 temp = "\n\t\t"; 112 s.characters(temp.toCharArray(), 0, temp.length()); 113 s.startElement("", "IssueDateTime", "IssueDateTime", attributes); 114 temp = "2003-03-13T13:13:32-08:00"; 115 s.characters(temp.toCharArray(), 0, temp.length()); 116 s.endElement("", "IssueDateTime", "IssueDateTime"); 117 118 temp = "\n\t\t"; 120 s.characters(temp.toCharArray(), 0, temp.length()); 121 attributes.clear(); 122 attributes.addAttribute(new QualifiedName("", "", "schemeAgencyName", "schemeAgencyName"), "ISO"); 123 attributes.addAttribute(new QualifiedName("", "", "schemeName", "schemeName"), "Invoice"); 124 s.startElement("", "Identifier", "Identifier", attributes); 125 temp = "15570720"; 126 s.characters(temp.toCharArray(), 0, temp.length()); 127 s.endElement("", "Identifier", "Identifier"); 128 129 temp = "\n\t\t"; 131 s.characters(temp.toCharArray(), 0, temp.length()); 132 attributes.clear(); 133 attributes.addAttribute(new QualifiedName("", "", "schemeName", "schemeName"), "Generic"); 134 attributes.addAttribute(new QualifiedName("", "", "schemeAgencyName", "schemeAgencyName"), "ISO"); 135 s.startElement("", "POIdentifier", "POIdentifier", attributes); 136 temp = "691"; 137 s.characters(temp.toCharArray(), 0, temp.length()); 138 s.endElement("", "POIdentifier", "POIdentifier"); 139 140 temp = "\n\t"; 142 s.characters(temp.toCharArray(), 0, temp.length()); 143 s.endElement("", "header", "header"); 144 145 temp = "\n"; 147 s.characters(temp.toCharArray(), 0, temp.length()); 148 s.endElement("", "invoice", "ns1:invoice"); 149 150 s.endDocument(); 151 } catch (Exception e) { 152 e.printStackTrace(); 153 } 154 } 155 156 private static void displayUsageAndExit() { 157 System.err.println("Usage: ant FISAXSerializer or samples.sax.FastInfosetSerializer FI_output_file"); 158 System.exit(1); 159 } 160 } 161 | Popular Tags |