From: Jeff Higgins on
Stefan Ram wrote:
> Jeff Higgins <oohiggins(a)yahoo.com> writes:
>> Rhino specified that he needs to write an XML file.
>
> Yes. Whether the standard output of a Java process is
> associated with a file or some other entity depends on the
> environment. (Under UN*X, the console might also be an entry
> in the file system.) One also can use redirection:
>
> java Main >file.xml
>
> , but you are right insofar as this association to a file
> would not be determined by the Java source code.
>

I only hope that Rhino doesn't take our facetiousness as mean-spirited,
and that he will respond with an indication of where his content resides.

From: Mike Schilling on
Arne Vajh�j wrote:
> On 03-03-2010 19:59, markspace wrote:
>> Tom Anderson wrote:
>>> You reckon? I'm not sure how much use any of the parser APIs are for
>>> writing XML. Parsing is traditionally very much about reading. Which
>>> is the opposite of writing.
>>
>> As far as I know, all of these parsers can also be used for writing,
>> except for XPath.
>
> A parser is by definition reading.
>
> But:
>
> W3C DOM : reading + in recent versions writing
> SAX : reading
> JAXB : reading + writing
> StAX : reading + writing
> JDOM : reading + writing
>
> So I would say 3.5 out of 5.
>
> XPath is not a parser but a query tool to be used
> with W3C DOM or JDOM.

XSLT can be used turn SAX events into an XML-formatted stream, though I've
never seen that used as a way of composing XML. Hmm.

import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLFilterImpl;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;

public class MakeXML
{
public static void main(String[] args) throws Exception
{
Transformer xf = TransformerFactory.newInstance().newTransformer();
xf.transform(
new SAXSource(new XMLEventGenerator(), null),
new StreamResult(System.out));
}

public static class XMLEventGenerator extends XMLFilterImpl
{
public void parse(String systemId) throws SAXException, IOException
{
generateEvents();
}

public void parse(InputSource input) throws SAXException,
IOException
{
generateEvents();
}

private void generateEvents() throws SAXException
{
ContentHandler ch = getContentHandler();
ch.startDocument();
ch.startElement(null, "start", "start", null);
ch.endElement(null, "start", "start");
ch.endDocument();
}
}
}

outputs :

<?xml version="1.0" encoding="UTF-8"?><start/>