DOM taglib ver. 2.1

    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.

 


<%@ taglib uri="taglib.tld" prefix="xml" %>
<html>
<%
   String s="<userslist><user><name>John</name><name admin='yes'>Sam</name>" +
    "</user><user><name>Bill</name></user></userslist>";
%>

<xml:domParse xmlString="<%=s%>" id="A" scope="session"/>
<!-- print error -->
<xml:domError id="A">
  <br>Parsing error: <xml:getDomError/>
</xml:domError>
...
</html>

2. Parse XML file
 


<%@ taglib uri="taglib.tld" prefix="xml" %>
<html>

<xml:domParse xmlData="c:/data/myfile.xml" id="A" scope="page"/>
<!-- print error -->
<xml:domError id="A">
  <br>Parsing error: <xml:getDomError/>
</xml:domError>
...
</html>

3. Parse body
 


<%@ taglib uri="taglib.tld" prefix="xml" %>
<html>

<xml:domParse id="A" scope="page">
  <some_xml_data>
    ...
  </some_xml_data>
</xml:domParse>

<xml:domError id="A">
  <br>Parsing error: <xml:getDomError/>
</xml:domError>
...
</html>

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
 


<xml:getNodeText id="A" path="/user/name" position="2"/>

<xml:getNodeAttribute id="A" path="/user/name" name="admin" position="2"/>

5. Update data
 


<xml:setNodeText id="A" path="/user/name" position="2" value="John" />

6. How many nodes ?
 


<xml:getNodeCount id="A" path="/user" into="B"/>

Count is <%=B.intValue()%>

7. Loop over nodes
 


<xml:forEachNode id="A" path="/user">
  Node name is:<xml:getNodeName/>
  Node value is:<xml:getNodeText/>
</xml:forEachNode>

8. Print (output, save) tree
 


<xml:printTree id="A"/>

9. Direct access to DOM tree

This scriptlet demonstrates how you can access to DOM directly. Suppose your document was parsed by this way:
 


<xml:domParse id="A" xmlData="your_data.xml" scope="page"/>

now you may use page scope variable A for access to DOM:
 


<%
com.cj.dom.domObject o = (com.cj.dom.domObject) pageContext.getAttribute("A", pageContext.PAGE_SCOPE );
org.w3c.dom.Document doc = o.getDoc();
// your Java code ...
%>

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.
5) into Optional parameter. Defines a name for page scope variable. Without this attribute tag simply prints a data.

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 a 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.
5) into Optional parameter. Defines a name for page scope variable. Without this attribute tag simply prints a data.

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 a 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.
3) into Optional parameter. Defines a 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.

Also in Coldtags: