Package net.sf.json.xml
Class XMLSerializer
- java.lang.Object
-
- net.sf.json.xml.XMLSerializer
-
public class XMLSerializer extends java.lang.ObjectUtility class for transforming JSON to XML an back.
When transforming JSONObject and JSONArray instances to XML, this class will add hints for converting back to JSON.
Examples:
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}"); String xml = new XMLSerializer().write( json );json true 1 JSONArray json = JSONArray.fromObject("[1,2,3]"); String xml = new XMLSerializer().write( json );1 2 3 - Author:
- Andres Almiray
-
-
Constructor Summary
Constructors Constructor Description XMLSerializer()Creates a new XMLSerializer with default options.
objectName: 'o'arrayName: 'a'elementName: 'e'typeHinstEnabled: truetypeHinstCompatibility: truenamespaceLenient: falseexpandableProperties: []skipNamespaces: falseremoveNameSpacePrefixFromElement: falsetrimSpaces: false
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddNamespace(java.lang.String prefix, java.lang.String uri)Adds a namespace declaration to the root element.voidaddNamespace(java.lang.String prefix, java.lang.String uri, java.lang.String elementName)Adds a namespace declaration to an element.
If the elementName param is null or blank, the namespace declaration will be added to the root element.voidclearNamespaces()Removes all namespaces declarations (from root an elements).voidclearNamespaces(java.lang.String elementName)Removes all namespace declarations from an element.
If the elementName param is null or blank, the declarations will be removed from the root element.java.lang.StringgetArrayName()Returns the name used for JSONArray.java.lang.StringgetElementName()Returns the name used for JSONArray elements.java.lang.String[]getExpandableProperties()Returns a list of properties to be expanded from child to parent.java.lang.StringgetObjectName()Returns the name used for JSONArray.java.lang.StringgetRootName()Returns the name used for the root element.booleanisForceTopLevelObject()booleanisNamespaceLenient()Returns wether this serializer is tolerant to namespaces without URIs or not.booleanisRemoveNamespacePrefixFromElements()Returns wether this serializer will remove namespace prefix from elements or not.booleanisSkipNamespaces()Returns wether this serializer will skip adding namespace declarations to elements or not.booleanisSkipWhitespace()Returns wether this serializer will skip whitespace or not.booleanisTrimSpaces()Returns wether this serializer will trim leading and trealing whitespace from values or not.booleanisTypeHintsCompatibility()Returns true if types hints will have a 'json_' prefix or not.booleanisTypeHintsEnabled()Returns true if JSON types will be included as attributes.JSONread(java.lang.String xml)Creates a JSON value from a XML string.JSONreadFromFile(java.io.File file)Creates a JSON value from a File.JSONreadFromFile(java.lang.String path)Creates a JSON value from a File.JSONreadFromStream(java.io.InputStream stream)Creates a JSON value from an input stream.voidremoveNamespace(java.lang.String prefix)Removes a namespace from the root element.voidremoveNamespace(java.lang.String prefix, java.lang.String elementName)Removes a namespace from the root element.
If the elementName is null or blank, the namespace will be removed from the root element.voidsetArrayName(java.lang.String arrayName)Sets the name used for JSONArray.
Default is 'a'.voidsetElementName(java.lang.String elementName)Sets the name used for JSONArray elements.
Default is 'e'.voidsetExpandableProperties(java.lang.String[] expandableProperties)Sets the list of properties to be expanded from child to parent.voidsetForceTopLevelObject(boolean forceTopLevelObject)voidsetNamespace(java.lang.String prefix, java.lang.String uri)Sets the namespace declaration to the root element.
Any previous values are discarded.voidsetNamespace(java.lang.String prefix, java.lang.String uri, java.lang.String elementName)Adds a namespace declaration to an element.
Any previous values are discarded.voidsetNamespaceLenient(boolean namespaceLenient)Sets wether this serializer is tolerant to namespaces without URIs or not.voidsetObjectName(java.lang.String objectName)Sets the name used for JSONObject.
Default is 'o'.voidsetRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFromElements)Sets if this serializer will remove namespace prefix from elements when reading.voidsetRootName(java.lang.String rootName)Sets the name used for the root element.voidsetSkipNamespaces(boolean skipNamespaces)Sets if this serializer will skip adding namespace declarations to elements when reading.voidsetSkipWhitespace(boolean skipWhitespace)Sets if this serializer will skip whitespace when reading.voidsetTrimSpaces(boolean trimSpaces)Sets if this serializer will trim leading and trealing whitespace from values when reading.voidsetTypeHintsCompatibility(boolean typeHintsCompatibility)Sets wether types hints will have a 'json_' prefix or not.voidsetTypeHintsEnabled(boolean typeHintsEnabled)Sets wether JSON types will be included as attributes.java.lang.Stringwrite(JSON json)Writes a JSON value into a XML string with UTF-8 encoding.java.lang.Stringwrite(JSON json, java.lang.String encoding)Writes a JSON value into a XML string with an specific encoding.
If the encoding string is null it will use UTF-8.
-
-
-
Constructor Detail
-
XMLSerializer
public XMLSerializer()
Creates a new XMLSerializer with default options.
objectName: 'o'arrayName: 'a'elementName: 'e'typeHinstEnabled: truetypeHinstCompatibility: truenamespaceLenient: falseexpandableProperties: []skipNamespaces: falseremoveNameSpacePrefixFromElement: falsetrimSpaces: false
-
-
Method Detail
-
addNamespace
public void addNamespace(java.lang.String prefix, java.lang.String uri)Adds a namespace declaration to the root element.- Parameters:
prefix- namespace prefixuri- namespace uri
-
addNamespace
public void addNamespace(java.lang.String prefix, java.lang.String uri, java.lang.String elementName)Adds a namespace declaration to an element.
If the elementName param is null or blank, the namespace declaration will be added to the root element.- Parameters:
prefix- namespace prefixuri- namespace urielementName- name of target element
-
clearNamespaces
public void clearNamespaces()
Removes all namespaces declarations (from root an elements).
-
clearNamespaces
public void clearNamespaces(java.lang.String elementName)
Removes all namespace declarations from an element.
If the elementName param is null or blank, the declarations will be removed from the root element.- Parameters:
elementName- name of target element
-
getArrayName
public java.lang.String getArrayName()
Returns the name used for JSONArray.
-
getElementName
public java.lang.String getElementName()
Returns the name used for JSONArray elements.
-
getExpandableProperties
public java.lang.String[] getExpandableProperties()
Returns a list of properties to be expanded from child to parent.
-
getObjectName
public java.lang.String getObjectName()
Returns the name used for JSONArray.
-
getRootName
public java.lang.String getRootName()
Returns the name used for the root element.
-
isForceTopLevelObject
public boolean isForceTopLevelObject()
-
isNamespaceLenient
public boolean isNamespaceLenient()
Returns wether this serializer is tolerant to namespaces without URIs or not.
-
isRemoveNamespacePrefixFromElements
public boolean isRemoveNamespacePrefixFromElements()
Returns wether this serializer will remove namespace prefix from elements or not.
-
isSkipNamespaces
public boolean isSkipNamespaces()
Returns wether this serializer will skip adding namespace declarations to elements or not.
-
isSkipWhitespace
public boolean isSkipWhitespace()
Returns wether this serializer will skip whitespace or not.
-
isTrimSpaces
public boolean isTrimSpaces()
Returns wether this serializer will trim leading and trealing whitespace from values or not.
-
isTypeHintsCompatibility
public boolean isTypeHintsCompatibility()
Returns true if types hints will have a 'json_' prefix or not.
-
isTypeHintsEnabled
public boolean isTypeHintsEnabled()
Returns true if JSON types will be included as attributes.
-
read
public JSON read(java.lang.String xml)
Creates a JSON value from a XML string.- Parameters:
xml- A well-formed xml document in a String- Returns:
- a JSONNull, JSONObject or JSONArray
- Throws:
JSONException- if the conversion from XML to JSON can't be made for I/O or format reasons.
-
readFromFile
public JSON readFromFile(java.io.File file)
Creates a JSON value from a File.- Parameters:
file-- Returns:
- a JSONNull, JSONObject or JSONArray
- Throws:
JSONException- if the conversion from XML to JSON can't be made for I/O or format reasons.
-
readFromFile
public JSON readFromFile(java.lang.String path)
Creates a JSON value from a File.- Parameters:
path-- Returns:
- a JSONNull, JSONObject or JSONArray
- Throws:
JSONException- if the conversion from XML to JSON can't be made for I/O or format reasons.
-
readFromStream
public JSON readFromStream(java.io.InputStream stream)
Creates a JSON value from an input stream.- Parameters:
stream-- Returns:
- a JSONNull, JSONObject or JSONArray
- Throws:
JSONException- if the conversion from XML to JSON can't be made for I/O or format reasons.
-
removeNamespace
public void removeNamespace(java.lang.String prefix)
Removes a namespace from the root element.- Parameters:
prefix- namespace prefix
-
removeNamespace
public void removeNamespace(java.lang.String prefix, java.lang.String elementName)Removes a namespace from the root element.
If the elementName is null or blank, the namespace will be removed from the root element.- Parameters:
prefix- namespace prefixelementName- name of target element
-
setArrayName
public void setArrayName(java.lang.String arrayName)
Sets the name used for JSONArray.
Default is 'a'.
-
setElementName
public void setElementName(java.lang.String elementName)
Sets the name used for JSONArray elements.
Default is 'e'.
-
setExpandableProperties
public void setExpandableProperties(java.lang.String[] expandableProperties)
Sets the list of properties to be expanded from child to parent.
-
setForceTopLevelObject
public void setForceTopLevelObject(boolean forceTopLevelObject)
-
setNamespace
public void setNamespace(java.lang.String prefix, java.lang.String uri)Sets the namespace declaration to the root element.
Any previous values are discarded.- Parameters:
prefix- namespace prefixuri- namespace uri
-
setNamespace
public void setNamespace(java.lang.String prefix, java.lang.String uri, java.lang.String elementName)Adds a namespace declaration to an element.
Any previous values are discarded. If the elementName param is null or blank, the namespace declaration will be added to the root element.- Parameters:
prefix- namespace prefixuri- namespace urielementName- name of target element
-
setNamespaceLenient
public void setNamespaceLenient(boolean namespaceLenient)
Sets wether this serializer is tolerant to namespaces without URIs or not.
-
setObjectName
public void setObjectName(java.lang.String objectName)
Sets the name used for JSONObject.
Default is 'o'.
-
setRemoveNamespacePrefixFromElements
public void setRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFromElements)
Sets if this serializer will remove namespace prefix from elements when reading.
-
setRootName
public void setRootName(java.lang.String rootName)
Sets the name used for the root element.
-
setSkipNamespaces
public void setSkipNamespaces(boolean skipNamespaces)
Sets if this serializer will skip adding namespace declarations to elements when reading.
-
setSkipWhitespace
public void setSkipWhitespace(boolean skipWhitespace)
Sets if this serializer will skip whitespace when reading.
-
setTrimSpaces
public void setTrimSpaces(boolean trimSpaces)
Sets if this serializer will trim leading and trealing whitespace from values when reading.
-
setTypeHintsCompatibility
public void setTypeHintsCompatibility(boolean typeHintsCompatibility)
Sets wether types hints will have a 'json_' prefix or not.
-
setTypeHintsEnabled
public void setTypeHintsEnabled(boolean typeHintsEnabled)
Sets wether JSON types will be included as attributes.
-
write
public java.lang.String write(JSON json)
Writes a JSON value into a XML string with UTF-8 encoding.- Parameters:
json- The JSON value to transform- Returns:
- a String representation of a well-formed xml document.
- Throws:
JSONException- if the conversion from JSON to XML can't be made for I/O reasons.
-
write
public java.lang.String write(JSON json, java.lang.String encoding)
Writes a JSON value into a XML string with an specific encoding.
If the encoding string is null it will use UTF-8.- Parameters:
json- The JSON value to transformencoding- The xml encoding to use- Returns:
- a String representation of a well-formed xml document.
- Throws:
JSONException- if the conversion from JSON to XML can't be made for I/O reasons or the encoding is not supported.
-
-