List of usage examples for javax.xml.stream XMLEventFactory createAttribute
public abstract Attribute createAttribute(QName name, String value);
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);/*w w w . j ava 2 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);//from w ww .j av a 2 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:WpRDFFunctionLibrary.java
public static void mergeGpmltoSingleFile(String gpmlLocation) throws IOException, XMLStreamException, ParserConfigurationException, SAXException, TransformerException { // Based on: http://stackoverflow.com/questions/10759775/how-to-merge-1000-xml-files-into-one-in-java //for (int i = 1; i < 8 ; i++) { Writer outputWriter = new FileWriter("/tmp/WpGPML.xml"); XMLOutputFactory xmlOutFactory = XMLOutputFactory.newFactory(); XMLEventWriter xmlEventWriter = xmlOutFactory.createXMLEventWriter(outputWriter); XMLEventFactory xmlEventFactory = XMLEventFactory.newFactory(); xmlEventWriter.add(xmlEventFactory.createStartDocument("ISO-8859-1", "1.0")); xmlEventWriter.add(xmlEventFactory.createStartElement("", null, "PathwaySet")); xmlEventWriter.add(xmlEventFactory.createAttribute("creationData", basicCalls.now())); XMLInputFactory xmlInFactory = XMLInputFactory.newFactory(); File dir = new File(gpmlLocation); File[] rootFiles = dir.listFiles(); //the section below is only in case of analysis sets for (File rootFile : rootFiles) { String fileName = FilenameUtils.removeExtension(rootFile.getName()); System.out.println(fileName); String[] identifiers = fileName.split("_"); System.out.println(fileName); String wpIdentifier = identifiers[identifiers.length - 2]; String wpRevision = identifiers[identifiers.length - 1]; //Pattern pattern = Pattern.compile("_(WP[0-9]+)_([0-9]+).gpml"); //Matcher matcher = pattern.matcher(fileName); //System.out.println(matcher.find()); //String wpIdentifier = matcher.group(1); File tempFile = new File(constants.localAllGPMLCacheDir() + wpIdentifier + "_" + wpRevision + ".gpml"); //System.out.println(matcher.group(1)); //String wpRevision = matcher.group(2); //System.out.println(matcher.group(2)); if (!(tempFile.exists())) { System.out.println(tempFile.getName()); Document currentGPML = basicCalls.openXmlFile(rootFile.getPath()); basicCalls.saveDOMasXML(WpRDFFunctionLibrary.addWpProvenance(currentGPML, wpIdentifier, wpRevision), constants.localCurrentGPMLCache() + tempFile.getName()); }//from w ww. java 2 s. c o m } dir = new File("/tmp/GPML"); rootFiles = dir.listFiles(); for (File rootFile : rootFiles) { System.out.println(rootFile); XMLEventReader xmlEventReader = xmlInFactory.createXMLEventReader(new StreamSource(rootFile)); XMLEvent event = xmlEventReader.nextEvent(); // Skip ahead in the input to the opening document element try { while (event.getEventType() != XMLEvent.START_ELEMENT) { event = xmlEventReader.nextEvent(); } do { xmlEventWriter.add(event); event = xmlEventReader.nextEvent(); } while (event.getEventType() != XMLEvent.END_DOCUMENT); xmlEventReader.close(); } catch (Exception e) { System.out.println("Malformed gpml file"); } } xmlEventWriter.add(xmlEventFactory.createEndElement("", null, "PathwaySet")); xmlEventWriter.add(xmlEventFactory.createEndDocument()); xmlEventWriter.close(); outputWriter.close(); }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
/** * {@inheritDoc }/*from w w w . j a v a 2 s . com*/ */ @Override protected InputStream addLinks(final String entitySetName, final String entitykey, final InputStream is, final Set<String> links) throws Exception { // ----------------------------------------- // 0. Build reader and writer // ----------------------------------------- final XMLEventReader reader = getEventReader(is); final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLOutputFactory xof = XMLOutputFactory.newInstance(); final XMLEventWriter writer = xof.createXMLEventWriter(bos); // ----------------------------------------- final XmlElement entry = getAtomElement(reader, writer, "entry"); writer.add(entry.getStart()); // add for links for (String link : links) { final Set<Attribute> attributes = new HashSet<Attribute>(); attributes.add(eventFactory.createAttribute(new QName("title"), link)); attributes.add(eventFactory.createAttribute(new QName("href"), Commons.getLinksURI(version, entitySetName, entitykey, link))); attributes.add(eventFactory.createAttribute(new QName("rel"), Constants.ATOM_LINK_REL + link)); attributes.add(eventFactory.createAttribute(new QName("type"), Commons.linkInfo.get(version).isFeed(entitySetName, link) ? Constants.ATOM_LINK_FEED : Constants.ATOM_LINK_ENTRY)); writer.add(eventFactory.createStartElement(new QName(LINK), attributes.iterator(), null)); writer.add(eventFactory.createEndElement(new QName(LINK), null)); } writer.add(entry.getContentReader()); writer.add(entry.getEnd()); writer.add(reader); IOUtils.closeQuietly(is); writer.flush(); writer.close(); reader.close(); return new ByteArrayInputStream(bos.toByteArray()); }
From source file:com.gtwm.pb.model.manageData.DataManagement.java
/** * Based on http://www.vogella.de/articles/RSSFeed/article.html *///from w w w. j av a 2s . c o m private String generateRSS(AppUserInfo user, BaseReportInfo report, List<DataRowInfo> reportDataRows) throws XMLStreamException, ObjectNotFoundException { // Create a XMLOutputFactory XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); // Create XMLEventWriter StringWriter stringWriter = new StringWriter(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(stringWriter); // Create a EventFactory XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); // Create and write Start Tag StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); // Create open tag eventWriter.add(end); StartElement rssStart = eventFactory.createStartElement("", "", "rss"); eventWriter.add(rssStart); eventWriter.add(eventFactory.createAttribute("version", "2.0")); eventWriter.add(end); eventWriter.add(eventFactory.createStartElement("", "", "channel")); eventWriter.add(end); // Write the different nodes this.createNode(eventWriter, "title", report.getModule().getModuleName() + " - " + report.getReportName()); // TODO: Don't hard code host part of URL String reportLink = "https://appserver.gtportalbase.com/agileBase/AppController.servlet?return=gui/display_application&set_table=" + report.getParentTable().getInternalTableName() + "&set_report=" + report.getInternalReportName(); this.createNode(eventWriter, "link", reportLink); this.createNode(eventWriter, "description", "A live data feed from www.agilebase.co.uk"); DateFormat dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); Date lastDataChangeDate = new Date(getLastCompanyDataChangeTime(user.getCompany())); this.createNode(eventWriter, "pubdate", dateFormatter.format(lastDataChangeDate)); for (DataRowInfo reportDataRow : reportDataRows) { eventWriter.add(eventFactory.createStartElement("", "", "item")); eventWriter.add(end); this.createNode(eventWriter, "title", buildEventTitle(report, reportDataRow, false)); this.createNode(eventWriter, "description", reportDataRow.toString()); String rowLink = reportLink + "&set_row_id=" + reportDataRow.getRowId(); this.createNode(eventWriter, "link", rowLink); this.createNode(eventWriter, "guid", rowLink); eventWriter.add(end); eventWriter.add(eventFactory.createEndElement("", "", "item")); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "channel")); eventWriter.add(end); eventWriter.add(eventFactory.createEndElement("", "", "rss")); eventWriter.add(end); return stringWriter.toString(); }
From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java
/** * {@inheritDoc }/* w ww .j ava2 s. c o m*/ */ @Override protected InputStream addLinks(final String entitySetName, final String entitykey, final InputStream is, final Set<String> links) throws Exception { // ----------------------------------------- // 0. Build reader and writer // ----------------------------------------- final XMLEventReader reader = getEventReader(is); final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); // ----------------------------------------- final Map.Entry<Integer, XmlElement> entry = extractElement(reader, writer, Collections.singletonList("entry"), 0, 1, 1); writer.add(entry.getValue().getStart()); // add for links for (String link : links) { final Set<Attribute> attributes = new HashSet<Attribute>(); attributes.add(eventFactory.createAttribute(new QName("title"), link)); attributes.add(eventFactory.createAttribute(new QName("href"), Commons.getLinksURI(version, entitySetName, entitykey, link))); attributes.add(eventFactory.createAttribute(new QName("rel"), Constants.ATOM_LINK_REL + link)); attributes.add(eventFactory.createAttribute(new QName("type"), Commons.linkInfo.get(version).isFeed(entitySetName, link) ? Constants.ATOM_LINK_FEED : Constants.ATOM_LINK_ENTRY)); writer.add(eventFactory.createStartElement(new QName(LINK), attributes.iterator(), null)); writer.add(eventFactory.createEndElement(new QName(LINK), null)); } writer.add(entry.getValue().getContentReader()); writer.add(entry.getValue().getEnd()); writer.add(reader); IOUtils.closeQuietly(is); writer.flush(); writer.close(); reader.close(); return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.apache.olingo.fit.utils.XMLUtilities.java
/** * {@inheritDoc }//from w w w. j av a 2 s . c o m */ @Override protected InputStream addLinks(final String entitySetName, final String entitykey, final InputStream is, final Set<String> links) throws Exception { // ----------------------------------------- // 0. Build reader and writer // ----------------------------------------- final XMLEventReader reader = getEventReader(is); final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); // ----------------------------------------- final Map.Entry<Integer, XMLElement> entry = extractElement(reader, writer, Collections.singletonList("entry"), 0, 1, 1); writer.add(entry.getValue().getStart()); final Map<String, NavigationProperty> navigationProperties = metadata .getNavigationProperties(entitySetName); // add for links for (String link : links) { final Set<Attribute> attributes = new HashSet<Attribute>(); attributes.add(eventFactory.createAttribute(new QName("title"), link)); attributes.add(eventFactory.createAttribute(new QName("href"), Commons.getLinksURI(entitySetName, entitykey, link))); attributes.add(eventFactory.createAttribute(new QName("rel"), Constants.get(ConstantKey.ATOM_LINK_REL) + link)); attributes.add(eventFactory.createAttribute(new QName("type"), navigationProperties.get(link).isEntitySet() ? Constants.get(ConstantKey.ATOM_LINK_FEED) : Constants.get(ConstantKey.ATOM_LINK_ENTRY))); writer.add(eventFactory.createStartElement(new QName(Constants.get(ConstantKey.LINK)), attributes.iterator(), null)); writer.add(eventFactory.createEndElement(new QName(Constants.get(ConstantKey.LINK)), null)); } writer.add(entry.getValue().getContentReader()); writer.add(entry.getValue().getEnd()); writer.add(reader); IOUtils.closeQuietly(is); writer.flush(); writer.close(); reader.close(); return new ByteArrayInputStream(bos.toByteArray()); }
From source file:org.callimachusproject.behaviours.ZipArchiveSupport.java
public XMLEventReader createAtomFeedFromArchive(final String id, final String entryPattern) throws IOException { final FileObject file = this; final XMLEventFactory ef = XMLEventFactory.newInstance(); final byte[] buf = new byte[1024]; final ZipArchiveInputStream zip = new ZipArchiveInputStream(file.openInputStream()); return new XMLEventReaderBase() { private boolean started; private boolean ended; public void close() throws XMLStreamException { try { zip.close();//from w ww . j a va 2 s.c o m } catch (IOException e) { throw new XMLStreamException(e); } } protected boolean more() throws XMLStreamException { try { ZipArchiveEntry entry; if (!started) { Namespace atom = ef.createNamespace(FEED.getPrefix(), FEED.getNamespaceURI()); add(ef.createStartDocument()); add(ef.createStartElement(FEED, null, Arrays.asList(atom).iterator())); add(ef.createStartElement(TITLE, null, null)); add(ef.createCharacters(file.getName())); add(ef.createEndElement(TITLE, null)); add(ef.createStartElement(ID, null, null)); add(ef.createCharacters(id)); add(ef.createEndElement(ID, null)); Attribute href = ef.createAttribute("href", file.toUri().toASCIIString()); List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", "application/zip")); add(ef.createStartElement(LINK, attrs.iterator(), null)); add(ef.createEndElement(LINK, null)); add(ef.createStartElement(UPDATED, null, null)); add(ef.createCharacters(format(new Date(file.getLastModified())))); add(ef.createEndElement(UPDATED, null)); started = true; return true; } else if (started && !ended && (entry = zip.getNextZipEntry()) != null) { String name = entry.getName(); String link = entryPattern.replace("{entry}", PercentCodec.encode(name)); MimetypesFileTypeMap mimetypes = new javax.activation.MimetypesFileTypeMap(); String type = mimetypes.getContentType(name); if (type == null || type.length() == 0) { type = "application/octet-stream"; } add(ef.createStartElement(ENTRY, null, null)); add(ef.createStartElement(TITLE, null, null)); add(ef.createCharacters(name)); add(ef.createEndElement(TITLE, null)); Attribute href = ef.createAttribute("href", link); List<Attribute> attrs = Arrays.asList(href, ef.createAttribute("type", type)); add(ef.createStartElement(LINK, attrs.iterator(), null)); add(ef.createEndElement(LINK, null)); long size = entry.getSize(); if (size > 0) { zip.skip(size); } else { while (zip.read(buf, 0, buf.length) >= 0) ; } add(ef.createEndElement(ENTRY, null)); return true; } else if (!ended) { add(ef.createEndElement(FEED, null)); add(ef.createEndDocument()); ended = true; return true; } else { return false; } } catch (IOException e) { throw new XMLStreamException(e); } } }; }
From source file:org.emonocot.job.io.StaxEventItemWriter.java
/** * Writes simple XML header containing://from w w w .j a v a 2s . c om * <ul> * <li>xml declaration - defines encoding and XML version</li> * <li>opening tag of the root element and its attributes</li> * </ul> * If this is not sufficient for you, simply override this method. Encoding, * version and root tag name can be retrieved with corresponding getters. * * @param writer * XML event writer * @throws XMLStreamException if there is a problem starting the document */ protected final void startDocument(final XMLEventWriter writer) throws XMLStreamException { XMLEventFactory factory = XMLEventFactory.newInstance(); // write start document writer.add(factory.createStartDocument(getEncoding(), getVersion())); // write root tag writer.add( factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName())); if (StringUtils.hasText(getRootTagNamespace())) { if (StringUtils.hasText(getRootTagNamespacePrefix())) { writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace())); } else { writer.add(factory.createNamespace(getRootTagNamespace())); } } // write root tag attributes if (!CollectionUtils.isEmpty(getRootElementAttributes())) { for (Map.Entry<String, String> entry : getRootElementAttributes().entrySet()) { String key = entry.getKey(); if (key.startsWith("xmlns")) { String prefix = ""; if (key.contains(":")) { prefix = key.substring(key.indexOf(":") + 1); } writer.add(factory.createNamespace(prefix, entry.getValue())); } else { writer.add(factory.createAttribute(key, entry.getValue())); } } } /* * This forces the flush to write the end of the root element and avoids * an off-by-one error on restart. */ writer.add(factory.createIgnorableSpace("")); writer.flush(); }
From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java
/** * Writes simple XML header containing://from w w w. j a v a 2 s. c o m * <ul> * <li>xml declaration - defines encoding and XML version</li> * <li>opening tag of the root element and its attributes</li> * </ul> * If this is not sufficient for you, simply override this method. Encoding, * version and root tag name can be retrieved with corresponding getters. * * @param writer XML event writer * @throws XMLStreamException */ protected void startDocument(XMLEventWriter writer) throws XMLStreamException { XMLEventFactory factory = createXmlEventFactory(); // write start document writer.add(factory.createStartDocument(getEncoding(), getVersion())); // write root tag writer.add( factory.createStartElement(getRootTagNamespacePrefix(), getRootTagNamespace(), getRootTagName())); if (StringUtils.hasText(getRootTagNamespace())) { if (StringUtils.hasText(getRootTagNamespacePrefix())) { writer.add(factory.createNamespace(getRootTagNamespacePrefix(), getRootTagNamespace())); } else { writer.add(factory.createNamespace(getRootTagNamespace())); } } // write root tag attributes if (!CollectionUtils.isEmpty(getRootElementAttributes())) { for (Map.Entry<String, String> entry : getRootElementAttributes().entrySet()) { String key = entry.getKey(); if (key.startsWith("xmlns")) { String prefix = ""; if (key.contains(":")) { prefix = key.substring(key.indexOf(":") + 1); } writer.add(factory.createNamespace(prefix, entry.getValue())); } else { writer.add(factory.createAttribute(key, entry.getValue())); } } } /* * This forces the flush to write the end of the root element and avoids * an off-by-one error on restart. */ writer.add(factory.createIgnorableSpace("")); writer.flush(); }