Example usage for javax.xml.stream XMLEventFactory createStartElement

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

Introduction

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

Prototype

public abstract StartElement createStartElement(String prefix, String namespaceUri, String localName);

Source Link

Document

Create a new StartElement.

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);//  w w w .  j  a v a  2s  .  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);/*w  ww .j  a  v a  2s.  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);//w w w .  ja va 2 s . c  o  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 .  j a va2  s .c  om
    }

    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:de.tudarmstadt.ukp.dkpro.core.io.tiger.TigerXmlWriter.java

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    OutputStream docOS = null;/*from www  . j  a va  2 s .co m*/
    try {
        docOS = getOutputStream(aJCas, filenameSuffix);

        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter xmlEventWriter = new IndentingXMLEventWriter(
                xmlOutputFactory.createXMLEventWriter(docOS));

        JAXBContext context = JAXBContext.newInstance(TigerSentence.class);
        Marshaller marshaller = context.createMarshaller();
        // We use the marshaller only for individual sentences. That way, we do not have to 
        // build the whole TIGER object graph before seralizing, which should safe us some
        // memory.
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        XMLEventFactory xmlef = XMLEventFactory.newInstance();
        xmlEventWriter.add(xmlef.createStartDocument());
        xmlEventWriter.add(xmlef.createStartElement("", "", "corpus"));
        xmlEventWriter.add(xmlef.createStartElement("", "", "body"));

        int sentenceNumber = 1;
        for (Sentence s : select(aJCas, Sentence.class)) {
            TigerSentence ts = convertSentence(s, sentenceNumber);
            marshaller.marshal(new JAXBElement<TigerSentence>(new QName("s"), TigerSentence.class, ts),
                    xmlEventWriter);
            sentenceNumber++;
        }

        xmlEventWriter.add(xmlef.createEndElement("", "", "body"));
        xmlEventWriter.add(xmlef.createEndElement("", "", "corpus"));
        xmlEventWriter.add(xmlef.createEndDocument());
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    } finally {
        closeQuietly(docOS);
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.article.ArticleMultiController.java

private void createNode(XMLEventWriter eventWriter, String name, String value) {
    try {/*from   w  ww  . j  av  a 2 s  .com*/
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent end = eventFactory.createDTD("\n");
        XMLEvent tab = eventFactory.createDTD("\t");
        // Create Start node
        StartElement sElement = eventFactory.createStartElement("", "", name);
        eventWriter.add(tab);
        eventWriter.add(sElement);
        // Create Content
        Characters characters = eventFactory.createCharacters(value);
        eventWriter.add(characters);
        // Create End node
        EndElement eElement = eventFactory.createEndElement("", "", name);
        eventWriter.add(eElement);
        eventWriter.add(end);
    } catch (Exception e) {
    }
}

From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java

@Override
protected InputStream replaceLink(final InputStream toBeChanged, final String linkName,
        final InputStream replacement) throws Exception {
    final XMLEventReader reader = getEventReader(toBeChanged);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLOutputFactory xof = XMLOutputFactory.newInstance();
    final XMLEventWriter writer = xof.createXMLEventWriter(bos);

    final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent newLine = eventFactory.createSpace("\n");

    try {/*  w  w w. j a v a2 s  . co  m*/
        final XmlElement linkElement = getAtomElement(reader, writer, LINK,
                Collections.<Map.Entry<String, String>>singletonList(
                        new SimpleEntry<String, String>("title", linkName)));
        writer.add(linkElement.getStart());

        // ------------------------------------------
        // write inline ...
        // ------------------------------------------
        writer.add(newLine);
        writer.add(eventFactory.createStartElement("m", null, "inline"));

        addAtomElement(replacement, writer);

        writer.add(eventFactory.createEndElement("m", null, "inline"));
        writer.add(newLine);
        // ------------------------------------------

        writer.add(linkElement.getEnd());

        writer.add(reader);
        writer.flush();
        writer.close();
    } finally {
        reader.close();
        IOUtils.closeQuietly(toBeChanged);
    }

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

From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java

/**
 * {@inheritDoc }// w  w  w  .  j av a 2 s.  co  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 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

private void createNode(XMLEventWriter eventWriter, String name, String value) throws XMLStreamException {
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = eventFactory.createDTD("\n");
    XMLEvent tab = eventFactory.createDTD("\t");
    // Create Start node
    StartElement sElement = eventFactory.createStartElement("", "", name);
    eventWriter.add(tab);/*from   w  ww  .j  av  a2 s . com*/
    eventWriter.add(sElement);
    // Create Content
    Characters characters = eventFactory.createCharacters(value);
    eventWriter.add(characters);
    // Create End node
    EndElement eElement = eventFactory.createEndElement("", "", name);
    eventWriter.add(eElement);
    eventWriter.add(end);
}

From source file:com.gtwm.pb.model.manageData.DataManagement.java

/**
 * Based on http://www.vogella.de/articles/RSSFeed/article.html
 *///from  www .  j ava  2 s .c om
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();
}