A sample DOM filter. This sample program illustrates how to
use the Document#getElementsByTagName() method to quickly
and easily locate elements by name.
Field Summary |
---|
protected static final String | NAMESPACES_FEATURE_ID | Namespaces feature id (http://xml.org/sax/features/namespaces). |
protected static final String | VALIDATION_FEATURE_ID | Validation feature id (http://xml.org/sax/features/validation). |
protected static final String | SCHEMA_VALIDATION_FEATURE_ID | Schema validation feature id (http://apache.org/xml/features/validation/schema). |
protected static final String | SCHEMA_FULL_CHECKING_FEATURE_ID | Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). |
protected static final String | HONOUR_ALL_SCHEMA_LOCATIONS_ID | Honour all schema locations feature id (http://apache.org/xml/features/honour-all-schemaLocations). |
protected static final String | VALIDATE_ANNOTATIONS_ID | Validate schema annotations feature id (http://apache.org/xml/features/validate-annotations). |
protected static final String | DYNAMIC_VALIDATION_FEATURE_ID | Dynamic validation feature id (http://apache.org/xml/features/validation/dynamic). |
protected static final String | XINCLUDE_FEATURE_ID | XInclude feature id (http://apache.org/xml/features/xinclude). |
protected static final String | XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID | XInclude fixup base URIs feature id (http://apache.org/xml/features/xinclude/fixup-base-uris). |
protected static final String | XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID | XInclude fixup language feature id (http://apache.org/xml/features/xinclude/fixup-language). |
protected static final String | DEFAULT_PARSER_NAME | Default parser name (dom.wrappers.Xerces). |
protected static final String | DEFAULT_ELEMENT_NAME | Default element name (*). |
protected static final boolean | DEFAULT_NAMESPACES | Default namespaces support (true). |
protected static final boolean | DEFAULT_VALIDATION | Default validation support (false). |
protected static final boolean | DEFAULT_SCHEMA_VALIDATION | Default Schema validation support (false). |
protected static final boolean | DEFAULT_SCHEMA_FULL_CHECKING | Default Schema full checking support (false). |
protected static final boolean | DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS | Default honour all schema locations (false). |
protected static final boolean | DEFAULT_VALIDATE_ANNOTATIONS | Default validate schema annotations (false). |
protected static final boolean | DEFAULT_DYNAMIC_VALIDATION | Default dynamic validation support (false). |
protected static final boolean | DEFAULT_XINCLUDE | Default XInclude processing support (false). |
protected static final boolean | DEFAULT_XINCLUDE_FIXUP_BASE_URIS | Default XInclude fixup base URIs support (true). |
protected static final boolean | DEFAULT_XINCLUDE_FIXUP_LANGUAGE | Default XInclude fixup language support (true). |
Method from dom.GetElementsByTagName Detail: |
public static void main(String[] argv) {
// is there anything to do?
if (argv.length == 0) {
printUsage();
System.exit(1);
}
// variables
PrintWriter out = new PrintWriter(System.out);
ParserWrapper parser = null;
String elementName = DEFAULT_ELEMENT_NAME;
String attributeName = null;
boolean namespaces = DEFAULT_NAMESPACES;
boolean validation = DEFAULT_VALIDATION;
boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION;
boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;
boolean honourAllSchemaLocations = DEFAULT_HONOUR_ALL_SCHEMA_LOCATIONS;
boolean validateAnnotations = DEFAULT_VALIDATE_ANNOTATIONS;
boolean dynamicValidation = DEFAULT_DYNAMIC_VALIDATION;
boolean xincludeProcessing = DEFAULT_XINCLUDE;
boolean xincludeFixupBaseURIs = DEFAULT_XINCLUDE_FIXUP_BASE_URIS;
boolean xincludeFixupLanguage = DEFAULT_XINCLUDE_FIXUP_LANGUAGE;
// process arguments
for (int i = 0; i < argv.length; i++) {
String arg = argv[i];
if (arg.startsWith("-")) {
String option = arg.substring(1);
if (option.equals("p")) {
// get parser name
if (++i == argv.length) {
System.err.println("error: Missing argument to -p option.");
}
String parserName = argv[i];
// create parser
try {
parser = (ParserWrapper)Class.forName(parserName).newInstance();
}
catch (Exception e) {
parser = null;
System.err.println("error: Unable to instantiate parser ("+parserName+")");
}
continue;
}
if (option.equals("e")) {
if (++i == argv.length) {
System.err.println("error: Missing argument to -e option.");
}
elementName = argv[i];
continue;
}
if (option.equals("a")) {
if (++i == argv.length) {
System.err.println("error: Missing argument to -a option.");
}
attributeName = argv[i];
continue;
}
if (option.equalsIgnoreCase("n")) {
namespaces = option.equals("n");
continue;
}
if (option.equalsIgnoreCase("v")) {
validation = option.equals("v");
continue;
}
if (option.equalsIgnoreCase("s")) {
schemaValidation = option.equals("s");
continue;
}
if (option.equalsIgnoreCase("f")) {
schemaFullChecking = option.equals("f");
continue;
}
if (option.equalsIgnoreCase("hs")) {
honourAllSchemaLocations = option.equals("hs");
continue;
}
if (option.equalsIgnoreCase("va")) {
validateAnnotations = option.equals("va");
continue;
}
if (option.equalsIgnoreCase("dv")) {
dynamicValidation = option.equals("dv");
continue;
}
if (option.equalsIgnoreCase("xi")) {
xincludeProcessing = option.equals("xi");
continue;
}
if (option.equalsIgnoreCase("xb")) {
xincludeFixupBaseURIs = option.equals("xb");
continue;
}
if (option.equalsIgnoreCase("xl")) {
xincludeFixupLanguage = option.equals("xl");
continue;
}
if (option.equals("h")) {
printUsage();
continue;
}
}
// use default parser?
if (parser == null) {
// create parser
try {
parser = (ParserWrapper)Class.forName(DEFAULT_PARSER_NAME).newInstance();
}
catch (Exception e) {
System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")");
continue;
}
}
// set parser features
try {
parser.setFeature(NAMESPACES_FEATURE_ID, namespaces);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+NAMESPACES_FEATURE_ID+")");
}
try {
parser.setFeature(VALIDATION_FEATURE_ID, validation);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")");
}
try {
parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")");
}
try {
parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")");
}
try {
parser.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+HONOUR_ALL_SCHEMA_LOCATIONS_ID+")");
}
try {
parser.setFeature(VALIDATE_ANNOTATIONS_ID, validateAnnotations);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+VALIDATE_ANNOTATIONS_ID+")");
}
try {
parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, dynamicValidation);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+DYNAMIC_VALIDATION_FEATURE_ID+")");
}
try {
parser.setFeature(XINCLUDE_FEATURE_ID, xincludeProcessing);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+XINCLUDE_FEATURE_ID+")");
}
try {
parser.setFeature(XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID, xincludeFixupBaseURIs);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+XINCLUDE_FIXUP_BASE_URIS_FEATURE_ID+")");
}
try {
parser.setFeature(XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID, xincludeFixupLanguage);
}
catch (SAXException e) {
System.err.println("warning: Parser does not support feature ("+XINCLUDE_FIXUP_LANGUAGE_FEATURE_ID+")");
}
// parse file
try {
Document document = parser.parse(arg);
GetElementsByTagName.print(out, document, elementName, attributeName);
}
catch (SAXParseException e) {
// ignore
}
catch (Exception e) {
System.err.println("error: Parse error occurred - "+e.getMessage());
if (e instanceof SAXException) {
Exception nested = ((SAXException)e).getException();
if (nested != null) {
e = nested;
}
}
e.printStackTrace(System.err);
}
}
}
Main program entry point. |
protected static String normalize(String s) {
StringBuffer str = new StringBuffer();
int len = (s != null) ? s.length() : 0;
for (int i = 0; i < len; i++) {
char ch = s.charAt(i);
switch (ch) {
case '< ": {
str.append("<");
break;
}
case ' >": {
str.append(">");
break;
}
case '&": {
str.append("&");
break;
}
case '"": {
str.append(""");
break;
}
case '\r":
case '\n": {
str.append("");
str.append(Integer.toString(ch));
str.append(';");
break;
}
default: {
str.append(ch);
}
}
}
return str.toString();
}
Normalizes the given string. |
protected static void print(PrintWriter out,
Element element,
NamedNodeMap attributes) {
out.print('< ");
out.print(element.getNodeName());
if (attributes != null) {
int attributeCount = attributes.getLength();
for (int i = 0; i < attributeCount; i++) {
Attr attribute = (Attr)attributes.item(i);
out.print(' ");
out.print(attribute.getNodeName());
out.print("=\"");
out.print(normalize(attribute.getNodeValue()));
out.print('"");
}
}
out.println(' >");
out.flush();
}
Prints the specified element. |
public static void print(PrintWriter out,
Document document,
String elementName,
String attributeName) {
//
// Public static methods
//
// get elements that match
NodeList elements = document.getElementsByTagName(elementName);
// is there anything to do?
if (elements == null) {
return;
}
// print all elements
if (attributeName == null) {
int elementCount = elements.getLength();
for (int i = 0; i < elementCount; i++) {
Element element = (Element)elements.item(i);
print(out, element, element.getAttributes());
}
}
// print elements with given attribute name
else {
int elementCount = elements.getLength();
for (int i = 0; i < elementCount; i++) {
Element element = (Element)elements.item(i);
NamedNodeMap attributes = element.getAttributes();
if (attributes.getNamedItem(attributeName) != null) {
print(out, element, attributes);
}
}
}
}
Prints the specified elements in the given document. |