Using Java SAX XML writer to construct an XML file with elements containing HTML tags

Posted on 2008-09-29
Last Modified: 2012-05-05
Hi Experts!

I'm using Java XML SAX writer to write an XML file from a map object containing some data. The can get it create the file just fine. However some data elements contain HTML syntax which I need to preserve. SAX writer for some reason is re-processing the HTML tags when reading in the data e.g.

<p>One, two! One, two! And through and through<br />&nbsp;


&lt;p&gt;One, two! One, two! And through and through&lt;br /&gt;&amp;nbsp;

Basically I don't want the writer to modify HTML tags at all, I just want it to include them in the resulting XML file as is. Is there anyway to stop this from happening?

Question by:vlad_oz
  • 5
  • 5
LVL 59

Expert Comment

by:Kevin Cross
ID: 22602684
You can try setting xmlns="" to indicate tags without any namespace should be treated as XHTML.  You can then prefix all other XML tags with another namespace to distinguish if needed.

Author Comment

ID: 22602706
Thanks for the quick response! Could you please provide a small example of what you mean.
LVL 59

Expert Comment

by:Kevin Cross
ID: 22602733
Please post what you have so far and I will post in what I am talking about in context of your code so it is clear.

Author Comment

ID: 22602767
here is the method responsible for creating XML elements
private void writeSimpleProp(ContentHandler handler, String name, String value)

			throws SAXException



		String nodeName;

		AttributesImpl attrs = null;

		if (validName(name)) {

			nodeName = name;

		} else {

			nodeName = "prop";

			attrs = new AttributesImpl();

			attrs.addAttribute(nsURI ,localName, "name", "CDATA", name);


		handler.startElement(nsURI, localName, nodeName, attrs);

		char[] str = String.valueOf(value).toCharArray();

                handler.characters(str, 0, str.length);

		handler.endElement(nsURI, localName, nodeName);


Open in new window


Author Comment

ID: 22602879
Also, I'm open to other Java XML writers, doesn't have to be SAX
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 59

Expert Comment

by:Kevin Cross
ID: 22606240
So your HTML is coming through the Value attribute?
LVL 59

Expert Comment

by:Kevin Cross
ID: 22606243
Sorry, I meant to say value parameter of the method.

Author Comment

ID: 22610187
Yes, the HTML is contained within the value parameter, basically its a String.

I'm experimenting with DOM4J XML parser at the moment, seems to be a lot easier to use then SAX. So far I've got the HTML being written into the XML file as is within CDATA elements. E.g.:

<additionalInformation><![CDATA[<p>One, two! One, two! And through and through<br />&nbsp; The vorpal blade went snicker-snack!<br />He left it dead, and with its head<br />&nbsp; He went galumphing back.</p>]]></additionalInformation>

This probably will be acceptable as the XML reader shouldn't care if the element is CDATA or not. Still, would have been good to know if it was possible to avoid using CDATA.
LVL 59

Accepted Solution

Kevin Cross earned 500 total points
ID: 22610220
Think so, but would have to parse the string value into its own XML element/node and then add that into your XML.

Since it is textnode right now, it gets treated like other text; however, if you could parse your text and find the nodes (i.e. any thing between <?> and add to your XML then add text and other HTML data to that).  Not sure if I am explaining it correctly, but sounds like you have a good alternative.


Author Comment

ID: 22611905
Hey Kevin,

I think I'll go with DOM4J for now, I know what you meant by text for html element nodes, but hoping to avoid doing it.
Thanks for your help!


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse formatting 6 73
Transform XML to Excel using XSL 1 23
javap bin 2 29
eclipse apache tomcat admin console 52 66
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

867 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

12 Experts available now in Live!

Get 1:1 Help Now