I'm having trouble parsing an xml string using XmlBeans. The problem itself is the J2EE application, where the string itself is obtained from external systems, but I replicated the problem in a small test project.
The only solution I found was to let XmlBeans parse the file instead of String, but this is not an option in a J2EE application. Plus, I really want to know what the problem is, because I want to solve it.
Test class source:
public class TestXmlSpy { public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(new FileInputStream("d:\\temp\\IE734.xml"),"UTF-8"); BufferedReader r = new BufferedReader(reader); String xml = ""; String str; while ((str = r.readLine()) != null) { xml = xml + str; } xml = xml.trim(); System.out.println("Ready reading XML"); XmlOptions options = new XmlOptions(); options.setCharacterEncoding("UTF-8"); try { XmlObject xmlObject = XmlObject.Factory.parse(new File("D:\\temp\\IE734.xml"), options); System.out.println("Ready parsing File"); XmlObject.Factory.parse(xml, options); System.out.println("Ready parsing String"); } catch (XmlException e) {
The XML file is well-tested for using XSD im. Also, parsing it as a File object works fine and gives me a parsed XmlObject to work with. However, parsing the xml-String gives the stacktrace value below. I checked the line itself in the debugger and actually do not see anything bad in it, especially not in column 1 of line 1, where, in my opinion, the Sax parser has a problem if I interpret the error correctly.

Stacktrace:
Ready reading XML Ready parsing File org.apache.xmlbeans.XmlException: error: Unexpected element: CDATA at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3511) at org.apache.xmlbeans.impl.store.Locale.parse(Locale.java:713) at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:697) at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:684) at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:208) at org.apache.xmlbeans.XmlObject$Factory.parse(XmlObject.java:658) at xmlspy.TestXmlSpy.main(TestXmlSpy.java:37) Caused by: org.xml.sax.SAXParseException; systemId: file:; lineNumber: 1; columnNumber: 1; Unexpected element: CDATA at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportFatalError(Piccolo.java:1038) at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:723) at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3479) ... 6 more
source share