Solved

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

Posted on 2008-09-29
10
2,860 Views
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;

becomes:

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

Thanks!
0
Comment
Question by:vlad_oz
  • 5
  • 5
10 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22602684
You can try setting xmlns="http://www.w3.org/1999/xhtml" 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.
0
 

Author Comment

by:vlad_oz
ID: 22602706
mwvisa1,
Thanks for the quick response! Could you please provide a small example of what you mean.
0
 
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.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:vlad_oz
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

0
 

Author Comment

by:vlad_oz
ID: 22602879
Also, I'm open to other Java XML writers, doesn't have to be SAX
0
 
LVL 59

Expert Comment

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

Expert Comment

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

Author Comment

by:vlad_oz
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.
0
 
LVL 59

Accepted Solution

by:
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.

Regards,
Kevin
0
 

Author Comment

by:vlad_oz
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!

Cheers,
Vlad
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 40
windows explorer path to command prompt 5 44
MySQL  on Tomcat 8 45
Adding multiple JVM environments to RedHat 6 7 21
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

773 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