0
votes

i'm having some trouble to write a particular xml tag (using an XmlStreamWriter).

Basically, we have an XMLWriter that is based on "javax.xml.stream.XMLStreamWriter" (STaX) which is working fine. All the xml files that are written begin automatically with the tag :

< ?xml version="1.0" encoding="ISO-8859-1"?> (first space is added to display the xml line)

What we need now is to add a new line (stylesheet) to write every single xml file with the beginning lines :

< ?xml version="1.0" encoding="ISO-8859-1"?> (same as above) < ?xml-stylesheet type="text/xsl" href="myXsl.xsl"?> (same as above)

I tried to do it the hard-coded way, using the XmlStreamWriter.writeCharacters(String) but the problem is that "<" and ">" are special characters so the output in the xml file is "<"/">".

Also, this is not very clean coding..

In the same way that STaX writes the first line using "XMLStreamWriter.writeStartDocument(String encoding, String version)", does anyone know an XML (XSL/XSLT?) API which WRITER does write the tag :

< ?xml-stylesheet type="text/xsl" href="myXsl.xsl"?> (same as above)

Any help would be much appreciated :)

1

1 Answers

2
votes

It is called a processing instruction.

See XMLStreamWriter.writeProcessingInstruction, for instance.

In your case:

writer.writeProcessingInstruction("xml-stylesheet",
    "type=\"text/xsl\" href=\"myXsl.xsl\"");

(Not tested.)