About us
Contact us
Products
Services
Articles
 Twitter    Bookmark and Share

XML Processing JSP Actions - Code Generator

JSP Tag Library for Fast and Easy XML Parsing

Devsphere XML JSP Tag Library (XJTL) comes with a code builder that takes a sample XML document and generates a JSP page that parses the sample XML content. You may edit the generated JSP adding the code that processes the XML information according to the requirements of your application.

Syntax

java com.devsphere.xml.taglib.process.builder.Main
    -sample  XML_file_path
    -output  JSP_file_path
   [-varXml  variable_name]
   [-systemId  XML_system_identifier]
   [-validate]
   [-ignoreSpaces]
   [-rootElements  element_name_1  element_name_2  ...]

where

  • XML_file_path is the file path of the sample XML file
  • JSP_file_path is the file path of the JSP that will be generated
  • variable_name is the name of the JSP variable that holds the XML input source that must be processed
  • XML_system_identifier is the ID of the XML source that the application wants to process
  • validate indicates that the XML source must be validated
  • ignoreSpaces indicates that the space character data that indents the elements must be ignored
  • element_name_1 element_name_2 ... are the names of the root elements of the XML fragments that the application wants to get as DOM trees rather than SAX events.

Example 1

The XJTLParse.jsp and XJTLParse2.jsp examples are derived from the following generated code. The two examples are explained in a different page of this documentation. The generated code contains the SAX events as comments so that you can replace them with code that processes the XML information exported as JSP variables. The varName="name", varAttr="attr" and varData="data" attributes specify the default names of the variables exported by the XJTL actions. You can rename those variables. If each variable has a unique name, the XML information is kept within the JSP page context for later processing.

Command Line:

java com.devsphere.xml.taglib.process.builder.Main 
-sample xjtldemo\process\sample.xml -systemId sample.xml 
-ignoreSpaces -output xjtldemo\builder\processSAX.jsp 

Generated Code:

<%@ page language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="p" uri="http://devsphere.com/xml/taglib/process" %>

<p:parse systemId="sample.xml" validate="false" ignoreSpaces="true">
    <p:start>
        <%-- startDocument --%>
    </p:start>
    <p:element varName="name" varAttr="attr" testName="person">
        <p:start>
            <%-- startElement name="person", attr.id="js890" --%>
        </p:start>
        <p:element varName="name" varAttr="attr" testName="name">
            <p:start>
                <%-- startElement name="name" --%>
            </p:start>
            <p:data varData="data">
                <%-- characters data="John Smith" --%>
            </p:data>
            <p:end>
                <%-- endElement name="name" --%>
            </p:end>
        </p:element>
        <p:element varName="name" varAttr="attr" testName="email">
            <p:start>
                <%-- startElement name="email" --%>
            </p:start>
            <p:data varData="data">
                <%-- characters data="John.Smith@yahoo.com" --%>
            </p:data>
            <p:end>
                <%-- endElement name="email" --%>
            </p:end>
        </p:element>
        <p:element varName="name" varAttr="attr" testName="phone">
            <p:start>
                <%-- startElement name="phone" --%>
            </p:start>
            <p:data varData="data">
                <%-- characters data="650-123-4567" --%>
            </p:data>
            <p:end>
                <%-- endElement name="phone" --%>
            </p:end>
        </p:element>
        <p:element varName="name" varAttr="attr" testName="address">
            <p:start>
                <%-- startElement name="address", attr.city="Palo Alto", 
                    attr.state="CA", attr.zip="94303", attr.country="USA" --%>
            </p:start>
            <p:element varName="name" varAttr="attr" testName="line1">
                <p:start>
                    <%-- startElement name="line1" --%>
                </p:start>
                <p:data varData="data">
                    <%-- characters data="JS Information Systems, Inc." --%>
                </p:data>
                <p:end>
                    <%-- endElement name="line1" --%>
                </p:end>
            </p:element>
            <p:element varName="name" varAttr="attr" testName="line2">
                <p:start>
                    <%-- startElement name="line2" --%>
                </p:start>
                <p:data varData="data">
                    <%-- characters data="1001 San Antonio Road" --%>
                </p:data>
                <p:end>
                    <%-- endElement name="line2" --%>
                </p:end>
            </p:element>
            <p:end>
                <%-- endElement name="address" --%>
            </p:end>
        </p:element>
        <p:end>
            <%-- endElement name="person" --%>
        </p:end>
    </p:element>
    <p:end>
        <%-- endDocument --%>
    </p:end>
</p:parse>

Example 2

This example shows you how to use XJTL to build a DOM tree. The generated code contains a list of valid XPaths within a comment. These XPaths could be used with JSTL actions, for example.

Command Line:

java com.devsphere.xml.taglib.process.builder.Main 
-sample xjtldemo\process\sample.xml -systemId sample.xml 
-ignoreSpaces -output xjtldemo\builder\processDOM.jsp 
-rootElements person

Generated Code:

<%@ page language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="p" uri="http://devsphere.com/xml/taglib/process" %>

<p:parse systemId="sample.xml" validate="false" ignoreSpaces="true">
    <p:start>
        <%-- startDocument --%>
    </p:start>
    <p:element varName="name" varAttr="attr" varDom="dom" testName="person">
        <%-- dom="..." name="person", attr.id="js890" --%>
        <%-- XPath expressions:
            $dom
                string($dom/@id)
                $dom/name
                    string($dom/name/text())
                $dom/email
                    string($dom/email/text())
                $dom/phone
                    string($dom/phone/text())
                $dom/address
                    string($dom/address/@city)
                    string($dom/address/@state)
                    string($dom/address/@zip)
                    string($dom/address/@country)
                    $dom/address/line1
                        string($dom/address/line1/text())
                    $dom/address/line2
                        string($dom/address/line2/text())
        --%>
    </p:element>
    <p:end>
        <%-- endDocument --%>
    </p:end>
</p:parse>

Example 3

This example mixes the SAX and DOM parsing methods during the processing of an XML document. Much of the code is based on SAX, but two XML fragments, which have email and address as root elements, are obtained as DOM trees.

Command Line:

java com.devsphere.xml.taglib.process.builder.Main 
-sample xjtldemo\process\sample.xml -systemId sample.xml 
-ignoreSpaces -output xjtldemo\builder\processSAXDOMIX.jsp 
-rootElements email address

Generated Code:

<%@ page language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="p" uri="http://devsphere.com/xml/taglib/process" %>

<p:parse systemId="sample.xml" validate="false" ignoreSpaces="true">
    <p:start>
        <%-- startDocument --%>
    </p:start>
    <p:element varName="name" varAttr="attr" testName="person">
        <p:start>
            <%-- startElement name="person", attr.id="js890" --%>
        </p:start>
        <p:element varName="name" varAttr="attr" testName="name">
            <p:start>
                <%-- startElement name="name" --%>
            </p:start>
            <p:data varData="data">
                <%-- characters data="John Smith" --%>
            </p:data>
            <p:end>
                <%-- endElement name="name" --%>
            </p:end>
        </p:element>
        <p:element varName="name" varAttr="attr" varDom="dom" testName="email">
            <%-- dom="..." name="email" --%>
            <%-- XPath expressions:
                $dom
                    string($dom/text())
            --%>
        </p:element>
        <p:element varName="name" varAttr="attr" testName="phone">
            <p:start>
                <%-- startElement name="phone" --%>
            </p:start>
            <p:data varData="data">
                <%-- characters data="650-123-4567" --%>
            </p:data>
            <p:end>
                <%-- endElement name="phone" --%>
            </p:end>
        </p:element>
        <p:element varName="name" varAttr="attr" varDom="dom" testName="address">
            <%-- dom="..." name="address", attr.city="Palo Alto", attr.state="CA", 
                attr.zip="94303", attr.country="USA" --%>
            <%-- XPath expressions:
                $dom
                    string($dom/@city)
                    string($dom/@state)
                    string($dom/@zip)
                    string($dom/@country)
                    $dom/line1
                        string($dom/line1/text())
                    $dom/line2
                        string($dom/line2/text())
            --%>
        </p:element>
        <p:end>
            <%-- endElement name="person" --%>
        </p:end>
    </p:element>
    <p:end>
        <%-- endDocument --%>
    </p:end>
</p:parse>

Copyright © 2000-2009 Devsphere

About us
Contact us
Products
Services
Articles