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.
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.
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
getDomError Prints parsing error. Parameters are: 1) id Object ID for DOM
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
existsNode Body tag, executes body if node exists. Parameters are: 1) id Object ID for DOM
getNodeText Prints node value or saves it in the page scope variable (defined by parameter into). Parameters are: 1) id Object ID for DOM
setNodeText Updates node value. Parameters are: 1) id Object ID for DOM
existsNodeAttribute Body tag, executes body if attribute exists. Parameters are: 1) id Object ID for DOM
getNodeAttribute Prints attribute value or saves it in the page scope variable (defined by parameter into). Parameters are: 1) id Object ID for DOM
setNodeAttribute Updates attribute value. Parameters are: 1) id Object ID for DOM
getNodeName Prints node name. Parameters are: 1) id Object ID for DOM
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
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
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:
|