JSP custom taglib. This library presents XML DOM parser and XPath similar requests to XML documents. You may parse XML document, save DOM into some variable and use this variable for getting values for nodes and attributes.
For example:
1. Parse XML string and save DOM in the session scope.
2. Parse XML file
3. Parse body
Now you may request data from XML source. When you request data you have to describe id
for parsed source (and optionally a scope) as well as a path for XML node.
Path here is just a sequence on names, separated with /. E.g.: node/sub_node1/sub_node2 etc.
Root element will be marked as /. Node path may points to several nodes. In this case you have to provide an index (position) for node
you are looking for.
You may use also a loop over nodes. In this case you do need to provide parameters for information tags, each of them will belong to the current node.
Let we see more examples:
4. Read data for above mentioned example
5. Update data
6. How many nodes ?
7. Loop over nodes
8. Print (output, save) tree
9. Direct access to DOM tree
This scriptlet demonstrates how you can access to DOM directly.
Suppose your document was parsed by this way:
now you may use page scope variable A for access to DOM:
Tags are:
domParse
Body tag. Tag parses XML source and saves results in the defined scope. You may set XML source as one of parameters (xmlString or xmlData) or as a body of this tag. Parameters are:
1) xmlString XML source as a string.
2) xmlData XML source as a some URL (file).
3) id Object ID for DOM
4) scope scope for parsed DOM object (page, request, session or application).
Default value is page.
5) ignoreDTD Optional parameter. Possible values are
true or false. Lets you ignore external DTD during the parsing.
Default value is false.
domError
Body tag. Executes own body in case of any errors detected in the given XML source. Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
Parameters are:
getDomError
Prints parsing error. Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
You may use this tag without parameters within domError body
forEachNode
Body tag, executes own body for each node in the path. Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
3) path path for XML node.
4) position Optional parameter: index for XML node.
existsNode
Body tag, executes body if node exists. Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
3) path path for XML node.
4) position Optional parameter: index for XML node.
getNodeText
Prints node value or saves it in the page scope variable (defined by parameter into). Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
3) path path for XML node.
4) position Optional parameter: index for XML node.
6) into Optional parameter. Defines name for page scope variable.
setNodeText
Updates node value. Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
3) path path for XML node.
4) position Optional parameter: index for XML node.
5) value new value.
existsNodeAttribute
Body tag, executes body if attribute exists. Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
3) path path for XML node.
4) position Optional parameter: index for XML node.
5) name name for attribute.
getNodeAttribute
Prints attribute value or saves it in the page scope variable (defined by parameter into). Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
3) path path for XML node.
4) position Optional parameter: index for XML node.
5) name name for attribute.
6) into Optional parameter. Defines name for page scope variable.
setNodeAttribute
Updates attribute value. Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
3) path path for XML node.
4) position Optional parameter: index for XML node.
5) name name for attribute.
6) value new value.
getNodeName
Prints node name. Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
3) path path for XML node.
4) position Optional parameter: index for XML node.
getNodeCount
Counts sub-nodes. Prints this value or saves it in the page scope variable (defined by parameter into). Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
3) path path for XML node.
4) into Optional parameter. Defines name for page scope variable.
printTree
Prints tree or saves it in the page scope variable (type is java.lang.Sting) defined by parameter into. Parameters are:
1) id Object ID for DOM
2) scope scope for DOM object. Default value is page.
4) into Optional parameter. Defines name for page scope variable.
Notes:
1. Taglib uses JAXP for dealing with XML documents.
for downloading:
Library: domtag.jar Description: taglib.tld
© Coldbeans Software Comments?
See also Coldtags suite - the largest collection of custom JSP tags.