public class SAXWriter
extends java.lang.Object
This class is provided because I needed it and because I think it would be handy to have a Writer that takes at least part of the DocumentHandler interface not to read but to write a document preventing me to have to spell out all the tags every time I need to write XML data to file.
Public Members throw SAXException, instead of IOException to keep in line with the SAX specification. Possible next versions will throw subtypes of SAXException, containing writing information and position.
Note: The Writer writes to files only if you supply it by String no URL connections will be openend. If you want to write to another target use the SAXWriter(Writer) constructor instead.
| Constructor and Description |
|---|
SAXWriter()
Default empty constructor.
|
SAXWriter(java.lang.String location)
Constructor
Sets the file to open a StreamWriter on.
|
SAXWriter(java.io.Writer writer)
Constructor
Sets the Writer where the output will go.
|
| Modifier and Type | Method and Description |
|---|---|
void |
endComment()
end a comment in an XML document
|
void |
endDocument()
ends the Document.
|
void |
endElement(java.lang.String name)
end Element.
|
void |
endElement(java.lang.String namespaceURI,
java.lang.String localname,
java.lang.String qname)
end Element.
|
void |
endPrefixMapping(java.lang.String prefix) |
boolean |
getFeature(java.lang.String name) |
java.io.Writer |
getWriter()
gets the Writer used for output
|
void |
insertDescriptor()
insert Descriptor.
|
void |
insertElement(java.lang.String name,
org.xml.sax.Attributes attr)
inserts an EMPTY Element
|
void |
insertElement(java.lang.String namespaceURI,
java.lang.String localname,
java.lang.String qname,
org.xml.sax.Attributes attr)
inserts an EMPTY Element
|
void |
setContent(java.lang.String content)
sets the Character Content of an Element.
|
void |
setFeature(java.lang.String name,
boolean value) |
void |
setWriter(java.io.Writer writer)
sets the Writer used for output
|
void |
startComment()
start a comment in an XML document
|
void |
startDocument()
start Document.
|
void |
startElement(java.lang.String name,
org.xml.sax.Attributes attr)
start Element.
|
void |
startElement(java.lang.String namespaceURI,
java.lang.String localname,
java.lang.String qname,
org.xml.sax.Attributes attr)
start Element.
|
void |
startPrefixMapping(java.lang.String prefix,
java.lang.String uri) |
public SAXWriter()
public SAXWriter(java.io.Writer writer)
writer - java.io.Writerpublic SAXWriter(java.lang.String location)
location - Stringpublic void startDocument()
throws org.xml.sax.SAXException
Does not insert the default processing Instruction for XML. You have to call insertDescriptor() yourself.
org.xml.sax.SAXExceptioninsertDescriptor()public void endDocument()
throws org.xml.sax.SAXException
org.xml.sax.SAXExceptionpublic void insertDescriptor()
throws org.xml.sax.SAXException
Inserts a descriptor of the type: <xml version="1.0" encoding="UTF-8" > into the document. If you need it. If you need it call it after startDocument to end up with a valid xml document.
org.xml.sax.SAXExceptionpublic void startPrefixMapping(java.lang.String prefix,
java.lang.String uri)
throws org.xml.sax.SAXException
org.xml.sax.SAXExceptionContentHandler.startPrefixMapping(String, String)public void endPrefixMapping(java.lang.String prefix)
throws org.xml.sax.SAXException
org.xml.sax.SAXExceptionContentHandler.endPrefixMapping(String)public void startComment()
throws org.xml.sax.SAXException
any content or element written after calling start en before calling end will be written inside the comment
org.xml.sax.SAXException - if start cannot be writtenpublic void endComment()
throws org.xml.sax.SAXException
any content or element written after calling start en before calling end will be written inside the comment
org.xml.sax.SAXException - if end cannot be writtenpublic void startElement(java.lang.String name,
org.xml.sax.Attributes attr)
throws org.xml.sax.SAXException
For EMPTY elements use insertElement.
You can insert null for the AttributeList if you don't need it. Parsers however will always return an AttributeList Object even it contains no attributes.
name - attr - java.io.IOExceptionorg.xml.sax.SAXExceptioninsertElement(String, Attributes)public void startElement(java.lang.String namespaceURI,
java.lang.String localname,
java.lang.String qname,
org.xml.sax.Attributes attr)
throws org.xml.sax.SAXException
For EMPTY elements use insertElement.
You can insert null for the AttributeList if you don't need it. Parsers however will always return an AttributeList Object even it contains no attributes.
namespaceURI - String, the namespace to declarelocalname - Stringqname - Stringattr - Attributesorg.xml.sax.SAXExceptioninsertElement(String, Attributes)public void endElement(java.lang.String name)
throws org.xml.sax.SAXException
For EMPTY elements use insertElement.
Do not call this after insertElement.
name - String, the local or qname of the elementorg.xml.sax.SAXExceptioninsertElement(String, Attributes)public void endElement(java.lang.String namespaceURI,
java.lang.String localname,
java.lang.String qname)
throws org.xml.sax.SAXException
For EMPTY elements use insertElement.
Do not call this after insertElement.
namespaceURI - Stringlocalname - String, the localname the elementqname - String, the qname of the elementorg.xml.sax.SAXExceptioninsertElement(String, Attributes)public void insertElement(java.lang.String name,
org.xml.sax.Attributes attr)
throws org.xml.sax.SAXException
Although I would not see a purpose for this method if you do, you can leave the AttributeList empty. For non-EMPTY Elements use startElement.
name - String, can be either a localname or qnameattr - Attributesorg.xml.sax.SAXExceptionstartElement(String, Attributes)public void insertElement(java.lang.String namespaceURI,
java.lang.String localname,
java.lang.String qname,
org.xml.sax.Attributes attr)
throws org.xml.sax.SAXException
For non-EMPTY Elements use startElement.
namespaceURI - String, the namespace to declarelocalname - String, can be either a localname or qnameqname - String, null if setting the default namespaceattr - Attributes, null if no attributes need to be definedorg.xml.sax.SAXExceptionstartElement(String, Attributes)public void setContent(java.lang.String content)
throws org.xml.sax.SAXException
content - org.xml.sax.SAXExceptionpublic boolean getFeature(java.lang.String name)
throws org.xml.sax.SAXNotRecognizedException,
org.xml.sax.SAXNotSupportedException
org.xml.sax.SAXNotRecognizedExceptionorg.xml.sax.SAXNotSupportedExceptionpublic void setFeature(java.lang.String name,
boolean value)
throws org.xml.sax.SAXNotRecognizedException,
org.xml.sax.SAXNotSupportedException
org.xml.sax.SAXNotRecognizedExceptionorg.xml.sax.SAXNotSupportedExceptionpublic java.io.Writer getWriter()
public void setWriter(java.io.Writer writer)
writer -