?
Solved

An invalid XML character (Unicode: 0x1e) was found in the element content of the document.

Posted on 2006-04-11
11
Medium Priority
?
3,695 Views
Last Modified: 2008-01-09
Hi,

i use following code to transform content of xml file (fileName) to html to show it in browser using stylesheet (xsltFile)

                        ....
                        StringWriter sw = new StringWriter();
                  // 1. Instantiate a TransformerFactory.
                  TransformerFactory tFactory = TransformerFactory.newInstance();
                  // 2. Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
                  Transformer transformer = tFactory.newTransformer(new StreamSource(xsltFile));
                  SAXBuilder builder = new SAXBuilder();                  
                  Document doc = builder.build(new File(fileName);
                  JDOMSource source = new JDOMSource(doc);
                  transformer.transform(source, new StreamResult(sw));
                        ....

and it works fine, except in case when in xml file occurs character 0x1e. and i got following exception
 
                        An invalid XML character (Unicode: 0x1e) was found in the element content of the document.

i tried with different builders, parsers... but allways is same exception.
what do i do wrong?

please help,

thanks in advance


0
Comment
Question by:mali_djuro
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16424836
You need to start with correct input or use a FilterInputStream/FilterReader to filter it out
0
 
LVL 92

Expert Comment

by:objects
ID: 16424841
try wrapping the content in a cdata block
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16425233
Try reading the contents of the file into a String first, trim out the string (with all characters like '\0', 0x1e, etc) and then use that String to populate your document.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 1400 total points
ID: 16425387
Pipe the input through this Reader:


package misc;

import java.io.Reader;
import java.io.StringReader;
import java.io.FilterReader;
import java.io.IOException;

class NoRecordSeparatorReader extends FilterReader {
      // (For testing)
      public static void main(String[] args) throws IOException {
            String s = "Rec1\u001eRec2\u001eRec3";
            NoRecordSeparatorReader in = new NoRecordSeparatorReader(
                        new StringReader(s));
            int c = -1;
            while ((c = in.read()) > -1) {
                  System.out.print((char) c);
            }
            in.close();
      }

      public NoRecordSeparatorReader(Reader in) {
            super(in);
      }

      public int read() throws IOException {
            int c = in.read();
            switch (c) {
            case 0x1E:
                  return read();
            default:
                  return c;
            }
      }

      public int read(char cbuf[], int off, int len) throws IOException {
            int charsRead = 0;
            int c = -1;
            while ((c = read()) > -1 && charsRead < len) {
                  cbuf[charsRead++] = (char) c;
            }
            return c > -1 ? charsRead : -1;
      }
}
0
 

Author Comment

by:mali_djuro
ID: 16425389
thanks to all.

at the end, i will use one of suggestions.

but i thought that there is a way to solve it without extra parsing of String or InputStream before transformation, maybe with sets some properties of parser, builder...
because, it isn't often case, and extra parsing will delay transforamtion and display, but if there is no other way...

thanks, once again.

p.s. i don't know how to accept all three answers as sollution and split points between you? so, i will give points to first one. it's ok or you have sollution for that?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16425403
Using a FilterReader is the least effortful and invasive way
0
 
LVL 92

Expert Comment

by:objects
ID: 16425460
> p.s. i don't know how to accept all three answers as sollution and split points between you? so, i will give points to first one. it's ok or you have sollution for that?

you can split the points

http://www.experts-exchange.com/help.jsp#hi69
0
 

Author Comment

by:mali_djuro
ID: 16425545
sorry,
i wrote last comment before i saw CEHJs answer,
so i changed opinion and accepted his answer as sollution,

thanks

p.s. now, i know how to split points, for next questions. thanks objects

0
 
LVL 92

Expert Comment

by:objects
ID: 16425586
>  it isn't often case, and extra parsing will delay transforamtion and display

using a cdata block would require no extra parsing ;)
better to fix it at the source.
0
 

Author Comment

by:mali_djuro
ID: 16425655
the problem is that i don't have access to source file while it is generating.

i tried to solve it in that way, but other side, team that generates file, said no.
so, i needed to find sollution on my side, in transformation.
 
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 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