Example usage for javax.xml.stream XMLOutputFactory newFactory

List of usage examples for javax.xml.stream XMLOutputFactory newFactory

Introduction

In this page you can find the example usage for javax.xml.stream XMLOutputFactory newFactory.

Prototype

public static XMLOutputFactory newFactory() throws FactoryConfigurationError 

Source Link

Document

Create a new instance of the factory.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);

    xsw.writeStartDocument();/*from   w  w w. j a  va2 s  .c o m*/
    xsw.writeStartElement("response");
    xsw.writeStartElement("message");
    xsw.writeCharacters("1 < 2");
    xsw.writeEndDocument();

    xsw.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    OutputStream stream = System.out;

    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(stream);

    xsw.writeStartDocument();/*www . j av  a2s  . co m*/
    xsw.writeStartElement("foo");
    xsw.writeStartElement("bar");

    xsw.writeCharacters("");
    xsw.flush();

    OutputStreamWriter osw = new OutputStreamWriter(stream);
    osw.write("<baz>Hello World<baz>");
    osw.flush();

    xsw.writeEndElement();
    xsw.writeEndElement();
    xsw.writeEndDocument();
    xsw.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
    writer.setDefaultNamespace("http://www.java2s.com");

    JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    writer.writeStartDocument();/*  w w  w  . ja  va  2 s. c o m*/
    writer.writeStartElement("http://www.java2s.com", "Import");
    writer.writeNamespace("", "http://www.java2s.com");
    writer.writeStartElement("WorkSets");

    m.marshal(new WorkSet(), writer);
    m.marshal(new WorkSet(), writer);

    writer.writeEndDocument();
    writer.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newFactory();

    StringWriter sw = new StringWriter();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(sw);
    xsw.writeStartDocument();// w ww .ja  va  2s.com
    xsw.writeStartElement("foo");
    xsw.writeCharacters("<>\"&'");
    xsw.writeEndDocument();

    String xml = sw.toString();
    System.out.println(xml);

    // READ THE XML
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
    xsr.nextTag(); // Advance to "foo" element
    System.out.println(xsr.getElementText());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Document doc = null;/*from  w ww .ja  va 2  s  .  c  o  m*/
    XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(doc), new StAXResult(writer));
}

From source file:Main.java

public Main() throws Exception {
    jaxbContext = JAXBContext.newInstance(Main.class);
    xmlOutputFactory = XMLOutputFactory.newFactory();
}

From source file:org.javelin.sws.ext.bind.internal.model.AnalyzeSimpleTypesTest.java

@Before
public void before() throws Exception {
    this.output = new StringWriter();
    this.eventFactory = XMLEventFactory.newFactory();
    this.writer = XMLOutputFactory.newFactory().createXMLEventWriter(this.output);
    this.writer.add(this.eventFactory.createStartElement(new QName("", "v"), null, null));
}

From source file:de.qucosa.webapi.v1.RelationResource.java

@RequestMapping(value = "/relation/urn/{URN}", method = RequestMethod.GET)
public ResponseEntity<String> describeRelationships(@PathVariable String URN)
        throws XMLStreamException, IOException, FedoraClientException {
    String pid = fedoraRepository.getPIDByIdentifier(URN);
    List<Tuple<String>> constituentPredecessorPids = fedoraRepository.getPredecessorPIDs(pid,
            FedoraRepository.RELATION_CONSTITUENT);
    List<Tuple<String>> derivativePredecessorPIDs = fedoraRepository.getPredecessorPIDs(pid,
            FedoraRepository.RELATION_DERIVATIVE);
    List<Tuple<String>> constituentSuccessorPids = fedoraRepository.getSuccessorPIDs(pid,
            FedoraRepository.RELATION_CONSTITUENT);
    List<Tuple<String>> derivativeSuccessorPids = fedoraRepository.getSuccessorPIDs(pid,
            FedoraRepository.RELATION_DERIVATIVE);

    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);
    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    w.writeStartElement("Opus_Document");
    w.writeStartElement("DocumentId");
    w.writeCharacters(stripPrefix(pid));
    w.writeEndElement();//from   w w  w.  ja v  a2 s  .  co  m
    w.writeStartElement("Relations");
    writeRelationElement(constituentPredecessorPids, w, "PredecessorRelation", "journal");
    writeRelationElement(derivativePredecessorPIDs, w, "PredecessorRelation", "predecessor");
    writeRelationElement(constituentSuccessorPids, w, "SuccessorRelation", "issue");
    writeRelationElement(derivativeSuccessorPids, w, "SuccessorRelation", "predecessor");
    w.writeEndElement();
    w.writeEndElement();
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();

    return new ResponseEntity<>(sw.toString(), HttpStatus.OK);
}

From source file:com.amalto.core.load.io.XMLStreamUnwrapper.java

public XMLStreamUnwrapper(InputStream stream) {
    try {//from   w w w  .  ja  v a 2  s  .  co m
        reader = XMLInputFactory.newFactory().createXMLEventReader(stream);
        // Skip to first record
        while (reader.hasNext() && level < RECORD_LEVEL) {
            final XMLEvent event = reader.nextEvent();
            if (event.isStartElement()) {
                // Declare root element namespaces (if any)
                final StartElement startElement = event.asStartElement();
                Iterator namespaces = startElement.getNamespaces();
                while (namespaces.hasNext()) {
                    rootNamespaceList.add((Namespace) namespaces.next());
                }
                level++;
            }
        }
        xmlOutputFactory = XMLOutputFactory.newFactory();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Unexpected parsing configuration error.", e);
    }
}

From source file:org.elasticsearch.discovery.ec2.AmazonEC2Fixture.java

/**
 * Generates a XML response that describe the EC2 instances
 *///w ww  . jav a  2s  .  c o m
private byte[] generateDescribeInstancesResponse() {
    final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
    xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

    final StringWriter out = new StringWriter();
    XMLStreamWriter sw;
    try {
        sw = xmlOutputFactory.createXMLStreamWriter(out);
        sw.writeStartDocument();

        String namespace = "http://ec2.amazonaws.com/doc/2013-02-01/";
        sw.setDefaultNamespace(namespace);
        sw.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "DescribeInstancesResponse", namespace);
        {
            sw.writeStartElement("requestId");
            sw.writeCharacters(UUID.randomUUID().toString());
            sw.writeEndElement();

            sw.writeStartElement("reservationSet");
            {
                if (Files.exists(nodes)) {
                    for (String address : Files.readAllLines(nodes)) {

                        sw.writeStartElement("item");
                        {
                            sw.writeStartElement("reservationId");
                            sw.writeCharacters(UUID.randomUUID().toString());
                            sw.writeEndElement();

                            sw.writeStartElement("instancesSet");
                            {
                                sw.writeStartElement("item");
                                {
                                    sw.writeStartElement("instanceId");
                                    sw.writeCharacters(UUID.randomUUID().toString());
                                    sw.writeEndElement();

                                    sw.writeStartElement("imageId");
                                    sw.writeCharacters(UUID.randomUUID().toString());
                                    sw.writeEndElement();

                                    sw.writeStartElement("instanceState");
                                    {
                                        sw.writeStartElement("code");
                                        sw.writeCharacters("16");
                                        sw.writeEndElement();

                                        sw.writeStartElement("name");
                                        sw.writeCharacters("running");
                                        sw.writeEndElement();
                                    }
                                    sw.writeEndElement();

                                    sw.writeStartElement("privateDnsName");
                                    sw.writeCharacters(address);
                                    sw.writeEndElement();

                                    sw.writeStartElement("dnsName");
                                    sw.writeCharacters(address);
                                    sw.writeEndElement();

                                    sw.writeStartElement("instanceType");
                                    sw.writeCharacters("m1.medium");
                                    sw.writeEndElement();

                                    sw.writeStartElement("placement");
                                    {
                                        sw.writeStartElement("availabilityZone");
                                        sw.writeCharacters("use-east-1e");
                                        sw.writeEndElement();

                                        sw.writeEmptyElement("groupName");

                                        sw.writeStartElement("tenancy");
                                        sw.writeCharacters("default");
                                        sw.writeEndElement();
                                    }
                                    sw.writeEndElement();

                                    sw.writeStartElement("privateIpAddress");
                                    sw.writeCharacters(address);
                                    sw.writeEndElement();

                                    sw.writeStartElement("ipAddress");
                                    sw.writeCharacters(address);
                                    sw.writeEndElement();
                                }
                                sw.writeEndElement();
                            }
                            sw.writeEndElement();
                        }
                        sw.writeEndElement();
                    }
                }
                sw.writeEndElement();
            }
            sw.writeEndElement();

            sw.writeEndDocument();
            sw.flush();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return out.toString().getBytes(UTF_8);
}