Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Generate XML using JSP and containing Special Character

Posted on 2002-03-06
11
Medium Priority
?
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 390 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …
Suggested Courses

609 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