Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Converting an XML tree to a Document



It is possible to convert an XML tree to a DOM Document which can be used, for example for XSL transformations.

Creation

You only need to use a XMLDocumentBuilderFactory factory to create the Document:
   XMLDocumentBuilderFactory factory = XMLDocumentBuilderFactory.newInstance();
   Document document = factory.parse(<the XML File>);

Usage

You can use the Document in many situations, such as using an XSL transform. For example:
   // create the Document
   XMLDocumentBuilderFactory factory =  XMLDocumentBuilderFactory.newInstance();
   Document document = factory.parse(<the XML File>);      
      
   // apply the XSL transform
   TransformerFactory factory = TransformerFactory.newInstance();

   StreamSource streamSource = new StreamSource(<the XSL transform>);
   Transformer transformer = factory.newTransformer(streamSource);
   transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
   transformer.setOutputProperty(OutputKeys.METHOD, "xml");
   transformer.setOutputProperty(OutputKeys.INDENT, "yes");
   transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
   transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

   DOMSource xslDomSource = new DOMSource(document);
   File outputFile = ... // the XSL output file
   try ( FileOutputStream outputStream = new FileOutputStream(outputFile)) {
      try {
        transformer.transform(xslDomSource, new StreamResult(outputStream));
      } catch (TransformerException e) {
      }
   }      

Configuration options

Indentation

By default the indentation of nodes in the DOM Document is 2.

You can specify the indentation by using the XMLDocumentBuilderFactory.setIndentation(int) method.

Attributes order

By default the order of the attributes in the DOM Document for each node will not be preserved from the input tree, but it will be an alphabetical order.

You can specify that you want the order of the attributes for each node to be preserved when creating the document by using the XMLDocumentBuilderFactory.keepAttributesOrder(boolean) method.

For example:
   XMLDocumentBuilderFactory factory =  XMLDocumentBuilderFactory.newInstance();
   factory.keepAttributesOrder(true); // specify that the order of attributes for each node will be preserved
   Document document = factory.parse(<the XML File>); 

Options used if creating the Document from a File, URL, or String

Main Article: XMLNodeUtilities2

These options are only used if the Document is created from a File, URL, or String:
  • It is possible to preserve spaces such as for the xml.space="&reserve& XML declaration (by default spaces are not preserved)
  • It is possible to keep the line numbers in the input XML file in the nodes in the result (by default the line numbers are removed)
  • It is possible to be silent about the warnings during the parsing (by default warnings are not shown)
  • It is possible to be silent about the errors during the parsing by default (by default exceptions are not shown)
  • It is possible to set the parser as validating (by default the parser is not validating)
  • It is possible to set the schema to use for validation
  • It is possible to keep the namespace informations during the parsing (by default the namespace informations are skipped)
For example:
   File file = ...
   XMLDocumentBuilderFactory factory = XMLDocumentBuilderFactory.newInstance();
   factory.setNamespaceAware(true);
   Document document = factory.parse(file); 

Using an ErrorHandler for the parsing

It is possible to set an ErrorHandler to be notified if a SAX exception is encountered when parsing the file, URL, or Stream.

The method is the method XMLDocumentBuilderFactory.setErrorHandler(ErrorHandler) method.

Using an EntityResolver for the parsing

Main Article: EntityListResolver

It is possible to set an EntityListResolver to manage entity resolution in XML files for the parsing.

The method is the method XMLDocumentBuilderFactory.setEntityListResolver(EntityListResolver) method.

Limitations

There are some limitations in the resulting Document:
  • The resulting DOM content can not be modified
  • All features will return null
  • The getSchemaTypeInfo and getDomConfig() methods are currently unsupported
  • The cloneNode method always return the same Node without cloning

The DocumentTraversal interface is supported in the resulting Document.

See also


Categories: packages | xml

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences