Package javax.xml.stream

Examples of javax.xml.stream.XMLStreamWriter


      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      Writer writer = new PrintWriter(bout);

      MappedNamespaceConvention con = new MappedNamespaceConvention();
      XMLStreamWriter streamWriter = new ResultAdapter(con, writer);

      Source source = new DOMSource(root);
      //Result output = new StAXResult(streamWriter); JDK 6 only
      Result output = new SAXResult((ContentHandler)streamWriter);

      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      transformer.transform(source, output);

      streamWriter.flush();
      writer.flush();

      return new String(bout.toByteArray());

    }
View Full Code Here


      public void translate(Writer writer) throws TransformerException,
          IOException {
          try {
            JSONParser parser = new JSONParser();
          XMLOutputFactory factory = getOutputFactory();
          final XMLStreamWriter streamWriter = factory.createXMLStreamWriter(writer);
           
          parser.parse(r, new JsonToXmlContentHandler(escapeName(rootName, true), streamWriter));
           
          streamWriter.flush(); //woodstox needs a flush rather than a close
        } catch (XMLStreamException e) {
          throw new TransformerException(e);
        } catch (ParseException e) {
          throw new TransformerException(e);
        } finally {
View Full Code Here

        // marshal root object back out to document in memory
        IMarshallingContext mctx = bfact.createMarshallingContext();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            XMLOutputFactory ofact = XMLOutputFactory.newInstance();
            XMLStreamWriter wrtr = ofact.createXMLStreamWriter(bos, enc);
            mctx.setXmlWriter(new StAXWriter(bfact.getNamespaces(), wrtr));
            mctx.marshalDocument(obj);
        } catch (XMLStreamException e) {
            throw new JiBXException("Error creating writer", e);
        }
View Full Code Here

        // marshal root object back out to document in memory
        IMarshallingContext mctx = bfact.createMarshallingContext();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            XMLOutputFactory ofact = XMLOutputFactory.newInstance();
            XMLStreamWriter wrtr = ofact.createXMLStreamWriter(bos, enc);
            mctx.setXmlWriter(new StAXWriter(bfact.getNamespaces(), wrtr));
            mctx.marshalDocument(obj);
        } catch (XMLStreamException e) {
            throw new JiBXException("Error creating writer", e);
        }
View Full Code Here

        // marshal bean back out to document in memory
        IMarshallingContext mctx = obf.createMarshallingContext();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            XMLOutputFactory ofact = XMLOutputFactory.newInstance();
            XMLStreamWriter wrtr = ofact.createXMLStreamWriter(bos, "UTF-8");
            mctx.setXmlWriter(new StAXWriter(obf.getNamespaces(), wrtr));
            mctx.marshalDocument(obj);
        } catch (XMLStreamException e) {
            throw new JiBXException("Error creating writer", e);
        }
View Full Code Here

    {
        InputSource is = new InputSource(getResourceAsStream("test.xml"));
        SAXSource s = new SAXSource(is);
       
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(bos);
        writer.writeStartDocument();
       
        SourceType st = new SourceType();
        st.writeObject(s, new ElementWriter(writer), new MessageContext());
       
        writer.writeEndDocument();
    }
View Full Code Here

    public void testStreamSource() throws Exception
    {
        StreamSource s = new StreamSource(getResourceAsStream("test.xml"));
       
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(bos);
       
        writer.writeStartDocument();
       
        SourceType st = new SourceType();
        st.writeObject(s, new ElementWriter(writer), new MessageContext());
       
        writer.writeEndDocument();
    }
View Full Code Here

        throws Exception
    {
        // XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        // XMLStreamWriter writer = ofactory.createXMLStreamWriter(bos);
        XMLStreamWriter writer = STAXUtils.createXMLStreamWriter(bos, null, null);
        TypeMappingRegistry tmr = new DefaultTypeMappingRegistry(true);
        TypeMapping tm = tmr.createTypeMapping(true);

        SimpleBean bean = new SimpleBean();
        bean.setBleh("bleh");
        bean.setHowdy("howdy");

        registerSimpleBeanType(tm);

        BeanType bt = (BeanType) tm.getType(SimpleBean.class);

        ElementWriter lwriter = new ElementWriter(writer, "SimpleBean", "urn:Bean");
        bt.writeObject(bean, lwriter, new MessageContext());
        lwriter.close();

        writer.close();

        System.out.println(bos.toString());
        // XMLInputFactory factory = XMLInputFactory.newInstance();
        // XMLStreamReader reader = factory.createXMLStreamReader( new
        // StringReader(bos.toString()) );
View Full Code Here

        throws Exception
    {
        // XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        // XMLStreamWriter writer = ofactory.createXMLStreamWriter(bos);
        XMLStreamWriter writer = STAXUtils.createXMLStreamWriter(bos, null, null);
        TypeMappingRegistry tmr = new DefaultTypeMappingRegistry(true);
        TypeMapping tm = tmr.createTypeMapping(true);

        registerSimpleBeanType(tm);
        registerArrayType(tm);

        SimpleBean bean = new SimpleBean();
        bean.setBleh("bleh");
        bean.setHowdy("howdy");

        SimpleBean[] beanArray = new SimpleBean[] { bean, bean };

        ArrayType at = (ArrayType) tm.getType(SimpleBean[].class);

        at.writeObject(beanArray,
                       new ElementWriter(writer, "SimpleBean", "urn:Bean"),
                       new MessageContext());
        writer.close();

        /*
         * TODO: figure out why this doesn't work. It works when you're actually
         * reading/writing documents. I think it has something to do with the
         * reader.next() in the END_ELEMENT case in LiteralReader.
View Full Code Here

        XQueryModule module = proc.parse(input);

        // prepare a result handler (StAX)
        Writer writer = new StringWriter();
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter streamWriter = factory.createXMLStreamWriter(writer);
        XQEventReceiver handler = new StAXSerializer(streamWriter);

        // #2 execute the compiled expression using ``push'' mode
        //   In push mode, the result is directed to the events.
        proc.execute(module, handler);

        streamWriter.flush(); // flushing is required
        System.out.println(writer.toString());
    }
View Full Code Here

TOP

Related Classes of javax.xml.stream.XMLStreamWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.