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.newInst ance();
// 2. Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
Transformer transformer = tFactory.newTransformer(ne w StreamSource(xsltFile));
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(fileName);
JDOMSource source = new JDOMSource(doc);
transformer.transform(sour ce, 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
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.newInst
// 2. Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
Transformer transformer = tFactory.newTransformer(ne
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(fileName);
JDOMSource source = new JDOMSource(doc);
transformer.transform(sour
....
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
You need to start with correct input or use a FilterInputStream/FilterRe ader 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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
you can split the points
https://www.experts-exchange.com/help.jsp#hi69
ASKER
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
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.
using a cdata block would require no extra parsing ;)
better to fix it at the source.
ASKER
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.
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.