Hi,
I just want to ask how are you able to "un-escape" the characters you've
escape using the code below?
Thanks!
Erik van Zijst-7 wrote:
>
> Hi folks,
>
> I'm running into a problem where a string that contains valid UTF-8
> characters that are illegal in XML (e.g. 0x10), gets serialized by
> jaxb without escaping/encoding these bytes, effectively producing
> illegal XML.
>
> When I later try to unmarshal these objects, the unmarshaller crashes
> with:
>
> javax.xml.bind.UnmarshalException
> - with linked exception:
> [org.xml.sax.SAXParseException: An invalid XML character (Unicode:
> 0x10) was found in the element content of the document.]
> at
> javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
> ...
>
> I've attached a very small unit test that reproduces this problem. I
> was under the impression that the serializer would escape illegal
> characters by encoding them like: 
 but instead the test produces
> invalid xml at line 31 and then crashes on line 35.
> What am I overlooking?
>
> cheers,
> Erik
>
> package test;
>
> import javax.xml.bind.JAXBContext;
> import javax.xml.bind.Marshaller;
> import javax.xml.bind.Unmarshaller;
> import javax.xml.bind.annotation.XmlRootElement;
> import java.io.ByteArrayOutputStream;
> import java.io.StringReader;
> import java.io.StringWriter;
>
> import org.junit.Test;
>
> public class JAXBTest {
>
> @Test
> public void marshalling() throws Exception {
>
> DAO dao = new DAO();
> dao.foo = "some unicode characters:\n" +
> "- 0x09: '\u0009' (tab; allowed)\n" +
> "- 0x10: '\u0010' (DLE; illegal in XML)\n" +
> "- 0x20: '\u0020' (space; legal in XML)";
>
> StringWriter writer = new StringWriter();
>
> JAXBContext jc = JAXBContext.newInstance(DAO.class);
> Marshaller m = jc.createMarshaller();
> Unmarshaller um = jc.createUnmarshaller();
>
> // marshal:
> m.marshal(dao, writer);
> System.out.println("Marshalled: " + writer.toString());
>
> // unmarshal
> DAO dao2 = (DAO)um.unmarshal(new StringReader(writer.toString()));
> System.out.println("Unmarshalled: " + dao2.foo);
> }
>
> @XmlRootElement
> public static class DAO {
> public String foo;
> }
> }
>
> -------------------------------------------------------------------------------
> Test set: test.JAXBTest
> -------------------------------------------------------------------------------
> Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.445 sec
> <<< FAILURE!
> marshalling(test.JAXBTest) Time elapsed: 0.404 sec <<< ERROR!
> javax.xml.bind.UnmarshalException
> - with linked exception:
> [org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x10)
> was found in the element content of the document.]
> at
> javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:476)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:198)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:167)
> at
> javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
> at
> javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
> at test.JAXBTest.marshalling(JAXBTest.java:35)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at
> org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
> at
> org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
> at
> org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
> at
> org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
> at
> org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
> at
> org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
> at
> org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
> at
> org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
> at
> org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
> at
> org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
> at
> org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
> at
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
> at
> org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
> at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at
> org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
> at
> org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
> Caused by: org.xml.sax.SAXParseException: An invalid XML character
> (Unicode: 0x10) was found in the element content of the document.
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
> at
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
> at
> com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1411)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2886)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
> at
> com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
> at
> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
> at
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
> at
> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
> at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:194)
> ... 28 more
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
--
View this message in context: http://old.nabble.com/Escaping-illegal-characters-during-marshalling-tp20090044p33325760.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.