Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

self-closing tags with XMLEventWriter

Tags:

java

xml

stax

So the question is pretty much as stated in the title. I am doing some xml work and using XMLEventWriter. The big issue I'm having is that I need to create some self closing tags

The problem is that I haven't figured out a way to do this with the eventWriter. I have tried everything I can think of using XMLEventFactory but nothing seems to work. Any help would be greatly appreciated.

like image 709
user141444 Avatar asked Nov 05 '22 20:11

user141444


1 Answers

I'm not sure if this is possible using XMLEventWriter. It is certainly possible with XMLStreamWriter.

If you are stuck with XMLEventWriter, you could transform the data afterwards.

Reader xml = new StringReader("<?xml version=\"1.0\"?><foo></foo>");
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.transform(new StreamSource(xml),
    new StreamResult(System.out));

The output of the above code is:

<?xml version="1.0" encoding="UTF-8"?><foo/>
like image 156
McDowell Avatar answered Nov 12 '22 18:11

McDowell