0

The following code

import java.io.File;
import java.io.FileReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception  {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("out.xml"));
        xsr.nextTag(); // Advance to statements element

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {

            t.transform(new StAXSource(xsr), new StreamResult("result.txt"));
        }
    }
} 

produces some txt, and on the first line of it there are processing instructions (<?xml version="1.0">). Where do they come from and how to get rid of them? And how to manipulate them?

StackExploded
  • 539
  • 7
  • 17

2 Answers2

0

The line <?xml version="1.0"?> is an xml declaration. According to the W3C XML Recommendation

XML documents SHOULD begin with an XML declaration which specifies the version of XML being used.

it is supposed to be there (albeit not mandatory).

Btw. in a strict technical sense it is not a processing instruction.

Edit: the encoding can be changed with:

t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
Henry
  • 42,982
  • 7
  • 68
  • 84
  • i mean HOW does it appear in my txt? What if i want it to be `` appears because "it is supposed to be there" – StackExploded Feb 12 '14 at 17:02
  • thank you . If you know how to solve [this](http://stackoverflow.com/questions/21731363/xml-transform-and-linebreak-characters/21731529?noredirect=1#21731529), it would be supercool as well – StackExploded Feb 12 '14 at 17:15
  • WRONG-WRONG. In theory It should work, BUT it doesn't. check [this](http://stackoverflow.com/questions/9598345/omitting-xml-declaration-when-invoking-transformer-with-staxresult) question. This is the problem I have – StackExploded Feb 12 '14 at 19:09
0

You can disable the creation of the xml declaration by setting the output properties on the transformer:

t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

If you want to generate text files from the xml then you probably want to specify an XSLT source in the TransformerFactory.newInstance call and set the output properties in that XSLT.

Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
  • WRONG-WRONG. In theory It should work, BUT it doesn't. check [this](http://stackoverflow.com/questions/9598345/omitting-xml-declaration-when-invoking-transformer-with-staxresult) question. This is the problem I have – StackExploded Feb 12 '14 at 19:08
  • That question seems to be about using a `StaxResult` and even says that the xml decl is omitted when using a `StreamResult`. But I did not actually run the code to verify this. – Jörn Horstmann Feb 12 '14 at 19:39
  • yes, you are right. You see, initially my task was to figure out how to get rid of xml declaration generally, and you answered this. But in my code i am using stax result, so, while it worked with the demo, it didn't with my code. It works with StreamResult indeed – StackExploded Feb 12 '14 at 20:55
  • but it was a different problem, so, guess this was answered – StackExploded Feb 13 '14 at 19:00