XmlBeans error: unexpected element CDATA when parsing String

28,611

Solution 1

This is an encoding problem, I used the below code that worked for me:

        File xmlFile = new File("./data/file.xml");
        FileDocument fileDoc = FileDocument.Factory.parse(xmlFile);

Solution 2

The exception is caused by the length of the XML file. If you add or remove one character from the file, the parser will succeed.

The problem occurs within the 3rd party PiccoloLexer library that XMLBeans relies on. It has been fixed in revision 959082 but has not been applied to xbean 2.5 jar.

What does the org.apache.xmlbeans.XmlException with a message of “Unexpected element: CDATA” mean?

XMLBeans - Problem with XML files if length is exactly 8193bytes

Issue reported on XMLBean Jira

Share:
28,611
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm having problems parsing an xml string using XmlBeans. The problem itself is in a J2EE application where the string itself is received from external systems, but i replicated the problem in a small test project.

    The only solution i found is to let XmlBeans parse a File instead of a String, but that's not an option in the J2EE application. Plus i really want to know what exactly the problem is because i want to solve it.

    Source of test class:

    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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }   
    }
    

    The XML file validates perfectly against the XSD's im using. 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 below. I've checked the string itself in the debugger and don't really see anything wrong with it at first sight, especially not at row 1 column 1 where i think the Sax parser is having a problem with if i'm interpreting the error correctly.

    debug

    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