Package com.x2j.converter.utils
Class X2JUtils
java.lang.Object
com.x2j.converter.utils.X2JUtils
This is a utility class which implements some common XML/String utilities
functionalities that are used across the XML-To-JSON Converter project.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
encodeText
(String text) Encodes the special characters in the given text.static org.json.JSONObject
getJsonFromFile
(File file) Reads and parses the file and returns the JSON object.static org.json.JSONObject
getJsonFromString
(String jsonString) Reads and parses the string and returns the JSON object.static NodeList
getNodesFromXPath
(Element element, String xpath) Returns the list of nodes in the XML element as per the path specified in the XPath.static String
Parses the XML document and returns the same as a String object.static String
Parses the XML element and returns the same as a String object.static String
getStringValueForHandlers
(String value, Element rootElement) Returns the string value as per specified schema for the String handlers.static Document
getXmlFromFile
(File file) Reads and parses the file and returns the XML document.static Document
getXmlFromString
(String inputXml) Reads and parses the string and returns the XML document.static String
getXpathAttribute
(Element element, String xpath) Returns the value of an attribute in the XML element as per the path specified in the XPath.static Element
getXpathElement
(Element element, String xpath) Returns the element in the XML element as per the path specified in the XPath.static boolean
isValidXPath
(String expression) Verifies whether the given expression is a valid XPath or not.static boolean
Null check for objects.static void
writeJsonToFile
(org.json.JSONObject json, File file) Writes the JSON object to a file.
-
Constructor Details
-
X2JUtils
public X2JUtils()
-
-
Method Details
-
isVoid
Null check for objects.
Null or empty check for String.- Parameters:
obj
- Any object- Returns:
- true/false
-
getXmlFromFile
Reads and parses the file and returns the XML document.- Parameters:
file
- the input file- Returns:
- XML Document
- Throws:
X2JException
- if the file is not readable or the content is not a valid XML.
-
getJsonFromFile
Reads and parses the file and returns the JSON object.- Parameters:
file
- the input file- Returns:
- JSON Object
- Throws:
X2JException
- if the file is not readable or the content is not a valid JSON.
-
getJsonFromString
Reads and parses the string and returns the JSON object.- Parameters:
jsonString
- the JSON string- Returns:
- JSON Object
- Throws:
X2JException
- if the String is not a valid JSON.
-
getXmlFromString
Reads and parses the string and returns the XML document.- Parameters:
inputXml
- the input XML- Returns:
- XML Document
- Throws:
X2JException
- if the String is not a valid XML.
-
getNodesFromXPath
Returns the list of nodes in the XML element as per the path specified in the XPath.- Parameters:
element
- the XML elementxpath
- the xpath- Returns:
- the list of nodes
- Throws:
X2JException
- if the xpath is not a valid XPATH expression
-
getXpathAttribute
Returns the value of an attribute in the XML element as per the path specified in the XPath.- Parameters:
element
- the XML elementxpath
- the xpath- Returns:
- the value of the attribute
- Throws:
X2JException
- if the xpath is not a valid XPATH expression
-
getXpathElement
Returns the element in the XML element as per the path specified in the XPath.- Parameters:
element
- the XML elementxpath
- the xpath- Returns:
- the element
- Throws:
X2JException
- if the xpath is not a valid XPATH expression
-
getString
Parses the XML document and returns the same as a String object.- Parameters:
document
- the XML document- Returns:
- the string equivalent
- Throws:
X2JException
- if the argument is null or there is an error while transforming XML to String
-
getString
Parses the XML element and returns the same as a String object.- Parameters:
element
- the XML element- Returns:
- the string equivalent
- Throws:
X2JException
- if the argument is null or there is an error while transforming XML to String
-
writeJsonToFile
Writes the JSON object to a file.- Parameters:
json
- the JSON Objectfile
- the target file- Throws:
X2JException
- if there is any error during the file writing process
-
encodeText
Encodes the special characters in the given text.
- < : <
- > : >
- ' : '
- " : "
- & : &
- Parameters:
text
- any input String- Returns:
- the encoded string
-
isValidXPath
Verifies whether the given expression is a valid XPath or not.
In the context of this application, a simple string is not a valid XPath expression.- Parameters:
expression
- the XPATH expression- Returns:
- true, if the expression is valid a XPath, otherwise false
-
getStringValueForHandlers
public static String getStringValueForHandlers(String value, Element rootElement) throws X2JException Returns the string value as per specified schema for the String handlers.- Parameters:
value
- the XPath stringrootElement
- the root element in the input XML- Returns:
- the value after processing the XPath
- Throws:
X2JException
- X2JException
-