The getAllowedChildren() Method
The
getAllowedChildren() method returns a list of elements allowed in an element, providing another way to determine whether adding an element would be a valid action. As an example, here's how to output the elements which can be added to the
article element.
XMLElement articleNode = (XMLElement)
(document.selectSingleNode(
"catalog/journal/article"));
ElementEditVAL articleVAL = (ElementEditVAL)
(articleNode);
elementList=articleVAL.getAllowedChildren();
After running the preceding code, you'll find that the
elementList return value contains two elements:
author and
title.
The getContentType() Method
The
getContentType() method returns the content type an element is allowed to have. For example, this code retrieves the valid content type for an
article element:
short contentType=articleVAL.getContentType();
If the value of the short variable returned by the
getContentType() method is
VAL_ANY_CONTENTTYPE, the element content is element, processing instruction, unexpanded entity reference, character, and comment information items.
The return value is a
short that represents the valid content type as shown in Table 2.
Table 2. Content Types: The table shows valid content type short values.
| Short Value |
Valid Element Content Type |
| VAL_ELEMENTS_CONTENTTYPE |
A sequence of elements |
| VAL_EMPTY_CONTENTTYPE |
Empty |
| VAL_MIXED_CONTENTTYPE |
A sequence of ordered elements with optional character data |
| VAL_SIMPLE_CONTENTTYPE |
Character data |
For the
article element,
getContentType() returns
VAL_ELEMENTS_CONTENTTYPEin other words, the
article element can contain a sequence of elements.
The
nodeValidity() method checks the validity of a node. As an example, checking the validity of an article node with the following code returns
VALID_TRUE.
short nodeValid = articleVAL.nodeValidity(
ElementEditVAL.VAL_SCHEMA);
The getAllowedAttributes() Method
The
getAllowedAttributes() method returns a list of allowed attributes for an element. For example, to discover the allowable attributes for the
catalog element, you can write:
NameList attr = elementEditVAL.getAllowedAttributes();
After running the preceding validation code, the
attr NameList contains two attributes:
title and
publisher.
The getRequiredAttributes() Method
The
getRequiredAttributes() method returns a list of required attributes for an element, letting you know in advance which attributes you'll need to add to an element to insert or append it into a valid document. In an XML schema, you specify a required attribute using the
use="required" syntax. For example, to get the required attributes in the
catalog element, you can use:
NameList attrRequired =
elementEditVAL.getRequiredAttributes();
After running the preceding line of code, for the sample documents, the
attrRequired NameList would contain
publisher, because that's the only
catalog element attribute specified with
use="required" in the sample schema.
The canSetAttribute() Method
The
canSetAttribute() method checks the validity of adding an attribute to an element. As an example, try adding a
section attribute to the
catalog element.
short attrSet = elementEditVAL.canSetAttribute(
"section", "SQL");
Because the
catalog.xsd schema does not define a
section attribute for the
catalog element the result is
VALID_FALSE.
The canRemoveAttribute() Method
The
canRemoveAttribute() checks the validity of removing an attribute from an element. As an example, try removing the
publisher element from the
catalog element. The
publisher attribute is a required attribute of element
catalog, so the return value is
VALID_FALSE.
The methods you've seen in this article cover DOM Level 3.0 validation, letting you test whether you can add or remove elements and attributes from an XML document according to a given schema. The
downloadable example validation program,
DOM3Validation.java works with the two sample
catalog.xml and
catalog.xsd files to illustrate the methods. After you download the code, copy the
DOM3Validation.java file to the same directory as
catalog.xml.