Java Reference
In-Depth Information
writeEndElement() methods in combination. Of course, elements can have nes-
ted elements. You have the responsibility to call these in proper sequence to create
well-formed documents. Use the writeAttribute() method to place an attribute
name and value into the current element. You should call writeAttribute() im-
mediately after calling the writeStartElement() method. Finally, signal the end
of the document with the writeEndDocument() method and close the Writer in-
stance.
One interesting point of using the XMLStreamWriter is that it does not format
the document output. Unless you specifically use the writeCharacters() method
to output space and newline characters, the output will stream to a single unformatted
line. Of course, this doesn't invalidate the resulting XML file, but it does make it in-
convenient and difficult for humans to read. Therefore, you should consider using the
writeCharacters() method to output spacing and newline characters as needed
to create a human readable document. You can safely ignore this method of writing ad-
ditional whitespace and line breaks if you do not need a document for human readabil-
ity. Regardless of the format, the XML document will be well formed because it ad-
heres to correct XML syntax.
The command-line usage pattern for this example code is this:
java org.java8recipes.chapter20.recipe20_1.DocWriter
<outputXmlFile>
Invoke this application to create a file named patients.xml in the following
way:
java org.java8recipes.chapter20.recipe20_1.DocWriter
patients.xml
20-2. Reading an XML File
Problem
You need to parse an XML document, retrieving known elements and attributes.
Solution 1
Search WWH ::




Custom Search