This is an Xerces implementation issue, check this out: https://issues.apache.org/jira/browse/XERCESJ-1033
It seems that empty CDATA should not exist, so the only tips I can give you are:
- Changing the implementation of XML parsing
- Remove the empty CDATA from the source files (replace "
<![CDATA[]]> " with "")
or put a space in CDATA, for example. <![CDATA[ ]]>
I am adding a few examples with a different implementation.
Jaxb
In Jaxb, you map your XML to POJOs in a simple way.
For example, if you have the following xml file in c: \ myFile.xml:
<root> <foo><![CDATA[]]></foo> <foo><![CDATA[some data here]]></foo> </root>
You may have the following POJOs:
@XmlRootElement public class Root { @XmlElement(name="foo") privateList<Foo> foo; public List<Foo> getFooList() { return foo; } public void setFooList(List<Foo> fooList) { this.foo = fooList; } } @XmlType(name = "foo") public class Foo { @XmlValue private String content; @Override public String toString() { return content; } }
And then parse the XML object in Object with the following snippet:
public static void main(String[] args) { try { File file = new File("C:\\myFile.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Root.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Root root = (Root) jaxbUnmarshaller.unmarshal(file); for (Foo foo : root.getFooList()) { System.out.println(String.format("Foo content: |%s|", foo)); } } catch (JAXBException e) { e.printStackTrace(); } }
I tested this and did not cause errors.
source share