|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.json.xml.XMLSerializer
public class XMLSerializer
Utility 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
Constructor Summary | |
---|---|
XMLSerializer()
Creates a new XMLSerializer with default options. |
Method Summary | |
---|---|
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. |
void |
clearNamespaces()
Removes all namespaces declarations (from root an elements). |
void |
clearNamespaces(String elementName)
Removes all namespace declarations from an element. |
String |
getArrayName()
Returns the name used for JSONArray. |
String |
getElementName()
Returns the name used for JSONArray elements. |
String[] |
getExpandableProperties()
Returns a list of properties to be expanded from child to parent. |
String |
getObjectName()
Returns the name used for JSONArray. |
String |
getRootName()
Returns the name used for the root element. |
boolean |
isNamespaceLenient()
Returns wether this serializer is tolerant to namespaces without URIs or not. |
boolean |
isRemoveNamespacePrefixFromElements()
Returns wether this serializer will remove namespace prefix from elements or not. |
boolean |
isSkipNamespaces()
Returns wether this serializer will skip adding namespace declarations to elements or not. |
boolean |
isTrimSpaces()
Returns wether this serializer will trim leading and trealing whitespace from values 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. |
void |
setArrayName(String arrayName)
Sets the name used for JSONArray. |
void |
setElementName(String elementName)
Sets the name used for JSONArray elements. |
void |
setExpandableProperties(String[] expandableProperties)
Sets the list of properties to be expanded from child to parent. |
void |
setNamespace(String prefix,
String uri)
Sets the namespace declaration to the root element. |
void |
setNamespace(String prefix,
String uri,
String elementName)
Adds a namespace declaration to an element. |
void |
setNamespaceLenient(boolean namespaceLenient)
Sets wether this serializer is tolerant to namespaces without URIs or not. |
void |
setObjectName(String objectName)
Sets the name used for JSONObject. |
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 |
setTrimSpaces(boolean trimSpaces)
Sets if this serializer will trim leading and trealing whitespace from values when reading. |
void |
setTypeHintsEnabled(boolean typeHintsEnabled)
Sets wether 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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public XMLSerializer()
objectName
: 'o'arrayName
: 'a'elementName
: 'e'typeHinstEnabled
: truenamespaceLenient
: falseexpandableProperties
: []skipNamespaces
: falseremoveNameSpacePrefixFromElement
: falsetrimSpaces
: false
Method Detail |
---|
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 void clearNamespaces()
public void clearNamespaces(String elementName)
elementName
- name of target elementpublic String getArrayName()
public String getElementName()
public String[] getExpandableProperties()
public String getObjectName()
public String getRootName()
public boolean isNamespaceLenient()
public boolean isRemoveNamespacePrefixFromElements()
public boolean isSkipNamespaces()
public boolean isTrimSpaces()
public boolean isTypeHintsEnabled()
public JSON read(String xml)
xml
- A well-formed xml document in a String
JSONException
- 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 setArrayName(String arrayName)
public void setElementName(String elementName)
public void setExpandableProperties(String[] expandableProperties)
public 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 setNamespaceLenient(boolean namespaceLenient)
public void setObjectName(String objectName)
public void setRemoveNamespacePrefixFromElements(boolean removeNamespacePrefixFromElements)
public void setRootName(String rootName)
public void setSkipNamespaces(boolean skipNamespaces)
public void setTrimSpaces(boolean trimSpaces)
public void setTypeHintsEnabled(boolean typeHintsEnabled)
public String write(JSON json)
json
- The JSON value to transform
JSONException
- 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 use
JSONException
- if the conversion from JSON to XML can't be made for
I/O reasons or the encoding is not supported.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |