Class XmlUtils

java.lang.Object
org.openehealth.ipf.commons.xml.XmlUtils

public abstract class XmlUtils extends Object
Various XML utilities.
Author:
Dmytro Rud
  • Method Details

    • source

      public static Source source(String s)
      Creates an XML Source from the given XML String.
      Parameters:
      s - XML String.
      Returns:
      XML Source.
    • rootElementName

      public static String rootElementName(String s)
      Returns local name of the root element of the XML document represented by the given string, or null, when the given string does not contain valid XML.
      Parameters:
      s - XML string.
      Returns:
      root element local name, or null when it could not be determined.
    • marshal

      public static String marshal(jakarta.xml.bind.JAXBContext jaxbContext, Object object, boolean prettyPrint)
      Marshals a JAXB object into its XML string representation, without XML declaration.
      Parameters:
      jaxbContext - JAXB context corresponding to the object's type
      object - the JAXB object to be marshalled
      prettyPrint - whether the XML shall be pretty printed
      Returns:
      XML string representation of the given JAXB object
      Throws:
      RuntimeException - wrapping the JAXBException if marshalling fails
      Since:
      6.1
    • marshal

      public static byte[] marshal(Node node) throws TransformerException
      Marshals a DOM node into indented XML bytes, without XML declaration.
      Parameters:
      node - DOM node to marshal
      Returns:
      XML bytes
      Throws:
      TransformerException - if serialization fails
      Since:
      6.1
    • unmarshal

      public static Object unmarshal(jakarta.xml.bind.JAXBContext jaxbContext, String xml) throws jakarta.xml.bind.JAXBException
      Unmarshals an untrusted XML string with JAXB. The document is parsed by a reader from XmlSecurity.newXmlReader(), so any DOCTYPE declaration is rejected.
      Parameters:
      jaxbContext - JAXB context corresponding to the expected content
      xml - XML string
      Returns:
      the unmarshalled object, which may be a JAXBElement for types without root element declaration
      Throws:
      jakarta.xml.bind.JAXBException - if the document cannot be parsed or unmarshalled, including when it contains a DOCTYPE declaration
      Since:
      6.1
    • unmarshal

      public static <T> T unmarshal(jakarta.xml.bind.JAXBContext jaxbContext, String xml, Class<T> type) throws jakarta.xml.bind.JAXBException
      Unmarshals an untrusted XML string with JAXB into an object of the given type, see unmarshal(JAXBContext, String).
      Type Parameters:
      T - expected type of the unmarshalled object
      Parameters:
      jaxbContext - JAXB context corresponding to the expected content
      xml - XML string
      type - expected type of the unmarshalled object
      Returns:
      the unmarshalled object
      Throws:
      jakarta.xml.bind.JAXBException - if the document cannot be parsed or unmarshalled
      ClassCastException - if the unmarshalled object is not of the expected type
      Since:
      6.1
    • renderJaxb

      @Deprecated(since="6.1", forRemoval=true) public static String renderJaxb(jakarta.xml.bind.JAXBContext jaxbContext, Object object, Boolean prettyPrint)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • serialize

      @Deprecated(since="6.1", forRemoval=true) public static byte[] serialize(Node inputNode) throws Exception
      Deprecated, for removal: This API element is subject to removal in a future version.
      Throws:
      Exception