So my code is simple and it creates what I want it to create, but I don't know if I'm really seeing any benefit to using the javax.xml.stream
package for this.
Are my requirements simply too basic to really utilize the class? The only real benefits I see are that the writer.EndElement()
and writer.EndDocument()
properly close the tags, however I am not aware of a way to create new lines or tabs (aka proper formatting) without manually writing them as I've done below.
public void WriteUserInfo(String username, String password)
throws FileNotFoundException, XMLStreamException {
outputStream = new FileOutputStream(getXmlFile());
factory = XMLOutputFactory.newInstance();
writer = factory.createXMLStreamWriter(outputStream);
writer.writeStartDocument(XMLTAG);
writer.writeCharacters("\n");
writer.writeStartElement(USER);
writer.writeCharacters("\n\t");
writer.writeStartElement(USERNAME);
writer.writeCharacters(username);
writer.writeEndElement();
writer.writeCharacters("\n\t");
writer.writeStartElement(PASSWORD);
writer.writeCharacters(password);
writer.writeEndElement();
writer.writeCharacters("\n");
writer.writeEndDocument();
writer.close();
}