Solved

Generate XML using JSP and containing Special Character

Posted on 2002-03-06
11
292 Views
Last Modified: 2008-03-10
Hi Experts,

How to generate an XML below using JSP? and the client will get the XML when they open it using browser?
It might be too simple, but occasionally the companyName contain single apostrophe character that must be
changed into &#x27 or contain any other special character that must be converted into Hexa, if not the XML will get error. Are there any function that will automatic convert the special character?

XML Example:
------------
<Order orderid='AP001' date='25-02-02' CompanyName='Bon app&#x27;'>
     <Detail ProductCode='BL001" Quantity='12'/>
     <Detail ProductCode='BL002" Quantity='9'/>
</Order>
0
Comment
Question by:falconew
  • 5
  • 3
  • 3
11 Comments
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6846609
<%@ page contentType="text/xml;charset=ASCII" %>

<%
out.println("<Order orderid='AP001' date='25-02-02' CompanyName='Bon appetit'>");
out.println("  <Detail ProductCode='BL001" Quantity='12'/>");
out.println<Detail ProductCode='BL002" Quantity='9'/>");
out.println("</Order>");
%>

-------------------------------------------------

The magic is in the first line. The charset declration should cause the escaping of the non-ASCII characters - if you get ?, then you will have to di it manually.  The contentType="text/xml" tells the browser to expect XML.
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 130 total points
ID: 6846743
you can use xerces ( http://xml.apache.org/xerces2-j/ )

this is an example code:

org.w3c.dom.Document doc = new org.apache.xerces.dom.DocumentImpl();
org.w3c.dom.Element el =  doc.createElement("Order");
el.setAttribute("test", "a&b");
org.w3c.dom.Element node = doc.createElement("Detail");
node.setAttribute("aa", "a&v");
el.appendChild(node);
node = doc.createElement("Detail");
node.setAttribute("aa", "as&");
el.appendChild(el);

org.apache.xml.serialize.XMLSerializer ss = new org.apache.xml.serialize.XMLSerializer();
ss.setOutputByteStream(System.out);
ss.serialize(doc);

(check how the & is escaped)
0
 
LVL 1

Author Comment

by:falconew
ID: 6846766
I mean when in the company name there has a single apostrophe between a pair of single apostrophe, eg. companyname='Bon App''. This will cause an error when I parse the XML. So there must have a method or function which can convert the ' (single apostrophe) into Hex &#x27;

Regards,
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6848019
The org.apache.xml.serialize.XMLSerializer will escape any characters that may break an XML document.
0
 
LVL 1

Author Comment

by:falconew
ID: 6849255
Venci,
Afterward how to generate the XML to the browser? I mean when someone request my JSP page, he will get the XML..

monkeyboy,
I would appreciate if you could give a sample how to generate it to JSP using org.apache.xml.serialize.XMLSerializer. Because I'm novice here.

Regards,
Falconew
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6849287
you can follow Venci's example, but change:

ss.setOutputByteStream(System.out);

to

ss.setOutputByteStream(out);

0
 
LVL 1

Author Comment

by:falconew
ID: 6849582
hi monkeyboy,

I got this error "Incompatible type for method. Can't convert javax.servlet.jsp.JspWriter to java.io.OutputStream. ss.setOutputByteStream(out);"

                                       
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6849876
Ok - then try to get the XML document as string:
...
java.io.ByteArrayOutputStream doc_stream = new java.io.ByteArrayOutputStream();
ss.setOutputByteStream(doc_stream);
ss.serialize(doc);
String doc_text = doc_stream.toString();
System.out.println(doc_text);

and now you can send back the doc_text to the browser:
<%= doc_text %>
0
 
LVL 1

Author Comment

by:falconew
ID: 6855397
Hi Venci,

It seems to work, but It doesn't generate the XML. What is wrong? It doesn't show up on the browser.

Regards,
Falconew
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6855554
may be you should set the correct content type for the response:
response.setContentType("text/xml");
0
 
LVL 1

Author Comment

by:falconew
ID: 6855993
I have figured out.. you forget the line...
Here it is I correct your code.
Thanks for all of you.

org.w3c.dom.Document doc = new org.apache.xerces.dom.DocumentImpl();
org.w3c.dom.Element el =  doc.createElement("Order");
doc.appendChild(el);
el.setAttribute("test", "a&b");
org.w3c.dom.Element node = doc.createElement("Detail");
node.setAttribute("aa", "a&v");
el.appendChild(node);
node = doc.createElement("Detail");
node.setAttribute("aa", "as&");
el.appendChild(node);
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now