Class XMLUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendChildElement(Element parent, Element el, String[] order) Append a child element to the parent at the specified location.static voidcopyDocument(Element from, Element to, String newNamespace) Copy elements from one document to another attaching at the specified element and translating the namespace.static DocumentcreateDocument(String rootQName, String namespaceURI, String doctypePublicID, String doctypeSystemID) Creates an empty DOM document.static XMLReaderCreate a simple parser.static XMLReadercreateXMLReader(boolean validate) Create a simple parser, possibly validating.static XMLReadercreateXMLReader(boolean validate, boolean namespaceAware) Creates a SAX parser.static ErrorHandlerCreate an XML error handler that rethrows errors and fatal errors and logs warnings.static ElementfindElement(Element parent, String name, String namespace) Search for an XML element in the direct children of parent only.findSubElements(Element parent) Find all direct child elements of an element.static StringExtract nested text from a node.static byte[]fromHex(char[] hex, int start, int len) Decodes data encoded usingtoHex.static Documentparse(InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver) Parses an XML document into a DOM tree.static StringtoAttributeValue(String val) Escape passed string as XML attibute value (<,&,'and"will be escaped.static StringtoElementContent(String val) Escape passed string as XML element content (<,&and>in]]>sequences).static StringtoHex(byte[] val, int start, int len) Can be used to encode values that contain invalid XML characters.static ElementtranslateXML(Element from, String namespace) Convert an XML fragment from one namespace to another.static voidCheck whether a DOM tree is valid according to a schema.static voidwrite(Document doc, OutputStream out, String enc) Writes a DOM document to a stream.
-
Method Details
-
createXMLReader
Create a simple parser.- Returns:
createXMLReader(false, false)- Throws:
SAXException- if a parser fulfilling given parameters can not be created
-
createXMLReader
Create a simple parser, possibly validating.- Parameters:
validate- if true, a validating parser is returned- Returns:
createXMLReader(validate, false)- Throws:
SAXException- if a parser fulfilling given parameters can not be created
-
createXMLReader
public static XMLReader createXMLReader(boolean validate, boolean namespaceAware) throws SAXException Creates a SAX parser.See
parse(org.xml.sax.InputSource, boolean, boolean, org.xml.sax.ErrorHandler, org.xml.sax.EntityResolver)for hints on setting an entity resolver.- Parameters:
validate- if true, a validating parser is returnednamespaceAware- if true, a namespace aware parser is returned- Returns:
- XMLReader configured according to passed parameters
- Throws:
FactoryConfigurationError- Application developers should never need to directly catch errors of this type.SAXException- if a parser fulfilling given parameters can not be created
-
createDocument
public static Document createDocument(String rootQName, String namespaceURI, String doctypePublicID, String doctypeSystemID) throws DOMException Creates an empty DOM document. E.g.:Document doc = createDocument("book", null, null, null);creates new DOM of a well-formed document with root element named book.- Parameters:
rootQName- qualified name of root element, for examplemyrootorns:myrootnamespaceURI- URI of root element namespace ornulldoctypePublicID- public ID of DOCTYPE ornulldoctypeSystemID- system ID of DOCTYPE ornullif no DOCTYPE required and doctypePublicID is alsonull- Returns:
- new DOM Document
- Throws:
DOMException- if new DOM with passed parameters can not be createdFactoryConfigurationError- Application developers should never need to directly catch errors of this type.
-
parse
public static Document parse(InputSource input, boolean validate, boolean namespaceAware, ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException Parses an XML document into a DOM tree.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); } }
- Parameters:
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 asdefaultErrorHandler()) ornullentityResolver- SAX entity resolver (such as EntityCatalog#getDefault) ornull- Returns:
- document representing given input
- Throws:
IOException- if an I/O problem during parsing occursSAXException- is thrown if a parser error occursFactoryConfigurationError- Application developers should never need to directly catch errors of this type.
-
Register the DTD. This is generally the best thing to do. See
EntityCatalog's documentation for details, but for example in your layer
use:
-
write
Writes a DOM document to a stream. The precise output format is not guaranteed but this method will attempt to indent it sensibly.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.- Parameters:
doc- DOM document to be writtenout- data sinkenc- XML-defined encoding name (for example, "UTF-8")- Throws:
IOException- if JAXP fails or the stream cannot be written to
-
validate
Check whether a DOM tree is valid according to a schema. Example of usage: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 }- Parameters:
data- a DOM treeschema- a parsed schema- Throws:
SAXException- if validation failed- Since:
- org.openide.util 7.17
-
toAttributeValue
Escape passed string as XML attibute value (<,&,'and"will be escaped. Note: An XML processor returns normalized value that can be different.- Parameters:
val- a string to be escaped- Returns:
- escaped value
- Throws:
CharConversionException- if val contains an improper XML character- Since:
- 1.40
-
toElementContent
Escape passed string as XML element content (<,&and>in]]>sequences).- Parameters:
val- a string to be escaped- Returns:
- escaped value
- Throws:
CharConversionException- if val contains an improper XML character- Since:
- 1.40
-
toHex
Can be used to encode values that contain invalid XML characters. At SAX parser end must be used pair method to get original value.- Parameters:
val- data to be convertedstart- offsetlen- count- Returns:
- the converted data
- Since:
- 1.29
-
fromHex
Decodes data encoded usingtoHex.- Parameters:
hex- data to be convertedstart- offsetlen- count- Returns:
- the converted data
- Throws:
IOException- if input does not represent hex encoded value- Since:
- 1.29
-
appendChildElement
public static void appendChildElement(Element parent, Element el, String[] order) throws IllegalArgumentException Append a child element to the parent at the specified location. Starting with a valid document, append an element according to the schema sequence represented by theorder. 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'.- Parameters:
parent- parent to which the child will be appendedel- element to be addedorder- order of the elements which must be followed- Throws:
IllegalArgumentException- if the order cannot be followed, either a missing existing or new child element is not specified in order- Since:
- 8.4
-
findSubElements
Find all direct child elements of an element. Children which are all-whitespace text nodes or comments are ignored; others cause an exception to be thrown.- Parameters:
parent- a parent element in a DOM tree- Returns:
- a list of direct child elements (may be empty)
- Throws:
IllegalArgumentException- if there are non-element children besides whitespace- Since:
- 8.4
-
findElement
public static Element findElement(Element parent, String name, String namespace) throws IllegalArgumentException Search for an XML element in the direct children of parent only. This compares localName (nodeName if localName is null) to name, and checks the tags namespace with the provided namespace. Anullnamespace will match any namespace.This is differs from the DOM version by:
- not searching recursively
- returns a single result
- Parameters:
parent- a parent elementname- the intended local namenamespace- the intended namespace (or null)- Returns:
- the one child element with that name, or null if none
- Throws:
IllegalArgumentException- if there is multiple elements of the same name- Since:
- 8.4
-
findText
Extract nested text from a node. Currently does not handle coalescing text nodes, CDATA sections, etc.- Parameters:
parent- a parent element- Returns:
- the nested text, or null if none was found
- Since:
- 8.4
-
translateXML
Convert an XML fragment from one namespace to another.- Parameters:
from- element to translatenamespace- namespace to be translated to- Returns:
- the element in the new namespace
- Since:
- 8.4
-
copyDocument
Copy elements from one document to another attaching at the specified element and translating the namespace.- Parameters:
from- copy the children of this element (exclusive)to- where to attach the copied elementsnewNamespace- destination namespace- Since:
- 8.4
-
defaultErrorHandler
Create an XML error handler that rethrows errors and fatal errors and logs warnings.- Returns:
- a standard error handler
- Since:
- 8.4
-