Home > Mobile >  Ignoring & Symbol in JAXB Unmarshalling (Java 1.8)
Ignoring & Symbol in JAXB Unmarshalling (Java 1.8)

Time:11-25

I am trying to unmarshall and XML which has the schema more or less like this:

<xs:complexType>
    <xs:sequence>
        <xs:element name="type" type="xs:int" minOccurs="0"/>
        <xs:element name="scope" type="xs:int" minOccurs="0"/>
        <xs:element name="code" type="xs:int" minOccurs="0"/>
        <xs:element name="target" type="xs:string" minOccurs="0"/>
        <xs:element name="message" type="xs:string" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>

I use this XML format to pass around messages in JMS.

Now my problem is, occasionally, the message tag contains a String that contains & (eg: Tom & Jerry). Now I know XML prefers it to be like &amp;. But is there any way to ignore the & during unmarshalling. Currently, it is throwing an exception:

[org.xml.sax.SAXParseException; lineNumber: 163; columnNumber: 45; The reference to entity "T" must end with the ';'

Any advice on this would be helpful. I can do some string manipulation, like search for & and replace with &amp; and once unmarshalled, just add the & back. But not a 100% sure it would work.

CodePudding user response:

XML doesn't "prefer" it to be like &amp;, it requires it. Your data isn't well-formed XML, so no XML tools will accept it. Whatever program generated the XML is broken and needs fixing.

  • Related