0

I've seen the answers provided [here] (Formatting XML file using StAX) and [here] (merge XML using STAX XMLStreamWriter)

In both cases it did not work. In both cases it was because my IDE, netbeans, doesn't recognize the methods as valid. This is driving me crazy. thanks in advance for your help.

Here's the code that doesn't work, I'm simply trying to wrap my writer in an IndentingWriter.

             XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
             XMLEventWriter writer = null;
         try {
             writer = outputFactory
                   .createXMLEventWriter(new FileOutputStream(args[1]), "UTF-8");
             writer = new IndentingXMLEventWriter(writer);

        } catch (XMLStreamException ex) {
            Logger.getLogger(XMLReader.class.getName()).log(Level.SEVERE, null, ex);
        }

Here is a list of my imports:

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.logging.Level; import java.util.logging.Logger; import java.util.StringTokenizer; import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.Characters; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartDocument; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent;

Community
  • 1
  • 1
Ben Zifkin
  • 892
  • 2
  • 8
  • 16
  • 1
    What version of Java are you using? What imports did you use? Can you provide a short but complete example which you'd expect to work, but which doesn't? – Jon Skeet Jul 21 '14 at 18:18
  • @JonSkeet added the code adn I'm running java version 1.7.0_55 – Ben Zifkin Jul 21 '14 at 18:35
  • That's not a short but complete program. In particular, imports are important here... – Jon Skeet Jul 21 '14 at 18:36
  • You could use `XMLEventFactory#createCharacters("...")` to insert the required newlines and indentation yourself. See http://stackoverflow.com/q/4616383/18157, which could be a dup but it's for stream output instead of event output; but the principles are the same. – Jim Garrison Jul 21 '14 at 18:41
  • @JimGarrison I tried a very similar solution using the eventwriter and it didn't work, again netbeans just didn't recognize the method my code was something like `XMLEvent newline = eventFactory.createCharacters("/n"); – Ben Zifkin Jul 21 '14 at 18:48
  • If NetBeans isn't recognizing methods that are in the standard API, then you have other problems, like a very old JDK on your classpath or you are importing classes from a different package than you think. – Jim Garrison Jul 21 '14 at 18:49
  • You seem to have _two_ problems: 1) unable to use methods that should be visible; and 2) output formatting in `XMLEventWriter `. You need to solve problem 1 before thinking about tackling problem 2. – Jim Garrison Jul 21 '14 at 18:51

1 Answers1

0

XMLOutputFactory.createXMLEventWriter has another signature, where it accepts a Writer instead of an OutputStream. If you use that, you can use the append method to add a newline wherever you want (shown here with a StringWriter, but should work for other types of Writers as well):

StringWriter outputXml = new StringWriter();
XMLEventWriter eventWriter = factory.createXMLEventWriter(outputXml);
XMLEvent event = eventFactory.createStartDocument();     
eventWriter.add(event);
eventWriter.flush(); // important!
outputXml.append("\n"); // add newline after XML declaration

You can even output stuff inside XML elements, not just necessarily in between elements. This can come handy if you need to emit some legacy or non-standard XML format.

Istvan Devai
  • 3,962
  • 23
  • 21