?
Solved

Generate XML using JSP and containing Special Character

Posted on 2002-03-06
11
Medium Priority
?
329 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 …
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 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