public class XMLSerializer extends Object
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
Constructor and Description |
---|
XMLSerializer()
Creates a new XMLSerializer with default options.
objectName : 'o'
arrayName : 'a'
elementName : 'e'
typeHinstEnabled : true
typeHinstCompatibility : true
namespaceLenient : false
expandableProperties : []
skipNamespaces : false
removeNameSpacePrefixFromElement : false
trimSpaces : false
expandableProperties : []
skipWhitespace : false
performAutoExpansion : false
keepCData : false
escapeLowerChars : false
keepArrayName : false
forcedArrayElements : []
parseJsonLiterals : true
|
Modifier and Type | Method and Description |
---|---|
void |
addNamespace(String prefix,
String uri)
Adds a namespace declaration to the root element.
|
void |
addNamespace(String prefix,
String uri,
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. |
void |
clearNamespaces()
Removes all namespaces declarations (from root an elements).
|
void |
clearNamespaces(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. |
String |
getArrayName()
Returns the name used for JSONArray.
|
String |
getElementName()
Returns the name used for JSONArray elements.
|
Map |
getElementNamespace(String elementName)
Returns a read-only view of the particular element name space if found.
|
String[] |
getExpandableProperties()
Returns a list of properties to be expanded from child to parent.
|
Collection<String> |
getForcedArrayElements()
Returns the set of XML elements that force their children to be treated as array elements.
|
String |
getObjectName()
Returns the name used for JSONArray.
|
String |
getRootName()
Returns the name used for the root element.
|
Map |
getRootNamespace()
Returns a read-only view of the root name space.
|
boolean |
isForceTopLevelObject() |
boolean |
isNamespaceLenient()
Returns whether this serializer is tolerant to namespaces without URIs or
not.
|
boolean |
isParseJsonLiterals()
Returns whether JSON literals are parsed as JSON or not.
|
boolean |
isRemoveNamespacePrefixFromElements()
Returns whether this serializer will remove namespace prefix from elements
or not.
|
boolean |
isSkipNamespaces()
Returns whether this serializer will skip adding namespace declarations to
elements or not.
|
boolean |
isSkipWhitespace()
Returns whether this serializer will skip whitespace or not.
|
boolean |
isSortPropertyNames()
Returns whether this serializer will sort object properties by name or not.
|
boolean |
isTrimSpaces()
Returns whether this serializer will trim leading and trealing whitespace
from values or not.
|
boolean |
isTypeHintsCompatibility()
Returns true if types hints will have a 'json_' prefix or not.
|
boolean |
isTypeHintsEnabled()
Returns true if JSON types will be included as attributes.
|
JSON |
read(String xml)
Creates a JSON value from a XML string.
|
JSON |
readFromFile(File file)
Creates a JSON value from a File.
|
JSON |
readFromFile(String path)
Creates a JSON value from a File.
|
JSON |
readFromStream(InputStream stream)
Creates a JSON value from an input stream.
|
void |
removeNamespace(String prefix)
Removes a namespace from the root element.
|
void |
removeNamespace(String prefix,
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. |
void |
setArrayName(String arrayName)
Sets the name used for JSONArray.
Default is 'a'. |
void |
setElementName(String elementName)
Sets the name used for JSONArray elements.
Default is 'e'. |
void |
setEscapeLowerChars(boolean escape)
Sets whether this serializer should escape characters lower than ' ' in texts.
|
void |
setExpandableProperties(String[] expandableProperties)
Sets the list of properties to be expanded from child to parent.
|
void |
setForcedArrayElements(Collection<String> forcedArrayElements)
Defines the set of XML elements that force their children to be treated as array elements.
|
void |
setForceTopLevelObject(boolean forceTopLevelObject) |
void |
setKeepArrayName(boolean keepName)
Sets whether this serializer should keep the XML element being an array.
|
void |
setKeepCData(boolean keepCData)
Sets whether this serializer should keep the CDATA information in the value or not.
|
void |
setNamespace(String prefix,
String uri)
Sets the namespace declaration to the root element.
Any previous values are discarded. |
void |
setNamespace(String prefix,
String uri,
String elementName)
Adds a namespace declaration to an element.
Any previous values are discarded. |
void |
setNamespaceLenient(boolean namespaceLenient)
Sets whether this serializer is tolerant to namespaces without URIs or not.
|
void |
setObjectName(String objectName)
Sets the name used for JSONObject.
Default is 'o'. |
void |
setParseJsonLiterals(boolean parseJsonLiterals)
Sets whether JSON literals are parsed as JSON or not.
|
void |
setPerformAutoExpansion(boolean autoExpansion)
Sets whether this serializer should perform automatic expansion of array elements or not.
|
void |
setRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFromElements)
Sets if this serializer will remove namespace prefix from elements when
reading.
|
void |
setRootName(String rootName)
Sets the name used for the root element.
|
void |
setSkipNamespaces(boolean skipNamespaces)
Sets if this serializer will skip adding namespace declarations to
elements when reading.
|
void |
setSkipWhitespace(boolean skipWhitespace)
Sets if this serializer will skip whitespace when reading.
|
void |
setSortPropertyNames(boolean sortPropertyNames)
Returns whether this serializer will sort object properties by name or not.
|
void |
setTrimSpaces(boolean trimSpaces)
Sets if this serializer will trim leading and trealing whitespace from
values when reading.
|
void |
setTypeHintsCompatibility(boolean typeHintsCompatibility)
Sets whether types hints will have a 'json_' prefix or not.
|
void |
setTypeHintsEnabled(boolean typeHintsEnabled)
Sets whether JSON types will be included as attributes.
|
String |
write(JSON json)
Writes a JSON value into a XML string with UTF-8 encoding.
|
String |
write(JSON json,
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. |
public XMLSerializer()
objectName
: 'o'arrayName
: 'a'elementName
: 'e'typeHinstEnabled
: truetypeHinstCompatibility
: truenamespaceLenient
: falseexpandableProperties
: []skipNamespaces
: falseremoveNameSpacePrefixFromElement
: falsetrimSpaces
: falseexpandableProperties
: []skipWhitespace
: falseperformAutoExpansion
: falsekeepCData
: falseescapeLowerChars
: falsekeepArrayName
: falseforcedArrayElements
: []parseJsonLiterals
: truepublic boolean isParseJsonLiterals()
public void setParseJsonLiterals(boolean parseJsonLiterals)
public boolean isSortPropertyNames()
public void setSortPropertyNames(boolean sortPropertyNames)
public void addNamespace(String prefix, String uri)
prefix
- namespace prefixuri
- namespace uripublic void addNamespace(String prefix, String uri, String elementName)
prefix
- namespace prefixuri
- namespace urielementName
- name of target elementpublic Map getRootNamespace()
public Map getElementNamespace(String elementName)
public void clearNamespaces()
public void clearNamespaces(String elementName)
elementName
- name of target elementpublic String getArrayName()
public void setArrayName(String arrayName)
public String getElementName()
public void setElementName(String elementName)
public String[] getExpandableProperties()
public void setExpandableProperties(String[] expandableProperties)
public String getObjectName()
public void setObjectName(String objectName)
public String getRootName()
public void setRootName(String rootName)
public boolean isForceTopLevelObject()
public void setForceTopLevelObject(boolean forceTopLevelObject)
public boolean isNamespaceLenient()
public void setNamespaceLenient(boolean namespaceLenient)
public boolean isRemoveNamespacePrefixFromElements()
public void setRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFromElements)
public boolean isSkipNamespaces()
public void setSkipNamespaces(boolean skipNamespaces)
public boolean isSkipWhitespace()
public void setSkipWhitespace(boolean skipWhitespace)
public boolean isTrimSpaces()
public void setTrimSpaces(boolean trimSpaces)
public boolean isTypeHintsCompatibility()
public void setTypeHintsCompatibility(boolean typeHintsCompatibility)
public boolean isTypeHintsEnabled()
public void setTypeHintsEnabled(boolean typeHintsEnabled)
public Collection<String> getForcedArrayElements()
public void setForcedArrayElements(Collection<String> forcedArrayElements)
public JSON read(String xml)
xml
- A well-formed xml document in a StringJSONException
- if the conversion from XML to JSON can't be made for
I/O or format reasons.public JSON readFromFile(File file)
file
- JSONException
- if the conversion from XML to JSON can't be made for
I/O or format reasons.public JSON readFromFile(String path)
path
- JSONException
- if the conversion from XML to JSON can't be made for
I/O or format reasons.public JSON readFromStream(InputStream stream)
stream
- JSONException
- if the conversion from XML to JSON can't be made for
I/O or format reasons.public void removeNamespace(String prefix)
prefix
- namespace prefixpublic void removeNamespace(String prefix, String elementName)
prefix
- namespace prefixelementName
- name of target elementpublic void setNamespace(String prefix, String uri)
prefix
- namespace prefixuri
- namespace uripublic void setNamespace(String prefix, String uri, String elementName)
prefix
- namespace prefixuri
- namespace urielementName
- name of target elementpublic void setPerformAutoExpansion(boolean autoExpansion)
public void setKeepCData(boolean keepCData)
keepCData
- True to keep CDATA, false to only use the text value.public void setEscapeLowerChars(boolean escape)
escape
- True to escape, false otherwise.public void setKeepArrayName(boolean keepName)
keepName
- True to include the element name in the JSON object, false otherwise.public String write(JSON json)
json
- The JSON value to transformJSONException
- if the conversion from JSON to XML can't be made for
I/O reasons.public String write(JSON json, String encoding)
json
- The JSON value to transformencoding
- The xml encoding to useJSONException
- if the conversion from JSON to XML can't be made for
I/O reasons or the encoding is not supported.