Class Xml

java.lang.Object
org.omnifaces.util.Xml

public final class Xml extends Object

Collection of utility methods for the JAXP API in general.

This class is not listed in showcase! Should I use it?

This class is indeed intented for internal usage only. We won't add methods here on user request. We only add methods here once we encounter non-DRY code in OmniFaces codebase. The methods may be renamed/changed without notice.

We don't stop you from using it if you found it in the Javadoc and you think you find it useful, but you have to accept the risk that the method signatures can be changed without notice. This utility class exists because OmniFaces intends to be free of 3rd party dependencies.

Since:
2.1
Author:
Bauke Scholtz
  • Method Details

    • createDocument

      public static Document createDocument(List<URL> urls) throws IOException, SAXException
      Creates a single XML Document based on given URLs representing XML documents. All those XML documents are merged into a single root element named root.
      Parameters:
      urls - The URLs representing XML documents.
      Returns:
      A single XML document containing all given XML documents.
      Throws:
      IOException - When an I/O error occurs.
      SAXException - When a XML parsing error occurs.
    • createDocumentBuilder

      public static DocumentBuilder createDocumentBuilder()
      Creates an instance of DocumentBuilder which doesn't validate, nor is namespace aware nor expands entity references and disables external entity processing (to keep it as lenient and secure as possible).
      Returns:
      A lenient instance of DocumentBuilder.
    • parseAndAppendChildren

      public static void parseAndAppendChildren(DocumentBuilder builder, Document document, List<URL> urls) throws IOException, SAXException
      Parse the given URLs as a document using the given builder and then append all its child nodes to the given document.
      Parameters:
      builder - The document builder.
      document - The document.
      urls - The URLs representing XML documents.
      Throws:
      IOException - When an I/O error occurs.
      SAXException - When a XML parsing error occurs.
    • getNodeList

      public static NodeList getNodeList(Node node, XPath xpath, String expression) throws XPathExpressionException
      Convenience method to return a node list matching given XPath expression.
      Parameters:
      node - The node to return node list matching given XPath expression for.
      xpath - The XPath instance.
      expression - The XPath expression to match node list.
      Returns:
      A node list matching given XPath expression
      Throws:
      XPathExpressionException - When the XPath expression contains a syntax error.
    • getTextContent

      public static String getTextContent(Node node)
      Convenience method to return trimmed text content of given node. This uses getFirstChild().getNodeValue() instead of getTextContent() to workaround some buggy JAXP implementations.
      Parameters:
      node - The node to return text content for.
      Returns:
      Trimmed text content of given node.
    • getNodeTextContents

      public static List<String> getNodeTextContents(URL url, String expression)
      Convenience method to return a list of node text contents for given URL representing XML document matching given XPath expression.
      Parameters:
      url - The URL representing XML document.
      expression - The XPath expression to match node list whose text content has to be collected.
      Returns:
      A list of node text contents.
      Since:
      2.6.3