Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

XMLNodeUtilities



The XMLNodeUtilities class contains various utilities concerning XML content.

This class has only static methods. It is useful if you want to create roots for several XML files, but use the same configuration for all of them. If you want a more controlled configuration for the underlying parser, you may want to use the XMLNodeUtilities2 class, which has instance methods.


It allows to:
  • Create a tree of XMLNode from a File, a URL, or a String
  • Print the content of a XML tree
  • Search for nodes in a XMLNodes tree, allowing to filter the search

Creating an XML tree

Several methods allow to create an XML tree, with an XMLRoot or an XMLNode at the top of the tree.

It is possible to customize the way the XML content is parsed to create the tree:
  • 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 keep the namespace informations during the parsing (by default the namespace informations are skipped)
  • It is possible to allow internal readers creation in this parser instance (by default they are not allowed)
This class makes use of the XMLTreeHandler class internally to parse the content.

These methods create an XMLRoot at the top of the tree, but it is also possible to create an XMLNode instead, which can be useful to include the content in anther XML tree.

Creating an XML tree with an XMLRoot at the root of the tree

Several methods allow to create an XML tree, with an XMLRoot at the top of the tree.

The most simple are:

Creating an XML tree with an XMLNode at the root of the tree

Several methods allow to create an XML tree, with an XMLNode at the top of the tree. The most simple are:

Configuring the parsing

Several properties allows to configure the parsing:
  • Show the exceptions encountered during the parsing
  • Show the warnings encountered during the parsing
  • Preserve the space in the CDATA content
  • Keep the line numbers
  • Keep the namespace informations
  • Allow internal readers creation in this parser instance
The most straightforward way to configure these properties is to add an int option to the methods, such as: Note that the properties is an AND of the following values: For example:
   XMLRoot root = XMLNodeUtilities.getNode(xmlURL, XMLNodeUtilitiesOptions.KEEP_LINE_NUMBERS & XMLNodeUtilitiesOptions.NAMESPACE_AWARE);

Preserving spaces

Main Article: preserving space

The XMLNodeUtilities class has methods which will preserve the spaces when getting the root node from an URL, a File, a Reader, or a String. Some of these methods allow to preserve spaces in CDATA content. For example in this example here:
<root desc="example">
  <element name="first">the first line
   the second line
  </element>
</root>
If you perform XMLNodeUtilities.getRootNode(<the XML file>, false, false, true, false), the spaces in CDATA content will be preserved.

Keeping line numbers

Main Article: keeping line numbers

The XMLNodeUtilities class has methods which will keep the line numbers for the nodes. The resulting nodes NumberedNode.getLineNumber() method will return the associated line number in the XML file.

If you perform XMLNodeUtilities.getRootNode(<the XML file>, false, false, false, true), the line numbers will be preserved.

Prevent the internal creation of readers

Main Article: XMLSAXParser

Contrary to the XMLSAXParser, by default this class will NOT try to create readers through theFiles.newBufferedReader(path, charset) method.

Configuration of the parser

It is possible to configure the parsing by using the XMLParserConfiguration configuration class through one of the following methods:

Keeping the namespace informations

It is possible to keep the namespace informations during the parsing.

Using an ErrorHandler for the parsing

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

The method is the static method XMLNodeUtilities.setErrorHandler(ErrorHandler) method.

Adding features or properties for the parsing

It is possible to configure the XMLNodeUtilities class to add SAX factory features or parser properties: The method is the static method XMLNodeUtilities.setErrorHandler(ErrorHandler) method.

Printing the content of an XML tree

Several methods allows to print the content of an XML tree, either in a File, URL, or String:

Searching for children elements

Several method allow to serarch for children elemetns which have a spzecified node name or qualified node name:

For example, suppose the following XML file:
<root desc="example">
  <element1 name="first">
    <element2 name="second"/>
    <element1 name="third"/>
  </element1>
</root>
If we apply:
   XMLRoot root = XMLNodeUtilities.getRootNode(<the XML file>);
   List<XMLNode> nodes = XMLNodeUtilities.search(root, "element1", false);
The nodes result will return only one element because the search only look for direct children of the node:
  • The "first" node


If we apply:
   XMLRoot root = XMLNodeUtilities.getRootNode(<the XML file>);
   List<XMLNode> nodes = XMLNodeUtilities.search(root, "element1", true);
The nodes result will return two elements because the search look recursively for all children:
  • The "first" node
  • The "third" node

Searching for children elements among several possible node names

It is also possible to search for several possible node names or qualified node names. For example:

For example, suppose the following XML file:
<root desc="example">
   <element2 name="first"/>
   <element1 name="second"/>
   <element3 name="third"/>
</root>
If we apply:
   XMLRoot root = XMLNodeUtilities.getRootNode(<the XML file>);
   List<String> list = new ArrayList<>;
   list.add("element1");
   list.add("element3");
   List<XMLNode> nodes = XMLNodeUtilities.searchForNames(root, list, false);
The nodes result will return two elements:
  • The "first" node
  • The "third" node

Searching for the first child element

The XMLNodeUtilities.searchFirst(XMLNode, String, boolean) method allows to search for the first child node which has a specific name under a root node.

For example, suppose the following XML file:
<root desc="example">
  <element1 name="first">
    <element2 name="second"/>
    <element1 name="third"/>
  </element1>
</root>
If we apply:
   XMLRoot root = XMLNodeUtilities.getRootNode(<the XML file>);
   XMLNode node = XMLNodeUtilities.searchFirst(root, "element1", false);
The node result will return:
  • The "first" node

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