APIdia Launch Partner
Top Description Fields Constructors Methods
javax.xml.parsers

public abstract Class DocumentBuilderFactory

extends Object
Class Inheritance
Known Direct Subclasses
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
Imports
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl, javax.xml.validation.Schema

Defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents.
Authors
Jeff Suttor, Neeraj Bajaj
Since
1.4

Field Summary

Modifier and TypeField and Description
private boolean
private static final String
private boolean
private boolean
private boolean
private boolean
private boolean

Constructor Summary

AccessConstructor and Description
protected
DocumentBuilderFactory()

Protected constructor to prevent instantiation.

Method Summary

Modifier and TypeMethod and Description
public abstract Object

Returns:

value The value of the attribute.
getAttribute
(String
The name of the attribute.
name
)

Allows the user to retrieve specific attributes on the underlying implementation.

public abstract boolean

Returns:

State of the named feature.
getFeature
(String
Feature name.
name
)

Get the state of the named feature.

public Schema

Returns:

the Schema object that was last set through the setSchema(Schema) method, or null if the method was not invoked since a DocumentBuilderFactory is created.
getSchema
()

Gets the Schema object specified through the setSchema(Schema schema) method.

public boolean

Returns:

true if the factory is configured to produce parsers which converts CDATA nodes to Text nodes and appends it to the adjacent (if any) Text node; false otherwise.
isCoalescing
()

Indicates whether or not the factory is configured to produce parsers which converts CDATA nodes to Text nodes and appends it to the adjacent (if any) Text node.

public boolean

Returns:

true if the factory is configured to produce parsers which expand entity reference nodes; false otherwise.
isExpandEntityReferences
()

Indicates whether or not the factory is configured to produce parsers which expand entity reference nodes.

public boolean

Returns:

true if the factory is configured to produce parsers which ignores comments; false otherwise.
isIgnoringComments
()

Indicates whether or not the factory is configured to produce parsers which ignores comments.

public boolean

Returns:

true if the factory is configured to produce parsers which ignore ignorable whitespace in element content; false otherwise.
isIgnoringElementContentWhitespace
()

Indicates whether or not the factory is configured to produce parsers which ignore ignorable whitespace in element content.

public boolean

Returns:

true if the factory is configured to produce parsers which are namespace aware; false otherwise.
isNamespaceAware
()

Indicates whether or not the factory is configured to produce parsers which are namespace aware.

public boolean

Returns:

true if the factory is configured to produce parsers which validate the XML content during parse; false otherwise.
isValidating
()

Indicates whether or not the factory is configured to produce parsers which validate the XML content during parse.

public boolean

Returns:

current state of XInclude processing
isXIncludeAware
()

Get state of XInclude processing.

private static DocumentBuilderFactory
public static DocumentBuilderFactory

Returns:

A new instance of the DocumentBuilderFactory builtin system-default implementation.
newDefaultInstance
()

Creates a new instance of the DocumentBuilderFactory builtin system-default implementation.

public static DocumentBuilderFactory

Returns:

a new instance of the DocumentBuilderFactory builtin system-default implementation.
newDefaultNSInstance
()

Creates a new NamespaceAware instance of the DocumentBuilderFactory builtin system-default implementation.

public abstract DocumentBuilder

Returns:

A new instance of a DocumentBuilder.
newDocumentBuilder
()

Creates a new instance of a javax.xml.parsers.DocumentBuilder using the currently configured parameters.

public static DocumentBuilderFactory

Returns:

New instance of a DocumentBuilderFactory
newInstance
()

Obtains a new instance of a DocumentBuilderFactory.

public static DocumentBuilderFactory

Returns:

New instance of a DocumentBuilderFactory
newInstance
(String
fully qualified factory class name that provides implementation of javax.xml.parsers.DocumentBuilderFactory.
factoryClassName
,
ClassLoader
ClassLoader used to load the factory class. If null current Thread's context classLoader is used to load the factory class.
classLoader
)

Obtain a new instance of a DocumentBuilderFactory from class name.

public static DocumentBuilderFactory

Returns:

a new instance of a DocumentBuilderFactory
newNSInstance
()

Creates a new NamespaceAware instance of a DocumentBuilderFactory.

public static DocumentBuilderFactory

Returns:

a new instance of a DocumentBuilderFactory
newNSInstance
(String
a fully qualified factory class name that provides implementation of javax.xml.parsers.DocumentBuilderFactory.
factoryClassName
,
ClassLoader
the ClassLoader used to load the factory class. If it is null, the current Thread's context classLoader is used to load the factory class.
classLoader
)

Creates a new NamespaceAware instance of a DocumentBuilderFactory from the class name.

public abstract void
setAttribute(String
The name of the attribute.
name
,
Object
The value of the attribute.
value
)

Allows the user to set specific attributes on the underlying implementation.

public void
setCoalescing(boolean
true if the parser produced will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node; false otherwise.
coalescing
)

Specifies that the parser produced by this code will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node.

public void
setExpandEntityReferences(boolean
true if the parser produced will expand entity reference nodes; false otherwise.
expandEntityRef
)

Specifies that the parser produced by this code will expand entity reference nodes.

public abstract void
setFeature(String
Feature name.
name
,
boolean
Is feature state true or false.
value
)

Set a feature for this DocumentBuilderFactory and DocumentBuilders created by this factory.

public void
setIgnoringComments(boolean
boolean value to ignore comments during processing
ignoreComments
)

Specifies that the parser produced by this code will ignore comments.

public void
setIgnoringElementContentWhitespace(boolean
true if the parser created must eliminate whitespace in the element content when parsing XML documents; false otherwise.
whitespace
)

Specifies that the parsers created by this factory must eliminate whitespace in element content (sometimes known loosely as 'ignorable whitespace') when parsing XML documents (see XML Rec 2.10).

public void
setNamespaceAware(boolean
true if the parser produced will provide support for XML namespaces; false otherwise.
awareness
)

Specifies that the parser produced by this code will provide support for XML namespaces.

public void
setSchema(Schema
Schema to use or null to remove a schema.
schema
)

Set the Schema to be used by parsers created from this factory.

public void
setValidating(boolean
true if the parser produced will validate documents as they are parsed; false otherwise.
validating
)

Specifies that the parser produced by this code will validate documents as they are parsed.

public void
setXIncludeAware(final boolean
Set XInclude processing to true or false
state
)

Set state of XInclude processing.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

coalescingback to summary
private boolean coalescing
DEFAULT_IMPLback to summary
private static final String DEFAULT_IMPL
expandEntityRefback to summary
private boolean expandEntityRef
ignoreCommentsback to summary
private boolean ignoreComments
namespaceAwareback to summary
private boolean namespaceAware
validatingback to summary
private boolean validating
whitespaceback to summary
private boolean whitespace

Constructor Detail

DocumentBuilderFactoryback to summary
protected DocumentBuilderFactory()

Protected constructor to prevent instantiation. Use newInstance().

Method Detail

getAttributeback to summary
public abstract Object getAttribute(String name) throws IllegalArgumentException

Allows the user to retrieve specific attributes on the underlying implementation.

Parameters
name:String

The name of the attribute.

Returns:Object

value The value of the attribute.

Exceptions
IllegalArgumentException:
thrown if the underlying implementation doesn't recognize the attribute.
getFeatureback to summary
public abstract boolean getFeature(String name) throws ParserConfigurationException

Get the state of the named feature.

Feature names are fully qualified java.net.URIs. Implementations may define their own features. An ParserConfigurationException is thrown if this DocumentBuilderFactory or the DocumentBuilders it creates cannot support the feature. It is possible for an DocumentBuilderFactory to expose a feature value but be unable to change its state.

Parameters
name:String

Feature name.

Returns:boolean

State of the named feature.

Exceptions
ParserConfigurationException:
if this DocumentBuilderFactory or the DocumentBuilders it creates cannot support this feature.
Since
1.5
getSchemaback to summary
public Schema getSchema()

Gets the Schema object specified through the setSchema(Schema schema) method.

Returns:Schema

the Schema object that was last set through the setSchema(Schema) method, or null if the method was not invoked since a DocumentBuilderFactory is created.

Exceptions
UnsupportedOperationException:
When implementation does not override this method.
Since
1.5
isCoalescingback to summary
public boolean isCoalescing()

Indicates whether or not the factory is configured to produce parsers which converts CDATA nodes to Text nodes and appends it to the adjacent (if any) Text node.

Returns:boolean

true if the factory is configured to produce parsers which converts CDATA nodes to Text nodes and appends it to the adjacent (if any) Text node; false otherwise.

isExpandEntityReferencesback to summary
public boolean isExpandEntityReferences()

Indicates whether or not the factory is configured to produce parsers which expand entity reference nodes.

Returns:boolean

true if the factory is configured to produce parsers which expand entity reference nodes; false otherwise.

isIgnoringCommentsback to summary
public boolean isIgnoringComments()

Indicates whether or not the factory is configured to produce parsers which ignores comments.

Returns:boolean

true if the factory is configured to produce parsers which ignores comments; false otherwise.

isIgnoringElementContentWhitespaceback to summary
public boolean isIgnoringElementContentWhitespace()

Indicates whether or not the factory is configured to produce parsers which ignore ignorable whitespace in element content.

Returns:boolean

true if the factory is configured to produce parsers which ignore ignorable whitespace in element content; false otherwise.

isNamespaceAwareback to summary
public boolean isNamespaceAware()

Indicates whether or not the factory is configured to produce parsers which are namespace aware.

Returns:boolean

true if the factory is configured to produce parsers which are namespace aware; false otherwise.

isValidatingback to summary
public boolean isValidating()

Indicates whether or not the factory is configured to produce parsers which validate the XML content during parse.

Returns:boolean

true if the factory is configured to produce parsers which validate the XML content during parse; false otherwise.

isXIncludeAwareback to summary
public boolean isXIncludeAware()

Get state of XInclude processing.

Returns:boolean

current state of XInclude processing

Exceptions
UnsupportedOperationException:
When implementation does not override this method.
Since
1.5
makeNSAwareback to summary
private static DocumentBuilderFactory makeNSAware(DocumentBuilderFactory dbf)
newDefaultInstanceback to summary
public static DocumentBuilderFactory newDefaultInstance()

Creates a new instance of the DocumentBuilderFactory builtin system-default implementation.

Returns:DocumentBuilderFactory

A new instance of the DocumentBuilderFactory builtin system-default implementation.

Since
9
newDefaultNSInstanceback to summary
public static DocumentBuilderFactory newDefaultNSInstance()

Creates a new NamespaceAware instance of the DocumentBuilderFactory builtin system-default implementation. Parsers produced by the factory instance provides support for XML namespaces by default.

Implementation Specification

In addition to creating a factory instance using the same process as newDefaultInstance(), this method must set NamespaceAware to true.

Returns:DocumentBuilderFactory

a new instance of the DocumentBuilderFactory builtin system-default implementation.

Since
13
newDocumentBuilderback to summary
public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException

Creates a new instance of a javax.xml.parsers.DocumentBuilder using the currently configured parameters.

Returns:DocumentBuilder

A new instance of a DocumentBuilder.

Exceptions
ParserConfigurationException:
if a DocumentBuilder cannot be created which satisfies the configuration requested.
newInstanceback to summary
public static DocumentBuilderFactory newInstance()

Obtains a new instance of a DocumentBuilderFactory. This method uses the JAXP Lookup Mechanism to determine the DocumentBuilderFactory implementation class to load.

Once an application has obtained a reference to a DocumentBuilderFactory, it can use the factory to configure and obtain parser instances.

Tip for Trouble-shooting

Setting the jaxp.debug system property will cause this method to print a lot of debug messages to System.err about what it is doing and where it is looking at.

If you have problems loading DocumentBuilders, try:

java -Djaxp.debug=1 YourProgram ....
Returns:DocumentBuilderFactory

New instance of a DocumentBuilderFactory

Exceptions
FactoryConfigurationError:
in case of service configuration error or if the implementation is not available or cannot be instantiated.
newInstanceback to summary
public static DocumentBuilderFactory newInstance(String factoryClassName, ClassLoader classLoader)

Obtain a new instance of a DocumentBuilderFactory from class name. This function is useful when there are multiple providers in the classpath. It gives more control to the application as it can specify which provider should be loaded.

Once an application has obtained a reference to a DocumentBuilderFactory it can use the factory to configure and obtain parser instances.

Tip for Trouble-shooting

Setting the jaxp.debug system property will cause this method to print a lot of debug messages to System.err about what it is doing and where it is looking at.

If you have problems try:

java -Djaxp.debug=1 YourProgram ....
Parameters
factoryClassName:String

fully qualified factory class name that provides implementation of javax.xml.parsers.DocumentBuilderFactory.

classLoader:ClassLoader

ClassLoader used to load the factory class. If null current Thread's context classLoader is used to load the factory class.

Returns:DocumentBuilderFactory

New instance of a DocumentBuilderFactory

Exceptions
FactoryConfigurationError:
if factoryClassName is null, or the factory class cannot be loaded, instantiated.
Since
1.6
See Also
newInstance()
newNSInstanceback to summary
public static DocumentBuilderFactory newNSInstance()

Creates a new NamespaceAware instance of a DocumentBuilderFactory. Parsers produced by the factory instance provides support for XML namespaces by default.

Implementation Specification

In addition to creating a factory instance using the same process as newInstance(), this method must set NamespaceAware to true.

Returns:DocumentBuilderFactory

a new instance of a DocumentBuilderFactory

Exceptions
FactoryConfigurationError:
in case of service configuration error or if the implementation is not available or cannot be instantiated.
Since
13
newNSInstanceback to summary
public static DocumentBuilderFactory newNSInstance(String factoryClassName, ClassLoader classLoader)

Creates a new NamespaceAware instance of a DocumentBuilderFactory from the class name. Parsers produced by the factory instance provides support for XML namespaces by default.

Implementation Specification

In addition to creating a factory instance using the same process as newInstance(java.lang.String, java.lang.ClassLoader), this method must set NamespaceAware to true.

Parameters
factoryClassName:String

a fully qualified factory class name that provides implementation of javax.xml.parsers.DocumentBuilderFactory.

classLoader:ClassLoader

the ClassLoader used to load the factory class. If it is null, the current Thread's context classLoader is used to load the factory class.

Returns:DocumentBuilderFactory

a new instance of a DocumentBuilderFactory

Exceptions
FactoryConfigurationError:
if factoryClassName is null, or the factory class cannot be loaded, instantiated.
Since
13
setAttributeback to summary
public abstract void setAttribute(String name, Object value) throws IllegalArgumentException

Allows the user to set specific attributes on the underlying implementation.

All implementations that implement JAXP 1.5 or newer are required to support the javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD and javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA properties.

Parameters
name:String

The name of the attribute.

value:Object

The value of the attribute.

Exceptions
IllegalArgumentException:
thrown if the underlying implementation doesn't recognize the attribute.
setCoalescingback to summary
public void setCoalescing(boolean coalescing)

Specifies that the parser produced by this code will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node. By default the value of this is set to false

Parameters
coalescing:boolean

true if the parser produced will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node; false otherwise.

setExpandEntityReferencesback to summary
public void setExpandEntityReferences(boolean expandEntityRef)

Specifies that the parser produced by this code will expand entity reference nodes. By default the value of this is set to true

Parameters
expandEntityRef:boolean

true if the parser produced will expand entity reference nodes; false otherwise.

setFeatureback to summary
public abstract void setFeature(String name, boolean value) throws ParserConfigurationException

Set a feature for this DocumentBuilderFactory and DocumentBuilders created by this factory.

Feature names are fully qualified java.net.URIs. Implementations may define their own features. A ParserConfigurationException is thrown if this DocumentBuilderFactory or the DocumentBuilders it creates cannot support the feature. It is possible for a DocumentBuilderFactory to expose a feature value but be unable to change its state.

All implementations are required to support the javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING feature. When the feature is:

Parameters
name:String

Feature name.

value:boolean

Is feature state true or false.

Exceptions
ParserConfigurationException:
if this DocumentBuilderFactory or the DocumentBuilders it creates cannot support this feature.
NullPointerException:
If the name parameter is null.
Since
1.5
setIgnoringCommentsback to summary
public void setIgnoringComments(boolean ignoreComments)

Specifies that the parser produced by this code will ignore comments. By default the value of this is set to false.

Parameters
ignoreComments:boolean

boolean value to ignore comments during processing

setIgnoringElementContentWhitespaceback to summary
public void setIgnoringElementContentWhitespace(boolean whitespace)

Specifies that the parsers created by this factory must eliminate whitespace in element content (sometimes known loosely as 'ignorable whitespace') when parsing XML documents (see XML Rec 2.10). Note that only whitespace which is directly contained within element content that has an element only content model (see XML Rec 3.2.1) will be eliminated. Due to reliance on the content model this setting requires the parser to be in validating mode. By default the value of this is set to false.

Parameters
whitespace:boolean

true if the parser created must eliminate whitespace in the element content when parsing XML documents; false otherwise.

setNamespaceAwareback to summary
public void setNamespaceAware(boolean awareness)

Specifies that the parser produced by this code will provide support for XML namespaces. By default the value of this is set to false

Parameters
awareness:boolean

true if the parser produced will provide support for XML namespaces; false otherwise.

setSchemaback to summary
public void setSchema(Schema schema)

Set the Schema to be used by parsers created from this factory.

When a Schema is non-null, a parser will use a validator created from it to validate documents before it passes information down to the application.

When errors are found by the validator, the parser is responsible to report them to the user-specified org.xml.sax.ErrorHandler (or if the error handler is not set, ignore them or throw them), just like any other errors found by the parser itself. In other words, if the user-specified org.xml.sax.ErrorHandler is set, it must receive those errors, and if not, they must be treated according to the implementation specific default error handling rules.

A validator may modify the outcome of a parse (for example by adding default values that were missing in documents), and a parser is responsible to make sure that the application will receive modified DOM trees.

Initially, null is set as the Schema.

This processing will take effect even if the isValidating() method returns false.

It is an error to use the http://java.sun.com/xml/jaxp/properties/schemaSource property and/or the http://java.sun.com/xml/jaxp/properties/schemaLanguage property in conjunction with a Schema object. Such configuration will cause a ParserConfigurationException exception when the newDocumentBuilder() is invoked.

Note for implementors

A parser must be able to work with any Schema implementation. However, parsers and schemas are allowed to use implementation-specific custom mechanisms as long as they yield the result described in the specification.

Parameters
schema:Schema

Schema to use or null to remove a schema.

Exceptions
UnsupportedOperationException:
When implementation does not override this method.
Since
1.5
setValidatingback to summary
public void setValidating(boolean validating)

Specifies that the parser produced by this code will validate documents as they are parsed. By default the value of this is set to false.

Note that "the validation" here means a validating parser as defined in the XML recommendation. In other words, it essentially just controls the DTD validation. (except the legacy two properties defined in JAXP 1.2.)

To use modern schema languages such as W3C XML Schema or RELAX NG instead of DTD, you can configure your parser to be a non-validating parser by leaving the setValidating(boolean) method false, then use the setSchema(Schema) method to associate a schema to a parser.

Parameters
validating:boolean

true if the parser produced will validate documents as they are parsed; false otherwise.

setXIncludeAwareback to summary
public void setXIncludeAware(final boolean state)

Set state of XInclude processing.

If XInclude markup is found in the document instance, should it be processed as specified in XML Inclusions (XInclude) Version 1.0.

XInclude processing defaults to false.

Parameters
state:boolean

Set XInclude processing to true or false

Exceptions
UnsupportedOperationException:
When implementation does not override this method.
Since
1.5