Results 1 to 8 of 8
  1. #1
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default Transforming xml to text keeps placing blank line at beginning of text file

    I am using the following code to read in data from a file called employee.dat into xml format, and then output it to a text file as text using an XSL transformation. Here is the code:

    Java Code:
    	public class TransformTest {
    
    		public static void main(String[] args) throws Exception {
    			String filename;
    			if (args.length > 0) filename = args[0];
    			else filename = "makehtml.xsl";
    			File styleSheet = new File(filename);
    			StreamSource styleSource = new StreamSource(styleSheet);
    
    			Transformer t = TransformerFactory.newInstance().newTransformer(styleSource);
    			t.setOutputProperty(OutputKeys.INDENT, "yes");
    			t.setOutputProperty(OutputKeys.METHOD, "text");
    			t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    
    			t.transform(new SAXSource(new EmployeeReader(), new InputSource(new FileInputStream(
    				"employee.dat"))), new StreamResult(new File("output.txt")));
    		}
    	}
    The XSL file I am using to make the transformation contains the following:

    Java Code:
    <?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:template match="/staff/employee">
    employee.<xsl:value-of select="position()"/>.name=<xsl:value-of select="name/text()"/>
    employee.<xsl:value-of select="position()"/>.salary=<xsl:value-of select="salary/text()"/>
    employee.<xsl:value-of select="position()"/>.hiredate=<xsl:value-of select="hiredate/@year"/>-<xsl:value-of select="hiredate/@month"/>-<xsl:value-of select="hiredate/@day"/>
    </xsl:template>
    </xsl:stylesheet>
    For some reason, the output always contains a blank line at the top and I am not sure why. Please let me know what I am doing wrong, thanks!

    -Derek

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    What does the XML look like?

  3. #3
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Post

    The contents of the file I am using for input are:

    Java Code:
    Carl Cracker|75000.0|1987|12|15
    Harry Hacker|50000.0|1989|10|1
    Tony Tester|40000.0|1990|3|15
    The output of this input after transformation to xml format (using a different XSL file to handle the converstion) is:

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <table border="1">
      <tr>
        <td>Carl Cracker</td>
        <td>75000.0</td>
        <td>1987-12-15</td>
      </tr>
      <tr>
        <td>Harry Hacker</td>
        <td>50000.0</td>
        <td>1989-10-1</td>
      </tr>
      <tr>
        <td>Tony Tester</td>
        <td>40000.0</td>
        <td>1990-3-15</td>
      </tr>
    </table>
    The output when set to be transformed to text (using the XSL file shown in the initial post) is:

    Java Code:
    employee.1.name=Carl Cracker
    employee.1.salary=75000.0
    employee.1.hiredate=1987-12-15
    employee.2.name=Harry Hacker
    employee.2.salary=50000.0
    employee.2.hiredate=1989-10-1
    employee.3.name=Tony Tester
    employee.3.salary=40000.0
    employee.3.hiredate=1990-3-15
    There is always a blank space just above the first entry that I can't seem to get rid of (sorry in the code block above it doesn't display the first blank line even though I put it there). Is this just inherent of the API to do that or is there a way to make it not do that? If xml is enabled, it adds the <?xml version="1.0"?> code to the first line followed by a blank line, and then the xml output. So it this blank line always supposed to be there? I was just hoping I could get the transformation to write without having to open the output up and fixing the first line if it is blank. It would be much more efficient if the transformer did it automatically. Thanks Tolls!

    -Derek

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    That's not the XML that that XSL is intended to transform.
    Where's the name, salary and hiredate tags/attributes?

  5. #5
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    In the rest of the code from the program, there is a reader that reads in the 3 lines of data that I showed in the last post. The date is actually three different attributes, being: year, month, day. Here is the rest of the code from the program (I left it out before to keep the listing from being too long).

    Java Code:
    	/**
    	 * This class reads the flat file employee.dat and reports SAX parser events to act as if
    	 * it was parsing an XML file.
    	 */
    	class EmployeeReader implements XMLReader {
    
    		private ContentHandler handler;
    
    		public void parse(InputSource source) throws IOException, SAXException {
    			InputStream stream = source.getByteStream();
    			BufferedReader in = new BufferedReader(new InputStreamReader(stream));
    			String rootElement = "staff";
    			AttributesImpl atts = new AttributesImpl();
    
    			if (handler == null) throw new SAXException("No content handler");
    
    			handler.startDocument();
    			handler.startElement("", rootElement, rootElement, atts);
    			String line;
    			while ((line = in.readLine()) != null) {
    				handler.startElement("", "employee", "employee", atts);
    				StringTokenizer t = new StringTokenizer(line, "|");
    			
    				handler.startElement("", "name", "name", atts);
    				String s = t.nextToken();
    				handler.characters(s.toCharArray(), 0, s.length());
    				handler.endElement("", "name", "name");
    
    				handler.startElement("", "salary", "salary", atts);
    				s = t.nextToken();
    				handler.characters(s.toCharArray(), 0, s.length());
    				handler.endElement("", "salary", "salary");
    
    				atts.addAttribute("", "year", "year", "CDATA", t.nextToken());
    				atts.addAttribute("", "month", "month", "CDATA", t.nextToken());
    				atts.addAttribute("", "day", "day", "CDATA", t.nextToken());
    				handler.startElement("", "hiredate", "hiredate", atts);
    				handler.endElement("", "hiredate", "hiredate");
    				atts.clear();
    
    				handler.endElement("", "employee", "employee");
    			}
    
    			handler.endElement("", rootElement, rootElement);
    			handler.endDocument();
    		}
    
    		public void setContentHandler(ContentHandler newValue) {
    			handler = newValue;
    		}
    
    		public ContentHandler getContentHandler() {
    			return handler;
    		}
    
    		// the following methods are just do-nothing implementations.
    		public void parse(String systemId) throws IOException, SAXException {
    
    		}
    
    		public void setErrorHandler(ErrorHandler handler) {
    		
    		}
    
    		public ErrorHandler getErrorHandler() {
    			return null;
    		}
    
    		public void setDTDHandler(DTDHandler handler) {
    		
    		}
    
    		public DTDHandler getDTDHandler() {
    			return null;
    		}
    
    		public void setEntityResolver(EntityResolver resolver) {
    
    		}
    
    		public EntityResolver getEntityResolver() {
    			return null;
    		}
    
    		public void setProperty(String name, Object value) {
    
    		}
    
    		public Object getProperty(String name) {
    			return null;
    		}
    
    		public void setFeature(String name, boolean value) {
    
    		}
    
    		public boolean getFeature(String name) {
    			return false;
    		}
    	}
    So the text data delimited with "|" characters is read and converted to XML, then written to a file called output.txt using whichever transformation file was specified, ex: java TransformTest makeprop.xsl (makeprop.xsl is the first xsl file I posted in the original post).

    Thanks again Tolls,

    -Derek

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    OK.
    But can you use that to actually generate the XML that is being transformed?
    That is, so you can see what the XML looks like before the transformation?

    I'm asking because I suspect there's actually a space/line in that XML.

    The transformer won't add lines in unless they are either in the source document int he first place, or added in as part of the stylesheet.

  7. #7
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Actually, just found out the blank space was due to a carriage return in the XSL file, I didn't realize that a carriage return immediately following the opening <xsl:template ... > tag would result in a line break. Just out of curiousity though, how would I go about displaying the XML of the SAXSource object? I couldn't find any methods of the class that would give me access to the individual elements, or to convert the elements and nodes to straight XML. Thanks again for all of your help Tolls,

    -Derek

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    I have to say, I'm not too sure how you'd do it.
    I suppose instead of doing the handler thing, you do the same code elsewhere but build an XML Document up. That should result in the same XML. Maybe...

    As for the original problem, any whitespace in the xsl is taken as intended and will appear in the output. Which I should have spotted I suppose.

Similar Threads

  1. Replies: 1
    Last Post: 02-28-2011, 06:30 PM
  2. Stepping through a text file line by line
    By evanlivingston in forum New To Java
    Replies: 10
    Last Post: 01-29-2011, 05:30 AM
  3. Replies: 8
    Last Post: 12-28-2010, 03:24 PM
  4. Replies: 4
    Last Post: 10-10-2010, 11:57 AM
  5. Remove a Blank Line from Text file
    By nitinverma in forum CLDC and MIDP
    Replies: 8
    Last Post: 06-15-2010, 07:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •