java.lang.Object
javax.xml.parsers.DocumentBuilderFactory
- See Also:
- Top Examples, Source Code
protected DocumentBuilderFactory()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[152]Create DOM document builder and parse the document
By Anonymous on 2004/01/06 16:33:01 Rate
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( ) ;
DocumentBuilder builder = factory.newDocumentBuilder ( ) ;
Document document = builder.parse ( "priceList.xml" ) ;
NodeList myNL = document.getElementsByTagName ( "price" ) ;
[600]_
By Anonymous on 2005/06/15 23:35:32 Rate
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( ) ;
DocumentBuilder builder = factory.newDocumentBuilder ( ) ;
Document document = builder.parse ( "priceList.xml" ) ;
NodeList myNL = document.getElementsByTagName ( "price" ) ;
} catch ( Exception ex ) {
ex.printStackTrace ( ) ;
}
}
[880]_
By Anonymous on 2004/08/27 15:58:37 Rate
public class teste {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( ) ;
DocumentBuilder builder = factory.newDocumentBuilder ( ) ;
}
public abstract Object getAttribute(String name)
throws IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract boolean getFeature(String name)
throws ParserConfigurationException
- See Also:
-
URI
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Schema getSchema()
- See Also:
- UnsupportedOperationException,
SAXParserFactory
, setSchema(Schema)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean isCoalescing()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean isExpandEntityReferences()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean isIgnoringComments()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean isIgnoringElementContentWhitespace()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean isNamespaceAware()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean isValidating()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean isXIncludeAware()
- See Also:
- UnsupportedOperationException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract DocumentBuilder newDocumentBuilder()
throws ParserConfigurationException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static DocumentBuilderFactory newInstance()
- See Also:
- FactoryConfigurationError,
DocumentBuilder
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void setAttribute(String name,
Object value)
throws IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1734]Where are the attributes that I can set?
By deb on 2006/03/24 07:46:12 Rate
How does one use the DocumentBuilderFactory.setAttribute ( ) method. Where are the attributes available for me to set? I want to ignore the DTD as I'm parsing the DOM. Using setNamespaceAware and setValidating does not work.
[1916]Associating a XML DOM Document with a XSD Schema
By Anonymous on 2007/08/14 16:14:55 Rate
There are two ways to do that:
* With a schema declaration in the XML document
* By specifying the schema ( s ) to use in the application
Note: When the application specifies the schema ( s ) to use, it overrides any schema declarations in the document.
To specify the schema definition in the document, you create XML like this:
< documentRoot
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='YourSchemaDefinition.xsd'
>
...
The first attribute defines the XML namespace ( xmlns ) prefix, xsi, which stands for "XML Schema instance." The second line specifies the schema to use for elements in the document that do not have a namespace prefix--that is, for the elements you typically define in any simple, uncomplicated XML document. ( You'll see how to deal with multiple namespaces in the next section. )
You can also specify the schema file in the application:
static final String schemaSource = "YourSchemaDefinition.xsd";
static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
...
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( )
...
factory.setAttribute ( JAXP_SCHEMA_SOURCE, new File ( schemaSource ) ) ;
[1918]Validating with Multiple Namespaces
By Anonymous on 2007/08/14 16:20:05 Rate
To declare the equivalent schemas in the application, the code would look something like this:
static final String employeeSchema = "employeeDatabase.xsd";
static final String taxSchema = "w2TaxForm.xsd";
static final String hiringSchema = "hiringForm.xsd";
static final String [ ] schemas = {
employeeSchema,
taxSchema,
hiringSchema,
} ;
static final String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";
...
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance ( )
...
factory.setAttribute ( JAXP_SCHEMA_SOURCE, schemas ) ;
Here, the array of strings that points to the schema definitions ( .xsd files ) is passed as the argument to the factory.setAttribute method. Note the differences from when you were declaring the schemas to use as part of the XML data set:
* There is no special declaration for the default ( unnamed ) schema.
* You don't specify the namespace name. Instead, you only give pointers to the .xsd files.
To make the namespace assignments, the parser reads the .xsd files, and finds in them the name of the target namespace they apply to. Because the files are specified with URIs, the parser can use an EntityResolver ( if one has been defined ) to find a local copy of the schema.
If the schema definition does not define a target namespace, then it applies to the default ( unnamed, or null ) namespace. So, in our example, you would expect to see these target namespace declarations in the schemas:
* employeeDatabase.xsd: none
* w2TaxForm.xsd: http://www.irs.gov/
* hiringForm.xsd: http://www.ourcompany.com
At this point, you have seen two possible values for the schema source property when invoking the factory.setAttribute ( ) method: a File object in factory.setAttribute ( JAXP_SCHEMA_SOURCE, new File ( schemaSource ) ) and an array of strings in factory.setAttribute ( JAXP_SCHEMA_SOURCE, schemas ) . Here is a complete list of the possible values for that argument:
* A string that points to the URI of the schema
* An InputStream with the contents of the schema
* A SAX InputSource
* A File
* An array of Objects, each of which is one of the types defined here.
Note: An array of Objects can be used only when the schema language ( like http://java.sun.com/xml/jaxp/properties/schemaLanguage ) has the ability to assemble a schema at runtime. Also, when an array of Objects is passed it is illegal to have two schemas that share the same namespace.
public void setCoalescing(boolean coalescing)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setExpandEntityReferences(boolean expandEntityRef)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void setFeature(String name,
boolean value)
throws ParserConfigurationException
- See Also:
- NullPointerException,
DocumentBuilder.setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
, ErrorHandler.fatalError(SAXParseException exception)
, XMLConstants.FEATURE_SECURE_PROCESSING
, URI
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setIgnoringComments(boolean ignoreComments)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setIgnoringElementContentWhitespace(boolean whitespace)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setNamespaceAware(boolean awareness)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1917]Configure DocumentBuilderFactory to generate a namespace-aware, validating parser that uses XML Schema
By Anonymous on 2007/08/14 16:17:16 Rate
static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";
...
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( )
factory.setNamespaceAware ( true ) ;
factory.setValidating ( true ) ;
try {
factory.setAttribute ( JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA ) ;
}
catch ( IllegalArgumentException x ) {
// Happens if the parser does not support JAXP 1.2
...
}
Because JAXP-compliant parsers are not namespace-aware by default, it is necessary to set the property for schema validation to work. You also set a factory attribute to specify the parser language to use. ( For SAX parsing, on the other hand, you set a property on the parser generated by the factory. )
public void setSchema(Schema schema)
- See Also:
- UnsupportedOperationException,
newDocumentBuilder()
, ParserConfigurationException
, isValidating()
, DOMErrorHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setValidating(boolean validating)
- See Also:
setSchema(Schema)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[584]Transform and print a XML document
By Anonymous on 2003/12/23 00:40:20 Rate
import java.io.PrintStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.xml.sax.*;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import org.xml.sax.*;
class Parser
{
public static void main ( String args [ ] )
throws Exception
{
if ( args == null || args.length == 0 )
{
System.out.println ( "usage: java Parser inputXML " ) ;
System.exit ( -1 ) ;
}
String s = args [ 0 ] .trim ( ) ;
try {
TransformerFactory tFactory = TransformerFactory.newInstance ( ) ;
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance ( ) ;
documentbuilderfactory.setExpandEntityReferences ( true ) ;
//documentbuilderfactory.setNamespaceAware ( true ) ;
//documentbuilderfactory.setValidating ( true ) ;
DocumentBuilder documentbuilder = documentbuilderfactory.newDocumentBuilder ( ) ;
System.out.println ( "Parser is " + documentbuilder.getClass ( ) .getName ( ) ) ;
Document dom = documentbuilder.parse ( s ) ;
if ( documentbuilderfactory.isExpandEntityReferences ( ) )
{
System.out.println ( "true" ) ;
}
else
{
System.out.println ( "false" ) ;
}
DOMSource domSource = new DOMSource ( ) ;
domSource.setNode ( dom ) ;
StreamResult result = new StreamResult ( System.out ) ;
// Create the transformer
Transformer transformer = tFactory.newTransformer ( ) ;
transformer.transform ( domSource, result ) ;
}
catch ( SAXParseException saxparseexception )
{
System.out.println ( "Error : " + saxparseexception.getMessage ( ) + " @ " + saxparseexception.getLineNumber ( ) + "," + saxparseexception.getColumnNumber ( ) + " in " + saxparseexception.getSystemId ( ) ) ;
}
}
}
public void setXIncludeAware(boolean state)
- See Also:
- UnsupportedOperationException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples