Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

XMLRootDetector



The XMLRootDetector class allows to get the root element of an XML file or only its name. Note that this class uses a Stax parser, which means that it will stop the parsing as soon as this element has been encountered.

It is possible to accept not well-formed XML to get the root node or the root node name.

Getting the root node name

The XMLRootDetector.getRootName(File) and XMLRootDetector.getQualifiedRootName(File) methods allow to get the root node name or qualified name. If the XML file is not well-formed, the result will be null.

For example:
  XMLRootDetector rootDetector = new XMLRootDetector();
  String name = rootDetector.getRootName(<the XML file>);

Accepting not well-formed XML files

The XMLRootDetector.getRootName(File, boolean) and XMLRootDetector.getQualifiedRootName(File, boolean) methods allow to get the root node name or qualified name even in the case of a not well-formed XML file.

For example:
  XMLRootDetector rootDetector = new XMLRootDetector();
  String name = rootDetector.getRootName(<the XML file>, true);

Note that in the case of a file which is not even an XML file, or not a text file, the result will still be null.

Getting the root node

The XMLRootDetector.getRoot(File) method allow to get the root node. If the XML file is not well-formed, the result will be null.

The XML root element is a XMLNode, but the parent-child structure will not be kept (only the root node will be returned, excluding its children), contrary to the XMLNodeUtilities class.

For example:
   XMLRootDetector rootDetector = new XMLRootDetector();
   XMLNode node = rootDetector.getRoot(<the XML file>);

Accepting not well-formed XML files

The XMLRootDetector.getRoot(File, boolean) method allow to get the root node even in the case of a not well-formed XML file. The attributes which have been detected will be there.

For example:
  XMLRootDetector rootDetector = new XMLRootDetector();
  XMLNode node = rootDetector.getRoot(<the XML file>, true);

Note that in the case of a file which is not even an XML file, or not a text file, the result will still be null.

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