noone
noone

Reputation: 157

Display xml content in jsp page

Below is the body of jsp page i am using:

<body>
    <%


     String schema=(String)request.getAttribute("schema");
    if(schema!=null)
    {


    **out.println(schema);**
    }
    else
    out.println("no schema found");

    %>
    </body>

"out.println();" is not displaying the xml content in browser.

the content of shcmea is :

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://edd.att.com/cnmeddservice" targetNamespace="http://edd.att.com/cnmeddservice" elementFormDefault="qualified">
    <xsd:include schemaLocation="EDD_DataTypes.xsd"/>
    <xsd:include schemaLocation="EDD_Internal_DataTypes.xsd"/>
    <xsd:element name="RET_MAIL">
        <xsd:annotation>
            <xsd:documentation>Represents the eddbdsbatch request for RET_MAIL</xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="AccountNumber">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="15"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="cType">
                    <xsd:simpleType>
                        <xsd:restriction base="CtypeType">
                            <xsd:enumeration value="RET_MAIL"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="RequestId">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:integer">
                            <xsd:pattern value="[0-9]{1,9}"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="BillingId" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="12"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="BillingRegion" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:enumeration value="S"/>
                            <xsd:enumeration value="P"/>
                            <xsd:enumeration value="B"/>
                            <xsd:enumeration value="A"/>
                            <xsd:enumeration value="N"/>
                            <xsd:enumeration value="W"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="BillIndicator" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:enumeration value="LS"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="EmailAddress" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="75"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="AccountBalance" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="DecimalAmountType">
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="BillAmount" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="DecimalAmountType">
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="DateLastchecked" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:pattern value="((0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d) ((0[0-9]|1[0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]))"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="PaymentDueDate" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="DateType"/>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="ctn">
                    <xsd:simpleType>
                        <xsd:restriction base="ContactNumberType">
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="AlternateCTN" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="10"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="AltPhoneExtension" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="4"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="CustomerType">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:enumeration value="R"/>
                            <xsd:enumeration value="B"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="clientIndicator" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="ClientIndicatorType">
                        <xsd:enumeration value="Wireless_BDS"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="OverridePriorityString" use="optional">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="E"/>
                        <xsd:enumeration value="A"/>
                        <xsd:enumeration value="S"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="Languageid" use="optional">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="EN"/>
                        <xsd:enumeration value="ES"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Myconcern is how can i display the content of schema variable which is xml in my browser?

Upvotes: 3

Views: 11287

Answers (5)

Travis Heeter
Travis Heeter

Reputation: 14054

Try URLEncoder.encode.

You need this: <%@ page import="java.net.URLEncoder" %>

Then you can do: URLEncoder.encode(schema).


My situation was calling a jsp as an endpoint for a web application. I wanted to send http://host.com/getFile?path=path\text.xml and I expected the contents of text.xml to be returned. However I was getting blank spaces.

I later realized this is because stuff like < makes the jsp think the String is jsp instructions. Instead, by wrapping the text in URLEncoder.encode(), it treated it as text that needs to be printed.

Then on the js side, I used decodeURIComponent(val).replaceAll('+',' ') (because it wouldn't be java without being a total pain) to decode the message, then I get a string that I can display in a textarea or whatever.

Upvotes: 0

Rakesh
Rakesh

Reputation: 1

Let me tell you the scenario: I have one jsp page where I am searching the data and getting the XML value and other data in the same JSP page, when user select any row then xml value should be display in new window.... I am seeing that I am getting the XML value(I hidden it in the jsp page) in the page but when I pass it in the JavaScript function to open in the new page then it does not work.......

example of XML:

Note-->I have to display the XML as it is with all the Tag and schema information.

Hope it clears the scenario...

Upvotes: -2

Nirbhay Rana
Nirbhay Rana

Reputation: 4347

JSP automatically parse the XML. So you need to change the content type of the jsp. Please use this..<%@ page contentType="text/xml" %>

Upvotes: -2

NameNotFoundException
NameNotFoundException

Reputation: 896

Use

<%@ page contentType="text/xml" %>

in your jsp page,it won;t parse the XML & will display it as it is.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691725

Stop using scriptlets in your JSPs. Learn how to use the JSP EL and the JSTL.

Once that is done, use the JSTL <c:out> tag, which escapes special characters like <, >, &, " and ' so that they appear correctly in the page and are not interpreted as incorrect HTML markup by the browser. Your code will simply become:

<body>
    <c:out value="${schema != null ? schema : 'no schema found'}"/>
</body>

Upvotes: 5

Related Questions