XmlBeans error: unexpected CDATA element parsing string

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

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.

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 
+4
source share
2 answers

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

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

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

The problem occurs in the third PiccoloLexer library, which XMLBeans relies on. It was fixed in edition 959082, but was not applied to the xbean 2.5 jar.

What does the org.apache.xmlbeans.XmlException message mean with the message "Unexpected element: CDATA"?

XMLBeans - problem with XML files if the length is exactly 8193 bytes

XMLBean Jira Report

+1
source

Source: https://habr.com/ru/post/1481667/


All Articles