Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Carriage Returns and Tabs in XML element

843834Apr 27 2006 — edited Apr 29 2006
I have a number of carriage returns and tabs within an xml element. When I take that element I would like it to transform to html in which I am then displaying within a JEditorPane. However, when I do this it is one long string. I have tried the xml:space="preserve" within that element and this still is not working.

Here is part of the xml with the carriage return line feeds. The element that I am talking about is called pseudocode. You can see it is displayed correctly below.

Design Document XML : <?xml version="1.0" encoding="ISO-8859-1"?><design-doc><title-page><title>Testing Document</title><class-prepared-for>Lloyd</class-prepared-for><authors>Ben Garbers</authors><date-created>September 23, 2004</date-created></title-page><introduction text="This is the introduction. This is the introduction."/><class-definitions><class-definition class-name="ClassOne" visibility="public" class-complexity="not complex"><global-variables><variable visibility="public" name="VariableOne" object="String" comments="This is a String."/><variable visibility="public" name="VariableTwo" object="Integer" comments="This is an Integer."/></global-variables><methods><method name="MethodOne" synopsis="This will do some things." purpose="public" visibility="public"><input-parms><variable visibility="null" name="firstName" object="String" comments="null"/></input-parms><output-parms><variable visibility="null" name="lastName" object="String" comments="null"/></output-parms><local-variables><variable visibility="null" name="age" object="int" comments="null"/></local-variables><pseudocode xml:space="preserve">/* Ensure that player name exist in the system*/
if (player =null)
NullPlayerException;
if (NOT (isPlayerNameExist(player)))
throw PlayerNameNotExistException;
/* Ensure player name exist in the team already*/
i ? 1;
while (i <= length(mPlayers)) {
if (mPlayers.mPlayerName != playerName)
throw PlayerNameNotExistInTeamException;
}
this.mPlayers ? this.mPlayers - player;</pseudocode>


However, when I do the transformation I get the following with the pseudocode:

/* Ensure that player name exist in the system*/if (player =null) NullPlayerException; if (NOT (isPlayerNameExist(player))) throw PlayerNameNotExistException; /* Ensure player name exist in the team already*/ i ? 1; while (i <= length(mPlayers)) { if (mPlayers[i].mPlayerName != playerName) throw PlayerNameNotExistInTeamException;}this.mPlayers ? this.mPlayers - player;

It looks like it adds some spaces but no carriage returns or line feeds.

My transformation code is the following:

public ParseFile(String xmlString) {

try {
//Here we will load the correct style sheet for the message
//that will be formatted.

String urlString = "DesignDocument.xsl";

URL url = this.getClass().getResource(urlString);

System.out.println("URL to style sheet = " + url.toString());
String styleSheetName = url.getFile();
System.out.println("Style sheet name : " + styleSheetName);

InputStream styleSheetInputStream = this.getClass().getResourceAsStream(urlString);

//We will than load our SAXParerFactory and parse the message.
SAXParserFactory SPFactory = SAXParserFactory.newInstance();
SPFactory.setValidating(true);
SAXParser sp = SPFactory.newSAXParser();
XMLReader sax2parser = sp.getXMLReader();

//NO Validation done on for this because we do NOT have a DTD.
sax2parser.setFeature(
"http://xml.org/sax/features/validation",
false);

sax2parser.setContentHandler(new FileContentHandler());

//we create a character array of the length of the xml message String
char[] messageCharacterArray =
new char[xmlString.length()];

// we then put the xml string into the character array.
messageCharacterArray = xmlString.toCharArray();

// after this ew end up creating a CharArrayReader that will be used by the Sax
// parser to parse the xml.
CharArrayReader xmlCharArrayReader =
new CharArrayReader(messageCharacterArray);

// we put the xml character array within the Input Source which will then be parsed
// by the Sax2 parser.
InputSource in = new InputSource(xmlCharArrayReader);


// Use a Transformer for outputting the message into our formatted xml using
// the stylesheet defined up in a messages own sxl stylesheet file.
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setURIResolver(new BasicURIResolver());
StreamSource stylesource = new StreamSource(styleSheetInputStream);
Transformer transformer = tFactory.newTransformer(stylesource);

// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");


// Properties properties = new Properties();
// properties.setProperty("indent", "yes");
// properties.setProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// transformer.setOutputProperties(properties);

// We will use a ByteArrayOutputStream to put our transformed xml.
ByteArrayOutputStream bos = new ByteArrayOutputStream();

StreamResult result = new StreamResult(bos);
SAXSource source = new SAXSource(in);

transformer.transform(source, result);

String s = bos.toString();

StringBuffer sb = new StringBuffer();

//This will make sure the Version Line is not shown.
BufferedReader br = new BufferedReader(new StringReader(s));

String line = "";
while ((line = br.readLine()) != null) {

sb.append(line);
}

bos.close();
br.close();
formattedXmlString = sb.toString();
System.out.println("--------------------------------");
System.out.println(formattedXmlString);
System.out.println("--------------------------------");

} catch (Exception e) {
e.printStackTrace();
}

}

I have been searching and trying to figure this out forever. Any help would be greatly appreciated. Bottom line is that I want carriage returns and tabs to work when transferring from the xml to xsl.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 27 2006
Added on Apr 27 2006
13 comments
783 views