public final class XMLUtil
extends java.lang.Object
| Modifier and Type | Method | Description |
|---|---|---|
static void |
appendChildElement(org.w3c.dom.Element parent,
org.w3c.dom.Element el,
java.lang.String[] order) |
Append a child element to the parent at the specified location.
|
static void |
copyDocument(org.w3c.dom.Element from,
org.w3c.dom.Element to,
java.lang.String newNamespace) |
Copy elements from one document to another attaching at the specified element
and translating the namespace.
|
static org.w3c.dom.Document |
createDocument(java.lang.String rootQName,
java.lang.String namespaceURI,
java.lang.String doctypePublicID,
java.lang.String doctypeSystemID) |
Creates an empty DOM document.
|
static org.xml.sax.XMLReader |
createXMLReader() |
Create a simple parser.
|
static org.xml.sax.XMLReader |
createXMLReader(boolean validate) |
Create a simple parser, possibly validating.
|
static org.xml.sax.XMLReader |
createXMLReader(boolean validate,
boolean namespaceAware) |
Creates a SAX parser.
|
static org.xml.sax.ErrorHandler |
defaultErrorHandler() |
Create an XML error handler that rethrows errors and fatal errors and logs warnings.
|
static org.w3c.dom.Element |
findElement(org.w3c.dom.Element parent,
java.lang.String name,
java.lang.String namespace) |
Search for an XML element in the direct children of parent only.
|
static java.util.List<org.w3c.dom.Element> |
findSubElements(org.w3c.dom.Element parent) |
Find all direct child elements of an element.
|
static java.lang.String |
findText(org.w3c.dom.Node parent) |
Extract nested text from a node.
|
static byte[] |
fromHex(char[] hex,
int start,
int len) |
Decodes data encoded using
toHex. |
static org.w3c.dom.Document |
parse(org.xml.sax.InputSource input,
boolean validate,
boolean namespaceAware,
org.xml.sax.ErrorHandler errorHandler,
org.xml.sax.EntityResolver entityResolver) |
Parses an XML document into a DOM tree.
|
static java.lang.String |
toAttributeValue(java.lang.String val) |
Escape passed string as XML attibute value
(
<, &, ' and "
will be escaped. |
static java.lang.String |
toElementContent(java.lang.String val) |
Escape passed string as XML element content (
<,
& and > |
static java.lang.String |
toHex(byte[] val,
int start,
int len) |
Can be used to encode values that contain invalid XML characters.
|
static org.w3c.dom.Element |
translateXML(org.w3c.dom.Element from,
java.lang.String namespace) |
Convert an XML fragment from one namespace to another.
|
static void |
validate(org.w3c.dom.Element data,
javax.xml.validation.Schema schema) |
Check whether a DOM tree is valid according to a schema.
|
static void |
write(org.w3c.dom.Document doc,
java.io.OutputStream out,
java.lang.String enc) |
Writes a DOM document to a stream.
|
public static org.xml.sax.XMLReader createXMLReader()
throws org.xml.sax.SAXException
createXMLReader(false, false)org.xml.sax.SAXExceptionpublic static org.xml.sax.XMLReader createXMLReader(boolean validate)
throws org.xml.sax.SAXException
validate - if true, a validating parser is returnedcreateXMLReader(validate, false)org.xml.sax.SAXExceptionpublic static org.xml.sax.XMLReader createXMLReader(boolean validate,
boolean namespaceAware)
throws org.xml.sax.SAXException
See parse(org.xml.sax.InputSource, boolean, boolean, org.xml.sax.ErrorHandler, org.xml.sax.EntityResolver) for hints on setting an entity resolver.
validate - if true, a validating parser is returnednamespaceAware - if true, a namespace aware parser is returnedjavax.xml.parsers.FactoryConfigurationError - Application developers should never need to directly catch errors of this type.org.xml.sax.SAXException - if a parser fulfilling given parameters can not be createdpublic static org.w3c.dom.Document createDocument(java.lang.String rootQName,
java.lang.String namespaceURI,
java.lang.String doctypePublicID,
java.lang.String doctypeSystemID)
throws org.w3c.dom.DOMException
Document doc = createDocument("book", null, null, null);
creates new DOM of a well-formed document with root element named book.
rootQName - qualified name of root element. e.g. myroot or ns:myrootnamespaceURI - URI of root element namespace or nulldoctypePublicID - public ID of DOCTYPE or nulldoctypeSystemID - system ID of DOCTYPE or null if no DOCTYPE
required and doctypePublicID is also nullorg.w3c.dom.DOMException - if new DOM with passed parameters can not be createdjavax.xml.parsers.FactoryConfigurationError - Application developers should never need to directly catch errors of this type.public static org.w3c.dom.Document parse(org.xml.sax.InputSource input,
boolean validate,
boolean namespaceAware,
org.xml.sax.ErrorHandler errorHandler,
org.xml.sax.EntityResolver entityResolver)
throws java.io.IOException,
org.xml.sax.SAXException
Remember that when parsing XML files you often want to set an explicit entity resolver. For example, consider a file such as this:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE root PUBLIC "-//NetBeans//DTD Foo 1.0//EN" "http://www.netbeans.org/dtds/foo-1_0.dtd"> <root/>
If you parse this with a null entity resolver, or you use the
default resolver (EntityCatalog.getDefault()) but do not do
anything special with this DTD, you will probably find the parse
blocking to make a network connection even when you are not
validating. That is because DTDs can be used to define
entities and other XML oddities, and are not a pure constraint
language like Schema or RELAX-NG.
There are three basic ways to avoid the network connection.
Register the DTD. This is generally the best thing to do. See
EntityCatalog's documentation for details, but for example
in your layer use:
<filesystem>
<folder name="xml">
<folder name="entities">
<folder name="NetBeans">
<file name="DTD_Foo_1_0"
url="resources/foo-1_0.dtd">
<attr name="hint.originalPublicID"
stringvalue="-//NetBeans//DTD Foo 1.0//EN"/>
</file>
</folder>
</folder>
</folder>
</filesystem>
Now the default system entity catalog will resolve the public ID to the local copy in your module, not the network copy. Additionally, anyone who mounts the "NetBeans Catalog" in the XML Entity Catalogs node in the Runtime tab will be able to use your local copy of the DTD automatically, for validation, code completion, etc. (The network URL should really exist, though, for the benefit of other tools!)
You can also set an explicit entity resolver which maps that particular public ID to some local copy of the DTD, if you do not want to register it globally in the system for some reason. If handed other public IDs, just return null to indicate that the system ID should be loaded.
In some cases where XML parsing is very performance-sensitive, and you know that you do not need validation and furthermore that the DTD defines no infoset (there are no entity or character definitions, etc.), you can speed up the parse. Turn off validation, but also supply a custom entity resolver that does not even bother to load the DTD at all:
public InputSource resolveEntity(String pubid, String sysid)
throws SAXException, IOException {
if (pubid.equals("-//NetBeans//DTD Foo 1.0//EN")) {
return new InputSource(new ByteArrayInputStream(new byte[0]));
} else {
return EntityCatalog.getDefault().resolveEntity(pubid, sysid);
}
}
input - a parser input (for URL users use: new InputSource(url.toString())validate - if true validating parser is usednamespaceAware - if true DOM is created by namespace aware parsererrorHandler - a error handler to notify about exception (such as defaultErrorHandler()) or nullentityResolver - SAX entity resolver (such as EntityCatalog.getDefault()) or nulljava.io.IOException - if an I/O problem during parsing occursorg.xml.sax.SAXException - is thrown if a parser error occursjavax.xml.parsers.FactoryConfigurationError - Application developers should never need to directly catch errors of this type.public static void write(org.w3c.dom.Document doc,
java.io.OutputStream out,
java.lang.String enc)
throws java.io.IOException
Important: There might be some problems with
<![CDATA[ ]]> sections in the DOM tree you pass into this method. Specifically,
some CDATA sections my not be written as CDATA section or may be merged with
other CDATA section at the same level. Also if plain text nodes are mixed with
CDATA sections at the same level all text is likely to end up in one big CDATA section.
For nodes that only have one CDATA section this method should work fine.
doc - DOM document to be writtenout - data sinkenc - XML-defined encoding name (e.g. "UTF-8")java.io.IOException - if JAXP fails or the stream cannot be written topublic static void validate(org.w3c.dom.Element data,
javax.xml.validation.Schema schema)
throws org.xml.sax.SAXException
Element fragment = ...;
SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema s = f.newSchema(This.class.getResource("something.xsd"));
try {
XMLUtil.validate(fragment, s);
// valid
} catch (SAXException x) {
// invalid
}
data - a DOM treeschema - a parsed schemaorg.xml.sax.SAXException - if validation failedpublic static java.lang.String toAttributeValue(java.lang.String val)
throws java.io.CharConversionException
<, &, ' and "
will be escaped.
Note: An XML processor returns normalized value that can be different.val - a string to be escapedjava.io.CharConversionException - if val contains an improper XML characterpublic static java.lang.String toElementContent(java.lang.String val)
throws java.io.CharConversionException
<,
& and > in ]]> sequences).val - a string to be escapedjava.io.CharConversionException - if val contains an improper XML characterpublic static java.lang.String toHex(byte[] val,
int start,
int len)
val - data to be convertedstart - offsetlen - countpublic static byte[] fromHex(char[] hex,
int start,
int len)
throws java.io.IOException
toHex.hex - data to be convertedstart - offsetlen - countjava.io.IOException - if input does not represent hex encoded valuepublic static void appendChildElement(org.w3c.dom.Element parent,
org.w3c.dom.Element el,
java.lang.String[] order)
throws java.lang.IllegalArgumentException
order. All existing child elements must be
include as well as the new element. The existing child element following
the new child is important, as the element will be 'inserted before', not
'inserted after'.parent - parent to which the child will be appendedel - element to be addedorder - order of the elements which must be followedjava.lang.IllegalArgumentException - if the order cannot be followed, either
a missing existing or new child element is not specified in orderpublic static java.util.List<org.w3c.dom.Element> findSubElements(org.w3c.dom.Element parent)
throws java.lang.IllegalArgumentException
parent - a parent element in a DOM treejava.lang.IllegalArgumentException - if there are non-element children besides whitespacepublic static org.w3c.dom.Element findElement(org.w3c.dom.Element parent,
java.lang.String name,
java.lang.String namespace)
throws java.lang.IllegalArgumentException
null namespace will match any namespace.
parent - a parent elementname - the intended local namenamespace - the intended namespace (or null)java.lang.IllegalArgumentException - if there is multiple elements of the same namepublic static java.lang.String findText(org.w3c.dom.Node parent)
parent - a parent elementpublic static org.w3c.dom.Element translateXML(org.w3c.dom.Element from,
java.lang.String namespace)
from - element to translatenamespace - namespace to be translated topublic static void copyDocument(org.w3c.dom.Element from,
org.w3c.dom.Element to,
java.lang.String newNamespace)
from - copy the children of this element (exclusive)to - where to attach the copied elementsnewNamespace - destination namespacepublic static org.xml.sax.ErrorHandler defaultErrorHandler()
Built on April 24 2018. | Portions Copyright 1997-2018 Oracle. All rights reserved.