Solved

Generate XML using JSP and containing Special Character

Posted on 2002-03-06
11
313 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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
 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 99
GUI builder for Eclipse? 8 36
Java SE 8u111  Lot of stuff broke 11 69
javap not working 8 46
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 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