List of usage examples for javax.xml.stream XMLEventWriter close
public void close() throws XMLStreamException;
From source file:Main.java
public static void main(String[] args) throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter writer = outputFactory.createXMLEventWriter(System.out); XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance(); StartDocument startDocument = xmlEventFactory.createStartDocument("UTF-8", "1.0"); writer.add(startDocument);/*from ww w.jav a2 s .co m*/ StartElement startElement = xmlEventFactory.createStartElement("", "", "My-list"); writer.add(startElement); Attribute attribute = xmlEventFactory.createAttribute("version", "1"); List attributeList = Arrays.asList(attribute); List nsList = Arrays.asList(); StartElement startElement2 = xmlEventFactory.createStartElement("", "", "Item", attributeList.iterator(), nsList.iterator()); writer.add(startElement2); StartElement codeSE = xmlEventFactory.createStartElement("", "", "code"); writer.add(codeSE); Characters codeChars = xmlEventFactory.createCharacters("I001"); writer.add(codeChars); EndElement codeEE = xmlEventFactory.createEndElement("", "", "code"); writer.add(codeEE); StartElement nameSE = xmlEventFactory.createStartElement(" ", " ", "name"); writer.add(nameSE); Characters nameChars = xmlEventFactory.createCharacters("a name"); writer.add(nameChars); EndElement nameEE = xmlEventFactory.createEndElement("", "", "name"); writer.add(nameEE); StartElement contactSE = xmlEventFactory.createStartElement("", "", "contact"); writer.add(contactSE); Characters contactChars = xmlEventFactory.createCharacters("another name"); writer.add(contactChars); EndElement contactEE = xmlEventFactory.createEndElement("", "", "contact"); writer.add(contactEE); EndDocument ed = xmlEventFactory.createEndDocument(); writer.add(ed); writer.flush(); writer.close(); }
From source file:XMLEventWriterDemo.java
public static void main(String[] args) throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter writer = outputFactory.createXMLEventWriter(System.out); XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance(); StartDocument startDocument = xmlEventFactory.createStartDocument("UTF-8", "1.0"); writer.add(startDocument);/* w w w. j ava2s . co m*/ StartElement startElement = xmlEventFactory.createStartElement("", "", "My-list"); writer.add(startElement); Attribute attribute = xmlEventFactory.createAttribute("version", "1"); List attributeList = Arrays.asList(attribute); List nsList = Arrays.asList(); StartElement startElement2 = xmlEventFactory.createStartElement("", "", "Item", attributeList.iterator(), nsList.iterator()); writer.add(startElement2); StartElement codeSE = xmlEventFactory.createStartElement("", "", "code"); writer.add(codeSE); Characters codeChars = xmlEventFactory.createCharacters("I001"); writer.add(codeChars); EndElement codeEE = xmlEventFactory.createEndElement("", "", "code"); writer.add(codeEE); StartElement nameSE = xmlEventFactory.createStartElement(" ", " ", "name"); writer.add(nameSE); Characters nameChars = xmlEventFactory.createCharacters("a name"); writer.add(nameChars); EndElement nameEE = xmlEventFactory.createEndElement("", "", "name"); writer.add(nameEE); StartElement contactSE = xmlEventFactory.createStartElement("", "", "contact"); writer.add(contactSE); Characters contactChars = xmlEventFactory.createCharacters("another name"); writer.add(contactChars); EndElement contactEE = xmlEventFactory.createEndElement("", "", "contact"); writer.add(contactEE); EndDocument ed = xmlEventFactory.createEndDocument(); writer.add(ed); writer.flush(); writer.close(); }
From source file:Main.java
/** * Describe <code>getInternalXML</code> method here. * * @param is an <code>InputStream</code> value * @param tagName a <code>String</code> value * @param namespace a <code>String</code> value * @return a <code>byte[]</code> value * @exception Exception if an error occurs *//*ww w . j av a 2s . c o m*/ public static byte[] getInternalXML(InputStream is, String tagName, String namespace) throws Exception { if (namespace == null) namespace = ""; ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = outputFactory.createXMLEventWriter(bos); // Loop over XML input stream and process events boolean bStartWritingEvents = false; XMLEventReader reader = inputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (checkNameAndNamespace(event, tagName, namespace) == true) { if (event.isStartElement() && !bStartWritingEvents) { writer.add(eventFactory.createStartDocument()); bStartWritingEvents = true; continue; } else if (event.isEndElement()) { writer.add(eventFactory.createEndDocument()); bStartWritingEvents = false; break; } } if (bStartWritingEvents) { writer.add(event); } } writer.close(); reader.close(); return bos.toByteArray(); }
From source file:eionet.webq.converter.JsonXMLBidirectionalConverter.java
/** * Closes quietly reader and writer./*from w ww . j a v a2 s .c o m*/ * @param reader xml event reader * @param writer xml event writer */ private void closeQuietly(XMLEventReader reader, XMLEventWriter writer) { try { reader.close(); } catch (XMLStreamException e) { LOGGER.warn("Unable to close XMLEventReader", e); } try { writer.close(); } catch (XMLStreamException e) { LOGGER.warn("Unable to close XMLEventWriter", e); } }
From source file:fr.gael.dhus.server.http.webapp.search.controller.SearchController.java
void xmlToJson(InputStream xmlInput, OutputStream jsonOutput) throws XMLStreamException { JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).autoPrimitive(false).fieldPrefix("") .contentField("content").build(); XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(xmlInput); XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(jsonOutput); writer.add(reader);// w ww . j a v a 2 s .c om reader.close(); writer.close(); }
From source file:log4JToXml.xmlToProperties.XmlToLog4jConverterImpl.java
private void addDTDDeclaration(String filename) throws XMLStreamException { XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent dtd = eventFactory//from ww w. j av a2 s.co m .createDTD("<!DOCTYPE log4j:configuration SYSTEM \"" + tempDTD.getAbsolutePath() + "\">"); XMLInputFactory inFactory = XMLInputFactory.newInstance(); XMLOutputFactory outFactory = XMLOutputFactory.newInstance(); XMLEventReader reader = inFactory.createXMLEventReader(new StreamSource(filename)); reader = new DTDReplacer(reader, dtd); XMLEventWriter writer = outFactory.createXMLEventWriter(documentStream); writer.add(reader); writer.flush(); writer.close(); }
From source file:com.google.code.activetemplates.impl.TemplateCompilerImpl.java
@Override public void compile(final Template t, final TemplateModel model, XmlResult out) throws TemplateCompileException { final XmlSource s = t.createSource(); XMLEventReader r = null;//www .j a v a2 s . com XMLEventWriter w = null; try { r = inFactory.createXMLEventReader(s.getSource()); w = outFactory.createXMLEventWriter(out.getResult()); final XMLStreamException[] xe = new XMLStreamException[1]; final XMLEventReader fr = r; final XMLEventWriter fw = w; StandardEvaluationContext eContext = new StandardEvaluationContext(model); eContext.addPropertyAccessor(new TemplateModelPropertyAccessor()); CompileContext ctx = new CompileContext(fr, fw, eFactory, eComponentFactory, expressionParser, eContext); doCompile(t.getName(), ctx); if (xe[0] != null) { throw xe[0]; } } catch (XMLStreamException e) { throw new TemplateCompileException(e); } finally { s.close(); if (r != null) try { r.close(); } catch (XMLStreamException e) { } if (w != null) try { w.close(); } catch (XMLStreamException e) { } } }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
public InputStream addAtomInlinecount(final InputStream feed, final int count, final Accept accept) throws Exception { final XMLEventReader reader = getEventReader(feed); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLOutputFactory xof = XMLOutputFactory.newInstance(); final XMLEventWriter writer = xof.createXMLEventWriter(bos); try {/* w w w .j a v a 2 s .co m*/ final XmlElement feedElement = getAtomElement(reader, writer, "feed"); writer.add(feedElement.getStart()); addAtomElement(IOUtils.toInputStream(String.format("<m:count>%d</m:count>", count)), writer); writer.add(feedElement.getContentReader()); writer.add(feedElement.getEnd()); while (reader.hasNext()) { writer.add(reader.nextEvent()); } } finally { writer.flush(); writer.close(); reader.close(); IOUtils.closeQuietly(feed); } return new ByteArrayInputStream(bos.toByteArray()); }
From source file:com.aionengine.gameserver.dataholders.loadingutils.XmlMerger.java
/** * This method processes the source file, replacing all of * the 'import' tags by the data from the relevant files. * * @throws XMLStreamException on event writing error. * @throws IOException if the destination file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason */// ww w . j a v a 2s .c o m private void doUpdate() throws XMLStreamException, IOException { XMLEventReader reader = null; XMLEventWriter writer = null; Properties metadata = new Properties(); try { writer = outputFactory.createXMLEventWriter(new BufferedWriter(new FileWriter(destFile, false))); reader = inputFactory.createXMLEventReader(new FileReader(sourceFile)); while (reader.hasNext()) { final XMLEvent xmlEvent = reader.nextEvent(); if (xmlEvent.isStartElement() && isImportQName(xmlEvent.asStartElement().getName())) { processImportElement(xmlEvent.asStartElement(), writer, metadata); continue; } if (xmlEvent.isEndElement() && isImportQName(xmlEvent.asEndElement().getName())) continue; if (xmlEvent instanceof Comment)// skip comments. continue; if (xmlEvent.isCharacters())// skip whitespaces. if (xmlEvent.asCharacters().isWhiteSpace() || xmlEvent.asCharacters().isIgnorableWhiteSpace())// skip whitespaces. continue; writer.add(xmlEvent); if (xmlEvent.isStartDocument()) { writer.add(eventFactory.createComment("\nThis file is machine-generated. DO NOT MODIFY IT!\n")); } } storeFileModifications(metadata, metaDataFile); } finally { if (writer != null) try { writer.close(); } catch (Exception ignored) { } if (reader != null) try { reader.close(); } catch (Exception ignored) { } } }
From source file:com.github.lindenb.jvarkit.tools.blast.BlastFilterJS.java
@Override protected Collection<Throwable> call(String inputName) throws Exception { final CompiledScript compiledScript; Unmarshaller unmarshaller;//from w w w .ja v a 2s .c o m Marshaller marshaller; try { compiledScript = super.compileJavascript(); JAXBContext jc = JAXBContext.newInstance("gov.nih.nlm.ncbi.blast"); unmarshaller = jc.createUnmarshaller(); marshaller = jc.createMarshaller(); XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE); xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); PrintWriter pw = openFileOrStdoutAsPrintWriter(); XMLOutputFactory xof = XMLOutputFactory.newFactory(); xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE); XMLEventWriter w = xof.createXMLEventWriter(pw); StreamSource src = null; if (inputName == null) { LOG.info("Reading stdin"); src = new StreamSource(stdin()); } else { LOG.info("Reading file " + inputName); src = new StreamSource(new File(inputName)); } XMLEventReader r = xmlInputFactory.createXMLEventReader(src); XMLEventFactory eventFactory = XMLEventFactory.newFactory(); SimpleBindings bindings = new SimpleBindings(); while (r.hasNext()) { XMLEvent evt = r.peek(); switch (evt.getEventType()) { case XMLEvent.START_ELEMENT: { StartElement sE = evt.asStartElement(); Hit hit = null; JAXBElement<Hit> jaxbElement = null; if (sE.getName().getLocalPart().equals("Hit")) { jaxbElement = unmarshaller.unmarshal(r, Hit.class); hit = jaxbElement.getValue(); } else { w.add(r.nextEvent()); break; } if (hit != null) { bindings.put("hit", hit); boolean accept = super.evalJavaScriptBoolean(compiledScript, bindings); if (accept) { marshaller.marshal(jaxbElement, w); w.add(eventFactory.createCharacters("\n")); } } break; } case XMLEvent.SPACE: break; default: { w.add(r.nextEvent()); break; } } r.close(); } w.flush(); w.close(); pw.flush(); pw.close(); return RETURN_OK; } catch (Exception err) { return wrapException(err); } finally { } }