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

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


mali_djuroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You need to start with correct input or use a FilterInputStream/FilterReader to filter it out
0
objectsCommented:
try wrapping the content in a cdata block
0
Mayank SAssociate Director - Product EngineeringCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

CEHJCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mali_djuroAuthor Commented:
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
CEHJCommented:
Using a FilterReader is the least effortful and invasive way
0
objectsCommented:
> 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
mali_djuroAuthor Commented:
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
objectsCommented:
>  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
mali_djuroAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.