Package org.omnifaces.util
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.
- Since:
- 2.1
- Author:
- Bauke Scholtz
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Document
createDocument(List<URL> urls)
Creates a single XMLDocument
based on given URLs representing XML documents.static DocumentBuilder
createDocumentBuilder()
Creates an instance ofDocumentBuilder
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).static NodeList
getNodeList(Node node, XPath xpath, String expression)
Convenience method to return a node list matching given XPath expression.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.static String
getTextContent(Node node)
Convenience method to return trimmed text content of given node.static void
parseAndAppendChildren(DocumentBuilder builder, Document document, List<URL> urls)
Parse the given URLs as a document using the given builder and then append all its child nodes to the given document.
-
-
-
Method Detail
-
createDocument
public static Document createDocument(List<URL> urls) throws IOException, SAXException
Creates a single XMLDocument
based on given URLs representing XML documents. All those XML documents are merged into a single root element namedroot
.- 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 ofDocumentBuilder
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 usesgetFirstChild().getNodeValue()
instead ofgetTextContent()
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
-
-