Class XmlUtils

java.lang.Object
com.aeontronix.commons.xml.XmlUtils

public class XmlUtils extends Object
Various XML related utility functions
  • Field Details

  • Constructor Details

    • XmlUtils

      public XmlUtils()
  • Method Details

    • createDocument

      public static Document createDocument()
    • createDocument

      public static Document createDocument(boolean namespaceAware)
    • createSchema

      public static Schema createSchema(Reader schema) throws SAXException
      Throws:
      SAXException
    • createSchema

      public static Schema createSchema(Source... sources) throws SAXException
      Throws:
      SAXException
    • createSchema

      public static Schema createSchema(LSResourceResolver rsResolver, Source... sources) throws SAXException
      Throws:
      SAXException
    • parse

      public static Document parse(File file) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(File file, boolean namespaceAware) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(InputStream stream) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(InputStream stream, boolean namespaceAware) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(Reader reader) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(Reader reader, boolean namespaceAware) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(byte[] data) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(byte[] data, boolean namespaceAware) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(String uri) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(String uri, boolean namespaceAware) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(URI uri) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • parse

      public static Document parse(URI uri, boolean namespaceAware) throws IOException, SAXException
      Throws:
      IOException
      SAXException
    • getChild

      public static Node getChild(Node parent, String namespaceUri, String nodeName)
    • getChild

      public static Node getChild(Node parent, String nodeName)
    • getChildElement

      public static Element getChildElement(Node parent, String elementName, boolean create) throws DOMException
      Search for a child element, with the option to automatically create it if the create flag is true.
      Parameters:
      parent - Parent node.
      elementName - Element name.
      create - Flag that indicates if the element should be automatically created should it not exist.
      Returns:
      Child element or null if it doesn't exist and create is false.
      Throws:
      DOMException - If there is an node with the specified name, that isn't an element.
    • getChildElement

      public static Element getChildElement(Node parent, String namespace, String elementName, boolean create) throws DOMException
      Search for a child element, with the option to automatically create it if the create flag is true.
      Parameters:
      parent - Parent node.
      namespace - Namespace (optional).
      elementName - Element name.
      create - Flag that indicates if the element should be automatically created should it not exist.
      Returns:
      Child element or null if it doesn't exist and create is false.
      Throws:
      DOMException - If there is an node with the specified name, that isn't an element.
    • serialize

      public static void serialize(Node node, Writer writer)
    • serialize

      public static void serialize(Node node, OutputStream stream)
    • serialize

      public static void serialize(Node node, Writer writer, boolean xmlDeclaration, boolean prettyPrint)
    • serialize

      public static void serialize(Node node, OutputStream writer, boolean xmlDeclaration, boolean prettyPrint)
    • toString

      public static String toString(Node node)
    • toString

      public static String toString(Node node, boolean xmlDeclaration, boolean prettyPrint)
    • getDomImplRegistry

      public static DOMImplementationRegistry getDomImplRegistry()
    • getDocumentBuilderFactory

      public static DocumentBuilderFactory getDocumentBuilderFactory()
    • getDocumentBuilderFactory

      public static DocumentBuilderFactory getDocumentBuilderFactory(boolean namespaceAware)
    • getDocumentBuilderFactory

      public static DocumentBuilderFactory getDocumentBuilderFactory(boolean namespaceAware, boolean disableDtdXxe)
    • getDocumentBuilder

      public static DocumentBuilder getDocumentBuilder()
    • getDocumentBuilder

      public static DocumentBuilder getDocumentBuilder(boolean namespaceAware)
    • getXPath

      public static String getXPath(Node node)
      Return an xpath expression that matches the provided node.
      Parameters:
      node - Node.
      Returns:
      resolved expression.
    • getSiblingNumber

      public static int getSiblingNumber(Node node)
      Calculate how many previous siblings of the same node type exists, and return that value plus one.
      Parameters:
      node - Node
      Returns:
      Sibling number.
    • getChildNodes

      public static <X extends Node> List<X> getChildNodes(Node node, Class<X> type)
    • toList

      public static List<Node> toList(NodeList nodelist)
      Convert a NodeList to a List
      Parameters:
      nodelist - Node list
      Returns:
      List of nodes
    • toList

      public static List<Attr> toList(NamedNodeMap attrList)
      Convert a NamedNodeMap contains attribute nodes Attr
      Parameters:
      attrList - NamedNodeMap containing Attr objects exclusively
      Returns:
      List of Attr.
    • toClonedList

      public static List<Node> toClonedList(NodeList nodelist, boolean deep)
      Clone all the nodes contains in the provided node list, and return them in a List.
      Parameters:
      nodelist - Node list
      deep - If true, recursively clone the subtree under each node; if false, clone only the node itself (and its attributes, if it is an Element).
      Returns:
      List of cloned nodes
    • toElementList

      public static List<Element> toElementList(NodeList nodelist)
      Clone all the nodes contains in the provided node list, assuming they are of type Element, and return them in a List.
      Parameters:
      nodelist - Node list
      Returns:
      List of cloned nodes
    • toClonedElementList

      public static List<Element> toClonedElementList(NodeList nodelist, boolean deep)
      Clone all the nodes contains in the provided node list, assuming they are of type Element, and return them in a List.
      Parameters:
      nodelist - Node list
      deep - If true, recursively clone the subtree under each node; if false, clone only the node itself (and its attributes, if it is an Element).
      Returns:
      List of cloned nodes
    • createElement

      public static Element createElement(String name, Node parent, Object... attrs)
    • createNSElement

      public static Element createNSElement(String name, Node parent, Object... attrs)
    • createNSElement

      public static Element createNSElement(String name, String namespace, String prefix, Node parent, Object... attrs)
    • setAttributes

      public static void setAttributes(Element element, Object... attrs)
    • createTreeWalker

      public static org.w3c.dom.traversal.TreeWalker createTreeWalker(Node node, int whatToShow)
      As calling createTreeWalker(org.w3c.dom.Node, int, org.w3c.dom.traversal.NodeFilter, boolean) with nodeFilter=null, entityReferenceExpansion=true }
      Parameters:
      node - The node which will serve as the root for the TreeWalker. The whatToShow flags and the NodeFilter are not considered when setting this value; any node type will be accepted as the root. The currentNode of the TreeWalker is initialized to this node, whether or not it is visible. The root functions as a stopping point for traversal methods that look upward in the document structure, such as parentNode and nextNode. The root must not be null.
      whatToShow - This flag specifies which node types may appear in the logical view of the tree presented by the TreeWalker. See the description of NodeFilter for the set of possible SHOW_ values.These flags can be combined using OR.
      Returns:
      TreeWalker
    • createTreeWalker

      public static org.w3c.dom.traversal.TreeWalker createTreeWalker(Node node, int whatToShow, org.w3c.dom.traversal.NodeFilter nodeFilter, boolean entityReferenceExpansion)
    • createElementTreeWalker

      public static org.w3c.dom.traversal.TreeWalker createElementTreeWalker(Node node)
    • createElementTreeWalker

      public static org.w3c.dom.traversal.TreeWalker createElementTreeWalker(Node node, boolean entityReferenceExpansion)
    • createElementTreeWalker

      public static org.w3c.dom.traversal.TreeWalker createElementTreeWalker(Node node, org.w3c.dom.traversal.NodeFilter nodeFilter, boolean entityReferenceExpansion)
    • convertDate

      public static XMLGregorianCalendar convertDate(Date date)
    • convertDate

      public static Date convertDate(XMLGregorianCalendar date)