Class XmlUtil

java.lang.Object
com.mirth.connect.userutil.XmlUtil

public class XmlUtil extends Object
Provides XML utility methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    decode(String entity)
    Converts an XML/HTML entity reference into a string with the literal character.
    static String
    encode(char s)
    Encodes a character into the corresponding XML/HTML entity.
    static String
    encode(char[] text, int start, int length)
    Converts a character array, encoding characters into the corresponding XML/HTML entities as needed.
    static String
    Converts a string, encoding characters into the corresponding XML/HTML entities as needed.
    static String
    Formats an XML string with indented markup.
    static String
    toJson(String xmlString)
    Converts an XML string to JSON, while stripping bound namespace prefixes.
    static String
    toJson(String xmlString, boolean normalizeNamespaces)
    Converts an XML string to JSON.
    static String
    toJson(String xmlString, boolean autoArray, boolean autoPrimitive, boolean prettyPrint, boolean normalizeNamespaces)
    Converts an XML string to JSON.
    static String
    toJson(String xmlString, boolean autoArray, boolean autoPrimitive, boolean prettyPrint, boolean normalizeNamespaces, boolean alwaysArray, boolean alwaysExpandObjects)
    Converts an XML string to JSON.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • prettyPrint

      public static String prettyPrint(String input)
      Formats an XML string with indented markup.
      Parameters:
      input - The XML string to format.
      Returns:
      The formatted XML string.
    • decode

      public static String decode(String entity)
      Converts an XML/HTML entity reference into a string with the literal character.
      Parameters:
      entity - The XML/HTML entity to decode.
      Returns:
      A string containing the decoded character.
    • encode

      public static String encode(char s)
      Encodes a character into the corresponding XML/HTML entity.
      Parameters:
      s - The character to encode.
      Returns:
      The encoded XML/HTML entity.
    • encode

      public static String encode(String s)
      Converts a string, encoding characters into the corresponding XML/HTML entities as needed.
      Parameters:
      s - The string to encode.
      Returns:
      The encoded string with replaced XML/HTML entities.
    • encode

      public static String encode(char[] text, int start, int length)
      Converts a character array, encoding characters into the corresponding XML/HTML entities as needed.
      Parameters:
      text - The character array to encode.
      start - The index to start at in the character array.
      length - The maximum amount of characters to read from the array.
      Returns:
      The encoded string with replaced XML/HTML entities.
    • toJson

      public static String toJson(String xmlString) throws Exception
      Converts an XML string to JSON, while stripping bound namespace prefixes.
      Parameters:
      xmlString - The XML string to convert.
      Returns:
      The converted JSON string.
      Throws:
      Exception - If conversion failed.
    • toJson

      public static String toJson(String xmlString, boolean normalizeNamespaces) throws Exception
      Converts an XML string to JSON.
      Parameters:
      xmlString - The XML string to convert.
      normalizeNamespaces - Whether or not to normalize namespaces by stripping prefixes.
      Returns:
      The converted JSON string.
      Throws:
      Exception - If conversion failed.
    • toJson

      public static String toJson(String xmlString, boolean autoArray, boolean autoPrimitive, boolean prettyPrint, boolean normalizeNamespaces) throws Exception
      Converts an XML string to JSON.
      Parameters:
      xmlString - The XML string to convert.
      autoArray - If true, sibling nodes with the same tag name will be consolidated into a JSON array. If false, multiple properties with the same name will be present.
      autoPrimitive - If true, element text will be converted to JSON primitive values where applicable. If false, element text will always be converted to string values.
      prettyPrint - Whether or not to fully indent the JSON output.
      normalizeNamespaces - Whether or not to normalize namespaces by stripping prefixes.
      Returns:
      The converted JSON string.
      Throws:
      Exception - If conversion failed.
    • toJson

      public static String toJson(String xmlString, boolean autoArray, boolean autoPrimitive, boolean prettyPrint, boolean normalizeNamespaces, boolean alwaysArray, boolean alwaysExpandObjects) throws Exception
      Converts an XML string to JSON.
      Parameters:
      xmlString - The XML string to convert.
      autoArray - If true, sibling nodes with the same tag name will be consolidated into a JSON array. If false, multiple properties with the same name will be present.
      autoPrimitive - If true, element text will be converted to JSON primitive values where applicable. If false, element text will always be converted to string values.
      prettyPrint - Whether or not to fully indent the JSON output.
      normalizeNamespaces - Whether or not to normalize namespaces by stripping prefixes.
      alwaysArray - If true, all nodes except for the top-level object will be written into JSON arrays. Overrides the autoArray option.
      alwaysExpandObjects - If true, all values will be written in the expanded "$" syntax.
      Returns:
      The converted JSON string.
      Throws:
      Exception - If conversion failed.