Example usage for javax.xml.stream XMLEventFactory createStartDocument

List of usage examples for javax.xml.stream XMLEventFactory createStartDocument

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventFactory createStartDocument.

Prototype

public abstract StartDocument createStartDocument(String encoding, String version);

Source Link

Document

Creates a new instance of a StartDocument event

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEventWriter writer = outputFactory.createXMLEventWriter(System.out);

    writer.add(eventFactory.createStartDocument("UTF-8", "1.0"));
    writer.add(eventFactory.createStartElement(new QName("p"), null, null));
    XMLEvent sampleElement = eventFactory.createStartElement("", null, "s", null, null);
    writer.add(sampleElement);//from   w w w . ja  v a  2  s.  c om
    writer.add(eventFactory.createEndElement("", null, "s"));
    writer.add(sampleElement);
    writer.add(eventFactory.createEndDocument());
    writer.flush();
}

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   www .j av a 2  s . c  om

    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);/* www.j a  v  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 w  w.j  a v  a 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

private InputStream writeFromStartToEndElement(final StartElement element, final XMLEventReader reader,
        final boolean document) throws XMLStreamException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLOutputFactory xof = XMLOutputFactory.newInstance();
    final XMLEventWriter writer = xof.createXMLEventWriter(bos);

    final QName name = element.getName();

    if (document) {
        final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        writer.add(eventFactory.createStartDocument("UTF-8", "1.0"));
        writer.add(element);//  w w w. j a  va2 s.  c om

        if (element.getAttributeByName(new QName(ATOM_DATASERVICE_NS)) == null) {
            writer.add(eventFactory.createNamespace(ATOM_PROPERTY_PREFIX.substring(0, 1), DATASERVICES_NS));
        }
        if (element.getAttributeByName(new QName(ATOM_METADATA_NS)) == null) {
            writer.add(eventFactory.createNamespace(ATOM_METADATA_PREFIX.substring(0, 1), METADATA_NS));
        }
    } else {
        writer.add(element);
    }

    XMLEvent event = element;

    while (reader.hasNext() && !(event.isEndElement() && name.equals(event.asEndElement().getName()))) {
        event = reader.nextEvent();
        writer.add(event);
    }

    writer.flush();
    writer.close();

    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:org.apache.olingo.fit.utils.AbstractXMLUtilities.java

@Override
public InputStream getProperty(final String entitySetName, final String entityId, final List<String> path,
        final String edmType) throws Exception {
    final List<String> pathElements = new ArrayList<String>();

    for (String element : path) {
        pathElements.add(ATOM_PROPERTY_PREFIX + element);
    }//from w w  w  .  jav  a2  s.  c o  m

    final InputStream src = fsManager.readFile(Commons.getEntityBasePath(entitySetName, entityId) + ENTITY,
            Accept.XML);

    final XMLEventReader reader = getEventReader(src);
    final XmlElement property = extractElement(reader, null, pathElements, 0, 3, 4).getValue();

    reader.close();
    IOUtils.closeQuietly(src);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLEventWriter writer = getEventWriter(bos);

    final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    writer.add(eventFactory.createStartDocument("UTF-8", "1.0"));
    writer.add(property.getStart());

    if (property.getStart().getAttributeByName(new QName(ATOM_DATASERVICE_NS)) == null) {
        writer.add(eventFactory.createNamespace(ATOM_PROPERTY_PREFIX.substring(0, 1), DATASERVICES_NS));
    }
    if (property.getStart().getAttributeByName(new QName(ATOM_METADATA_NS)) == null) {
        writer.add(eventFactory.createNamespace(ATOM_METADATA_PREFIX.substring(0, 1), METADATA_NS));
    }

    writer.add(property.getContentReader());
    writer.add(property.getEnd());

    writer.flush();
    writer.close();

    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:org.emonocot.job.io.StaxEventItemWriter.java

/**
 * Writes simple XML header containing:/*  ww w  . j av 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 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:/*w  w  w .j av  a2  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();

}