Class X2JUtils

java.lang.Object
com.x2j.converter.utils.X2JUtils

public class X2JUtils extends Object
This is a utility class which implements some common XML/String utilities functionalities that are used across the XML-To-JSON Converter project.
  • Constructor Details

    • X2JUtils

      public X2JUtils()
  • Method Details

    • isVoid

      public static boolean isVoid(Object obj)
      Null check for objects.
      Null or empty check for String.
      Parameters:
      obj - Any object
      Returns:
      true/false
    • getXmlFromFile

      public static Document getXmlFromFile(File file) throws X2JException
      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

      public static org.json.JSONObject getJsonFromFile(File file) throws X2JException
      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

      public static org.json.JSONObject getJsonFromString(String jsonString) throws X2JException
      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

      public static Document getXmlFromString(String inputXml) throws X2JException
      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

      public static NodeList getNodesFromXPath(Element element, String xpath) throws X2JException
      Returns the list of nodes in the XML element as per the path specified in the XPath.
      Parameters:
      element - the XML element
      xpath - the xpath
      Returns:
      the list of nodes
      Throws:
      X2JException - if the xpath is not a valid XPATH expression
    • getXpathAttribute

      public static String getXpathAttribute(Element element, String xpath) throws X2JException
      Returns the value of an attribute in the XML element as per the path specified in the XPath.
      Parameters:
      element - the XML element
      xpath - the xpath
      Returns:
      the value of the attribute
      Throws:
      X2JException - if the xpath is not a valid XPATH expression
    • getXpathElement

      public static Element getXpathElement(Element element, String xpath) throws X2JException
      Returns the element in the XML element as per the path specified in the XPath.
      Parameters:
      element - the XML element
      xpath - the xpath
      Returns:
      the element
      Throws:
      X2JException - if the xpath is not a valid XPATH expression
    • getString

      public static String getString(Document document) throws X2JException
      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

      public static String getString(Element element) throws X2JException
      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

      public static void writeJsonToFile(org.json.JSONObject json, File file) throws X2JException
      Writes the JSON object to a file.
      Parameters:
      json - the JSON Object
      file - the target file
      Throws:
      X2JException - if there is any error during the file writing process
    • encodeText

      public static String encodeText(String text)
      Encodes the special characters in the given text.
      • < : &lt;
      • > : &gt;
      • ' : &apos;
      • " : &quot;
      • & : &amp;
      Parameters:
      text - any input String
      Returns:
      the encoded string
    • isValidXPath

      public static boolean isValidXPath(String expression)
      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 string
      rootElement - the root element in the input XML
      Returns:
      the value after processing the XPath
      Throws:
      X2JException - X2JException