Link to home
Start Free TrialLog in
Avatar of mali_djuro
mali_djuro

asked on

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


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to start with correct input or use a FilterInputStream/FilterReader to filter it out
try wrapping the content in a cdata block
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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mali_djuro
mali_djuro

ASKER

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?
Using a FilterReader is the least effortful and invasive way
> 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

https://www.experts-exchange.com/help.jsp#hi69
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

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