Solved

Generate XML using JSP and containing Special Character

Posted on 2002-03-06
11
322 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
Independent Software Vendors: 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaFX TableView not displaying correctly 3 100
Java Eclipse Loop 3 28
Setup GlassFish 4 34
passing enum to a method 4 34
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

679 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