Class JsonXmlUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic Element
Returns the element at the specified index in the sequence of JSON-XML elements that are children of the specified XML element.childIterator
(Element element) Returns an iterator over the JSON-XML elements that are children of the specified XML element.Returns a list of the child nodes of the specified XML element that are JSON-XML elements.Removes all the descendant nodes of the specified XML element that are not JSON-XML elements.static Element
copy
(IElementFacade sourceElementFacade, IElementFacade destElementFacade, Element element) Creates and returns a deep copy of the specified JSON-XML element.static int
countChildren
(Element element) Returns a count of the child nodes of the specified XML element that are JSON-XML elements.static int
countDescendants
(Element element) Returns a count of the descendant nodes of the specified XML element that are JSON-XML elements.static IElementFacade
elementFacade
(Element element) Returns a new instance of an element facade that is associated with the owner document of the specified XML element.static boolean
Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other.static boolean
equals
(IElementFacade elementFacade1, Element element1, IElementFacade elementFacade2, Element element2) Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other.static boolean
equals
(IElementFacade elementFacade1, Element element1, IElementFacade elementFacade2, Element element2, boolean includeNameAttr) Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other.static boolean
equalsIgnoreNS
(Element element1, Element element2) Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other.static boolean
equalsIgnoreNS
(Element element1, Element element2, boolean includeNameAttr) Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other.static String
getAttrIgnoreNS
(Element element, String attrName) Returns the value of the attribute of the specified XML element with the specified attribute name, ignoring any namespace prefix of the attribute.static String
Returns the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.static String
getName
(IElementFacade elementFacade, Element element) Returns the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.static String
getNameIgnoreNS
(Element element) Returns the value of the name attribute of the specified XML element, ignoring any namespace prefix of the attribute.static String
Returns the value of the value attribute of the specified XML element, which corresponds to a JSON value.static String
getValue
(IElementFacade elementFacade, Element element) Returns the value of the value attribute of the specified XML element, which corresponds to a JSON value.static String
getValueIgnoreNS
(Element element) Returns the value of the value attribute of the specified XML element, ignoring any namespace prefix of the attribute.static int
Returns a deep hash code for the specified XML element.static int
hashCode
(IElementFacade elementFacade, Element element) Returns a deep hash code for the specified XML element.static int
hashCode
(IElementFacade elementFacade, Element element, boolean includeNameAttr) Returns a deep hash code for the specified XML element.static Element
Returns the parent of the specified element, which is assumed to be an element.static void
Sets the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.static void
setName
(IElementFacade elementFacade, Element element, String name) Sets the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.static void
Sets the value of the value attribute of the specified XML element, which corresponds to a JSON value.static void
setValue
(IElementFacade elementFacade, Element element, String value) Sets the value of the value attribute of the specified XML element, which corresponds to a JSON value.static String
toJsonText
(Element element, boolean printableAsciiOnly) Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and returns the text.static String
toJsonText
(IElementFacade elementFacade, Element element, boolean printableAsciiOnly) Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and returns the text.static String
Generates a textual representation of the tree of JSON-XML elements whose root is the specified XML element and returns the text.static void
Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and writes the text to the specified character stream.static void
writeJson
(IElementFacade elementFacade, Element element, boolean printableAsciiOnly, Writer writer) Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and writes the text to the specified character stream.static void
Generates a textual representation of the tree of JSON-XML elements whose root is the specified XML element and writes the text to the specified character stream.
-
Method Details
-
getName
Returns the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.- Parameters:
element
- the target element.- Returns:
- the value of the name attribute of
element
, ornull
if the element does not have such an attribute.
-
getName
Returns the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object. If an element facade is supplied, it is used to access the attribute. An instance ofSimpleElementFacade
may be used to apply a namespace prefix to the name of the attribute.- Parameters:
elementFacade
- the facade through which the name attribute will be accessed. If it isnull
, the name attribute is accessed by callinggetAttribute(AttrName.NAME)
onelement
.element
- the target element.- Returns:
- the value of the name attribute of
element
, ornull
if the element does not have such an attribute.
-
setName
Sets the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object.- Parameters:
element
- the target element.name
- the value to which the name attribute will be set.
-
setName
Sets the value of the name attribute of the specified XML element, which corresponds to the name of a member of a JSON object. If an element facade is supplied, it is used to access the attribute. An instance ofSimpleElementFacade
may be used to apply a namespace prefix to the name of the attribute.- Parameters:
elementFacade
- the facade through which the name attribute will be accessed. If it isnull
, the name attribute is accessed by callingsetAttribute(AttrName.NAME, name)
onelement
.element
- the target element.name
- the value to which the name attribute will be set.
-
getValue
Returns the value of the value attribute of the specified XML element, which corresponds to a JSON value.- Parameters:
element
- the target element.- Returns:
- the value of the value attribute of
element
, ornull
if the element does not have such an attribute.
-
getValue
Returns the value of the value attribute of the specified XML element, which corresponds to a JSON value. If an element facade is supplied, it is used to access the attribute. An instance ofSimpleElementFacade
may be used to apply a namespace prefix to the name of the attribute.- Parameters:
elementFacade
- the facade through which the value attribute will be accessed. If it isnull
, the value attribute is accessed by callinggetAttribute(AttrName.VALUE)
onelement
.element
- the target element.- Returns:
- the value of the value attribute of
element
, ornull
if the element does not have such an attribute.
-
setValue
Sets the value of the value attribute of the specified XML element, which corresponds to a JSON value.- Parameters:
element
- the target element.value
- the value to which the value attribute will be set.
-
setValue
Sets the value of the value attribute of the specified XML element, which corresponds to a JSON value. If an element facade is supplied, it is used to access the attribute. An instance ofSimpleElementFacade
may be used to apply a namespace prefix to the name of the attribute.- Parameters:
elementFacade
- the facade through which the value attribute will be accessed. If it isnull
, the value attribute is accessed by callingsetAttribute(AttrName.VALUE, name)
onelement
.element
- the target element.value
- the value to which the value attribute will be set.
-
getNameIgnoreNS
Returns the value of the name attribute of the specified XML element, ignoring any namespace prefix of the attribute. The name attribute of the element corresponds to the name of a member of a JSON object.- Parameters:
element
- the target element.- Returns:
- the value of the name attribute of
element
, ignoring any namespace prefix of the attribute, ornull
if the element does not have such an attribute.
-
getValueIgnoreNS
Returns the value of the value attribute of the specified XML element, ignoring any namespace prefix of the attribute. The value attribute of the element corresponds to a JSON value.- Parameters:
element
- the target element.- Returns:
- the value of the value attribute of
element
, ignoring any namespace prefix of the attribute, ornull
if the element does not have such an attribute.
-
getAttrIgnoreNS
Returns the value of the attribute of the specified XML element with the specified attribute name, ignoring any namespace prefix of the attribute.- Parameters:
element
- the target element.attrName
- the name of the target attribute, without a namespace prefix.- Returns:
- the value of the attribute of
element
that has the nameattrName
, ignoring any namespace prefix of the attribute, ornull
if the element does not have such an attribute.
-
elementFacade
Returns a new instance of an element facade that is associated with the owner document of the specified XML element.- Parameters:
element
- the element with whose owner document the new element facade will be associated.- Returns:
- a element facade that is associated with the owner document of
element
. - Throws:
IllegalStateException
- ifelement
has no owner document.
-
parent
Returns the parent of the specified element, which is assumed to be an element.- Parameters:
element
- the target element.- Returns:
- the parent of
element
, ornull
if the element does not have a parent.
-
countChildren
Returns a count of the child nodes of the specified XML element that are JSON-XML elements. Any child node that is not a JSON-XML elements is omitted from the count.- Parameters:
element
- the target element.- Returns:
- a count of the JSON-XML elements that are children of
element
.
-
countDescendants
Returns a count of the descendant nodes of the specified XML element that are JSON-XML elements. Any descendant node that is not a JSON-XML element is omitted from the count.- Parameters:
element
- the target element.- Returns:
- a count of the JSON-XML elements that are descendants of
element
.
-
children
Returns a list of the child nodes of the specified XML element that are JSON-XML elements. Any child node that is not a JSON-XML element is omitted from the list.- Parameters:
element
- the target element.- Returns:
- a modifiable list of the JSON-XML elements that are children of
element
.
-
child
Returns the element at the specified index in the sequence of JSON-XML elements that are children of the specified XML element. Any child node of the specified element that is not a JSON-XML element is ignored.- Parameters:
element
- the target element.index
- the index of the desired element in the sequence of JSON-XML children ofelement
.- Returns:
- the element at
index
in the sequence of JSON-XML elements that are children ofelement
, ornull
ifindex
is out of bounds.
-
childIterator
Returns an iterator over the JSON-XML elements that are children of the specified XML element. The iterator ignores any child node of the specified element that is not a JSON-XML element.The returned iterator does not support the
remove
operation. The behaviour of an iterator is undefined if the list of children of the specified element is modified structurally while an iteration is in progress.- Parameters:
element
- the target element.- Returns:
- an iterator over the JSON-XML elements that are children of
element
.
-
clean
Removes all the descendant nodes of the specified XML element that are not JSON-XML elements.- Parameters:
element
- the target element.- Returns:
- a list of the non-JSON-XML descendants of
element
for which an error occurred when trying to remove them from their parent.
-
equals
Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other. If either of the elements has a name attribute, the attribute is ignored when comparing the elements.- Parameters:
element1
- the first element.element2
- the second element.- Returns:
true
ifelement1
andelement2
are deeply equal to each other or if both elements arenull
;false
otherwise.- Throws:
ElementKind.UnexpectedKindException
- if eitherelement1
orelement2
is not a JSON-XML element.
-
equals
public static boolean equals(IElementFacade elementFacade1, Element element1, IElementFacade elementFacade2, Element element2) Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other. If either of the elements has a name attribute, the attribute is ignored when comparing the elements.If an element facade is supplied for an element, it will be used to access the attributes of the element and its descendants. An instance of
SimpleElementFacade
may be used to apply a namespace prefix to the name of an attribute.- Parameters:
elementFacade1
- the facade through which the attributes of the first element and its descendants will be accessed. If it isnull
, attributes are accessed by callinggetAttribute(String)
on an element.element1
- the first element.elementFacade2
- the facade through which the attributes of the second element and its descendants will be accessed. If it isnull
, attributes are accessed by callinggetAttribute(String)
on an element.element2
- the second element.- Returns:
true
ifelement1
andelement2
are deeply equal to each other or if both elements arenull
;false
otherwise.- Throws:
ElementKind.UnexpectedKindException
- if eitherelement1
orelement2
is not a JSON-XML element.
-
equals
public static boolean equals(IElementFacade elementFacade1, Element element1, IElementFacade elementFacade2, Element element2, boolean includeNameAttr) Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other.If an element facade is supplied for an element, it will be used to access the attributes of the element and its descendants. An instance of
SimpleElementFacade
may be used to apply a namespace prefix to the name of an attribute.- Parameters:
elementFacade1
- the facade through which the attributes of the first element and its descendants will be accessed. If it isnull
, attributes are accessed by callinggetAttribute(String)
on an element.element1
- the first element.elementFacade2
- the facade through which the attributes of the second element and its descendants will be accessed. If it isnull
, attributes are accessed by callinggetAttribute(String)
on an element.element2
- the second element.includeNameAttr
- iftrue
, the name attributes ofelement1
andelement2
will be included in the comparison of the elements.- Returns:
true
ifelement1
andelement2
are deeply equal to each other or if both elements arenull
;false
otherwise.- Throws:
ElementKind.UnexpectedKindException
- if eitherelement1
orelement2
is not a JSON-XML element.
-
equalsIgnoreNS
Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other. When comparing the name and value attributes of JSON-XML elements, any namespace prefix of an attribute is ignored. If either of the elements has a name attribute, the attribute is ignored when comparing the elements.- Parameters:
element1
- the first element.element2
- the second element.- Returns:
true
ifelement1
andelement2
are deeply equal to each other or if both elements arenull
;false
otherwise.- Throws:
ElementKind.UnexpectedKindException
- if eitherelement1
orelement2
is not a JSON-XML element.
-
equalsIgnoreNS
Performs a deep comparison of the specified pair of JSON-XML elements and returnstrue
if the elements are equal to each other. When comparing the name and value attributes of JSON-XML elements, any namespace prefix of an attribute is ignored.- Parameters:
element1
- the first element.element2
- the second element.includeNameAttr
- iftrue
, the name attributes ofelement1
andelement2
will be included in the comparison of the elements.- Returns:
true
ifelement1
andelement2
are deeply equal to each other or if both elements arenull
;false
otherwise.- Throws:
ElementKind.UnexpectedKindException
- if eitherelement1
orelement2
is not a JSON-XML element.
-
hashCode
Returns a deep hash code for the specified XML element.- Parameters:
element
- the target element.- Returns:
- a hash code for
element
.
-
hashCode
Returns a deep hash code for the specified XML element. If an element facade is supplied, it will be used to access the attributes of JSON-XML elements. An instance ofSimpleElementFacade
may be used to apply a namespace prefix to the name of the attribute.- Parameters:
elementFacade
- the facade through which the attributes of JSON-XML elements will be accessed. If it isnull
, attributes are accessed by callinggetAttribute(String)
on an element.element
- the target element.- Returns:
- a hash code for
element
.
-
hashCode
Returns a deep hash code for the specified XML element. If an element facade is supplied, it will be used to access the attributes of JSON-XML elements. An instance ofSimpleElementFacade
may be used to apply a namespace prefix to the name of the attribute.- Parameters:
elementFacade
- the facade through which the attributes of JSON-XML elements will be accessed. If it isnull
, attributes are accessed by callinggetAttribute(String)
on an element.element
- the target element.includeNameAttr
- iftrue
, the name attribute ofelement
will be included in the calculation of the hash code.- Returns:
- a hash code for
element
.
-
copy
public static Element copy(IElementFacade sourceElementFacade, IElementFacade destElementFacade, Element element) Creates and returns a deep copy of the specified JSON-XML element. The specified facades are used to create destination elements and to access attributes of source and destination elements.- Parameters:
sourceElementFacade
- the facade through which the attributes of source JSON-XML elements will be accessed. If it isnull
, attributes are accessed by callinggetAttribute(String)
on a source element.destElementFacade
- the facade through which destination JSON-XML elements will be created and the attributes of destination elements will be accessed. It must not benull
.element
- the element for which a copy is desired.- Returns:
- a deep copy of
element
, if it is a JSON-XML element; otherwise,null
.
-
writeJson
public static void writeJson(Element element, boolean printableAsciiOnly, Writer writer) throws IOException Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and writes the text to the specified character stream.- Parameters:
element
- the root of the tree of JSON-XML elements that will be written as JSON text.printableAsciiOnly
- iftrue
, JSON string values and the names of the members of JSON objects will be escaped so that they contain only printable characters from the US-ASCII character encoding (ie, characters in the range U+0020 to U+007E inclusive).writer
- the character stream to which the JSON text will be written.- Throws:
IOException
- if an error occurs when writing the JSON text to the character stream.
-
writeJson
public static void writeJson(IElementFacade elementFacade, Element element, boolean printableAsciiOnly, Writer writer) throws IOException Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and writes the text to the specified character stream.If an element facade is supplied, it is used to access the attributes of the JSON-XML elements. An instance of
SimpleElementFacade
may be used to apply a namespace prefix to the names of attributes.- Parameters:
elementFacade
- the facade through which the attributes of JSON-XML elements will be accessed. If it isnull
, the attributes will be accessed directly through the elements.element
- the root of the tree of JSON-XML elements that will be written as JSON text.printableAsciiOnly
- iftrue
, JSON string values and the names of the members of JSON objects will be escaped so that they contain only printable characters from the US-ASCII character encoding (ie, characters in the range U+0020 to U+007E inclusive).writer
- the character stream to which the JSON text will be written.- Throws:
IOException
- if an error occurs when writing the JSON text to the character stream.
-
toJsonText
Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and returns the text.- Parameters:
element
- the root of the tree of JSON-XML elements that will be converted to JSON text.printableAsciiOnly
- iftrue
, JSON string values and the names of the members of JSON objects will be escaped so that they contain only printable characters from the US-ASCII character encoding (ie, characters in the range U+0020 to U+007E inclusive).- Returns:
- the result of converting the tree of JSON-XML elements whose root is
element
to JSON text.
-
toJsonText
public static String toJsonText(IElementFacade elementFacade, Element element, boolean printableAsciiOnly) Converts the tree of JSON-XML elements whose root is the specified XML element to JSON text and returns the text.If an element facade is supplied, it is used to access the attributes of the JSON-XML elements. An instance of
SimpleElementFacade
may be used to apply a namespace prefix to the names of attributes.- Parameters:
elementFacade
- the facade through which the attributes of JSON-XML elements will be accessed. If it isnull
, the attributes will be accessed through the elements.element
- the root of the tree of JSON-XML elements that will be converted to JSON text.printableAsciiOnly
- iftrue
, JSON string values and the names of the members of JSON objects will be escaped so that they contain only printable characters from the US-ASCII character encoding (ie, characters in the range U+0020 to U+007E inclusive).- Returns:
- the result of converting the tree of JSON-XML elements whose root is
element
to JSON text.
-
writeXml
public static void writeXml(Element element, int indent, int indentIncrement, Writer writer) throws IOException Generates a textual representation of the tree of JSON-XML elements whose root is the specified XML element and writes the text to the specified character stream.- Parameters:
element
- the root of the tree of JSON-XML elements for which a textual representation will be generated.indent
- the number of spaces by which the element at the root of XML tree will be indented.indentIncrement
- the number of spaces by which each additional level of the XML tree will be indented.writer
- the character stream to which the text will be written.- Throws:
IOException
- if an error occurs when writing the text to the character stream.
-
toXmlText
Generates a textual representation of the tree of JSON-XML elements whose root is the specified XML element and returns the text.- Parameters:
element
- the root of the tree of JSON-XML elements for which a textual representation will be generated.indent
- the number of spaces by which the element at the root of XML tree will be indented.indentIncrement
- the number of spaces by which each additional level of the XML tree below the root will be indented.- Returns:
- a textual representation of the tree of JSON-XML elements whose root is
element
.
-