public class XmlUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
XmlUtil.VALIDATION_TYPE
Document validation types.
|
Modifier and Type | Field and Description |
---|---|
static char[] |
AMP |
static char[] |
APOS |
static DocumentBuilder |
documentBuilder |
static char[] |
GT |
static char[] |
LT |
static char[] |
QUOT |
Constructor and Description |
---|
XmlUtil() |
Modifier and Type | Method and Description |
---|---|
static Element |
createElement(String localPart)
Create an element with the specified name.
|
static Element |
createElementNS(String namespace,
String localPart)
Create an element with the specified name and namespace.
|
static void |
encodeAttributeValue(char[] characters,
int offset,
int length,
Writer writer) |
static void |
encodeTextValue(char[] characters,
int offset,
int length,
Writer writer) |
static Node |
getNode(Node node,
String xpath)
Get the W3C Node instance associated with the XPath selection supplied.
|
static NodeList |
getNodeList(Node node,
String xpath)
Get the W3C NodeList instance associated with the XPath selection
supplied.
|
static String |
getString(Node node,
String xpath)
Get the String data associated with the XPath selection supplied.
|
static String |
indent(String xml,
int indent)
Indent the supplied XML string by the number of spaces specified in the
'indent' param.
|
static boolean |
isTextNode(Node node)
Is the supplied W3C DOM Node a text node.
|
static boolean |
isXMLReservedNamespace(String namespace) |
static Document |
parseStream(InputStream stream)
Basic DOM namespace aware parse.
|
static Document |
parseStream(InputStream stream,
EntityResolver entityResolver,
XmlUtil.VALIDATION_TYPE validation,
boolean expandEntityRefs)
Parse the XML stream and return the associated W3C Document object.
|
static Document |
parseStream(InputStream stream,
XmlUtil.VALIDATION_TYPE validation,
boolean expandEntityRefs)
Parse the XML stream and return the associated W3C Document object.
|
static Document |
parseStream(Reader stream)
Basic DOM namespace aware parse.
|
static Document |
parseStream(Reader stream,
EntityResolver entityResolver,
XmlUtil.VALIDATION_TYPE validation,
boolean expandEntityRefs)
Parse the XML stream and return the associated W3C Document object.
|
static Document |
parseStream(Reader stream,
ErrorHandler errorHandler)
Basic DOM namespace aware parse.
|
static void |
removeEntities(Reader reader,
Writer writer)
Remove all entities from the supplied
Reader stream
replacing them with their actual character values. |
static String |
removeEntities(String string)
Remove all entities from the supplied
String stream
replacing them with there actual character values. |
static void |
rewriteEntities(Reader reader,
Writer writer)
Rewrite all entities from the supplied
Reader stream
replacing them with their character reference equivalents. |
static String |
serialize(Node node,
boolean format)
Serialise the supplied W3C DOM subtree.
|
static void |
serialize(Node node,
boolean format,
Writer writer)
Serialise the supplied W3C DOM subtree.
|
static String |
serialize(NodeList nodeList)
Serialise the supplied W3C DOM subtree.
|
static String |
serialize(NodeList nodeList,
boolean format)
Serialise the supplied W3C DOM subtree.
|
static void |
serialize(NodeList nodeList,
boolean format,
Writer writer)
Serialise the supplied W3C DOM subtree.
|
public static final DocumentBuilder documentBuilder
public static final char[] LT
public static final char[] GT
public static final char[] AMP
public static final char[] QUOT
public static final char[] APOS
public static boolean isXMLReservedNamespace(String namespace)
public static void removeEntities(Reader reader, Writer writer) throws IOException
Reader
stream
replacing them with their actual character values. Both the read and
write streams are returned unclosed.reader
- The read stream.writer
- The write stream.IOException
public static String removeEntities(String string)
String
stream
replacing them with there actual character values.string
- The string on which the operation is to be carried out.public static void rewriteEntities(Reader reader, Writer writer) throws IOException
Reader
stream
replacing them with their character reference equivalents. Example:
  is rewriten as   Both the read and
write streams are returned unclosed.reader
- The read stream.writer
- The write stream.IOException
public static Document parseStream(InputStream stream, XmlUtil.VALIDATION_TYPE validation, boolean expandEntityRefs) throws SAXException, IOException
stream
- The stream to be parsed.validation
- Validation type to be carried out on the document.expandEntityRefs
- Expand entity References as per
DocumentBuilderFactory.setExpandEntityReferences(boolean)
.SAXException
IOException
public static Document parseStream(InputStream stream, EntityResolver entityResolver, XmlUtil.VALIDATION_TYPE validation, boolean expandEntityRefs) throws SAXException, IOException
stream
- The stream to be parsed.entityResolver
- Entity resolver to be used during the parse.validation
- Validation type to be carried out on the document.expandEntityRefs
- Expand entity References as per
DocumentBuilderFactory.setExpandEntityReferences(boolean)
.SAXException
IOException
public static Document parseStream(Reader stream, EntityResolver entityResolver, XmlUtil.VALIDATION_TYPE validation, boolean expandEntityRefs) throws SAXException, IOException
stream
- The stream to be parsed.entityResolver
- Entity resolver to be used during the parse.validation
- Validation type to be carried out on the document.expandEntityRefs
- Expand entity References as per
DocumentBuilderFactory.setExpandEntityReferences(boolean)
.SAXException
IOException
public static Document parseStream(InputStream stream) throws ParserConfigurationException, IOException, SAXException
stream
- Document stream.ParserConfigurationException
IOException
SAXException
public static Document parseStream(Reader stream) throws ParserConfigurationException, IOException, SAXException
stream
- Document stream.ParserConfigurationException
IOException
SAXException
public static Document parseStream(Reader stream, ErrorHandler errorHandler) throws ParserConfigurationException, IOException, SAXException
stream
- Document stream.errorHandler
- ErrorHandler
to be set on the DocumentBuilder.
This can be used to controll error reporting. If null
the default error handler will be used.ParserConfigurationException
IOException
SAXException
public static NodeList getNodeList(Node node, String xpath)
node
- The document node to be searched.xpath
- The XPath String to be used in the selection.public static Node getNode(Node node, String xpath)
node
- The document node to be searched.xpath
- The XPath String to be used in the selection.public static String getString(Node node, String xpath)
node
- The node to be searched.xpath
- The XPath String to be used in the selection.public static Element createElement(String localPart)
localPart
- The localPart name.public static Element createElementNS(String namespace, String localPart)
namespace
- The namespace.localPart
- The localPart name.public static String serialize(NodeList nodeList) throws DOMException
nodeList
- The DOM subtree as a NodeList.DOMException
- Unable to serialise the DOM.public static String serialize(Node node, boolean format) throws DOMException
node
- The DOM node to be serialized.format
- Format the output.DOMException
- Unable to serialise the DOM.public static void serialize(Node node, boolean format, Writer writer) throws DOMException
node
- The DOM node to be serialized.format
- Format the output.writer
- The target writer for serialization.DOMException
- Unable to serialise the DOM.public static String serialize(NodeList nodeList, boolean format) throws DOMException
nodeList
- The DOM subtree as a NodeList.format
- Format the output.DOMException
- Unable to serialise the DOM.public static void serialize(NodeList nodeList, boolean format, Writer writer) throws DOMException
nodeList
- The DOM subtree as a NodeList.format
- Format the output.writer
- The target writer for serialization.DOMException
- Unable to serialise the DOM.public static String indent(String xml, int indent)
xml
- The XML to indent.indent
- The number of spaces to insert as the indent.public static boolean isTextNode(Node node)
node
- The node to be tested.public static void encodeTextValue(char[] characters, int offset, int length, Writer writer) throws IOException
IOException
public static void encodeAttributeValue(char[] characters, int offset, int length, Writer writer) throws IOException
IOException
Copyright © 2020. All rights reserved.