Class XMLToJSONConverter

java.lang.Object
com.x2j.converter.XMLToJSONConverter

public class XMLToJSONConverter extends Object
The class XMLToJSONConverter is the starting point.
This exposes various methods for the XML to JSON conversion.
It accepts the input XML and the schema JSON as String or as a File object.
Before initiating the conversion logic, this class tries to figure out the JSON schema, if not explicitly passed as argument.
If the JSON schema is not found, the default conversion logic will be applied.
  • Constructor Details

    • XMLToJSONConverter

      public XMLToJSONConverter()
  • Method Details

    • convertToJson

      public org.json.JSONObject convertToJson(Document inDoc) throws X2JException
      Convert XML to JSON. The schema should be part of the XML element.
      If the schema definition is present as a child of the root element, under the tag JSONSchema, then that schema definition will be considered for conversion.
      Otherwise, the default conversion logic will be applied.
      Parameters:
      inDoc - the input XML document
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.
    • convertToJson

      public org.json.JSONObject convertToJson(Document inDoc, String jsonSchema) throws X2JException
      Convert XML to JSON. The schema is explicitly passed as a String argument.
      The default conversion logic will only get applied when the schema string is null or empty.
      Parameters:
      inDoc - the input XML document
      jsonSchema - the JSON schema object
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.
    • convertToJson

      public org.json.JSONObject convertToJson(Document inDoc, org.json.JSONObject jsonSchemaObj) throws X2JException
      Convert XML to JSON. The schema is explicitly passed as a JSONObject argument.
      The default conversion logic will only get applied when the schema is null.
      Parameters:
      inDoc - the input XML document
      jsonSchemaObj - the JSON schema object
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.
    • convertToJson

      public org.json.JSONObject convertToJson(File inputXmlFile) throws X2JException
      Convert XML to JSON. The schema should be part of the XML element.
      The input XML is stored in a file and the corresponding File object is passed as an argument to this method.
      If the schema definition is present as a child of the root element, under the tag JSONSchema, then that schema definition will be considered for conversion.
      Otherwise, the default conversion logic will be applied.
      Parameters:
      inputXmlFile - the input XML file
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.
    • convertToJson

      public org.json.JSONObject convertToJson(File inputXmlFile, String jsonSchema) throws X2JException
      Convert XML to JSON. The schema is explicitly passed as a String argument.
      The input XML is stored in a file and the corresponding File object is passed as an argument to this method.
      The default conversion logic will only get applied when the schema string is null or empty.
      Parameters:
      inputXmlFile - the input XML file
      jsonSchema - the JSON schema
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.
    • convertToJson

      public org.json.JSONObject convertToJson(File inputXmlFile, org.json.JSONObject jsonSchemaObj) throws X2JException
      Convert XML to JSON. The schema is explicitly passed as a JSONObject argument.
      The input XML is stored in a file and the corresponding File object is passed as an argument to this method.
      The default conversion logic will only get applied when the schema is null.
      Parameters:
      inputXmlFile - the input XML file
      jsonSchemaObj - the JSON schema
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.
    • convertToJson

      public org.json.JSONObject convertToJson(String inputXml) throws X2JException
      Convert XML to JSON. The schema should be part of the XML element.
      The input XML is a String and is passed as an argument to this method.
      If the schema definition is present as a child of the root element, under the tag JSONSchema, then that schema definition will be considered for conversion.
      Otherwise, the default conversion logic will be applied.
      Parameters:
      inputXml - the input XML String
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.
    • convertToJson

      public org.json.JSONObject convertToJson(String inputXml, String jsonSchema) throws X2JException
      Convert XML to JSON. The schema is explicitly passed as a String argument.
      The input XML is a String as well which is passed as an argument to this method.
      The default conversion logic will only get applied when the schema string is null or empty.
      Parameters:
      inputXml - the input XML String
      jsonSchema - the JSON schema
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.
    • convertToJson

      public org.json.JSONObject convertToJson(String inputXml, org.json.JSONObject jsonSchemaObj) throws X2JException
      Convert XML to JSON. The schema is explicitly passed as a JSONObject argument.
      The input XML is a String and is passed as an argument to this method.
      The default conversion logic will only get applied when the schema is null.
      Parameters:
      inputXml - the input XML String
      jsonSchemaObj - the JSON schema
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.
    • convertToJson

      public org.json.JSONObject convertToJson(File inputXmlFile, File jsonSchemaFile) throws X2JException
      Convert XML to JSON. The schema is explicitly passed as a JSONObject argument.
      The input XML and the schema JSON are stored in 2 separate files and the corresponding File objects are passed as arguments to this method.
      The default conversion logic will only get applied when the schema is null.
      Parameters:
      inputXmlFile - the input XML file
      jsonSchemaFile - the JSON schema file
      Returns:
      the converted JSON object
      Throws:
      X2JException - if there is any error during the conversion process.