Java, UnmarshallingException caused by XML attribute with special chars: ;ìè+òàù-<^èç°§_>!£$%&/()=?~`'#;
-
30-09-2019 - |
Question
my xml file has a tag with an attribute "containsValue" which contains the "special" characters you can see in the subject:
<original_msg_body id="msgBodySpecialCharsRule" containsValue=";ìè+òàù-<^èç°§_>!£$%&/()=?~`'#;" />
in my xml schema the attribute has xs:string:
<xs:attribute name="containsValue" type="xs:string" />
I use this value inside a Java software which check if this value is contained inside another String.
but I always obtain this Exception:
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: The value of attribute "containsValue" associated with an element type "original_msg_body" must not contain the '<' character.]
How can I solve it? I've tried changing the attribute type to xs:NMTOKEN, ut I get the same exception. Is there any other type?
I think I could change the characters encoding, for example using the HTML representation, like <, but than could be tricky for the string comparison...
Solution
You need to escape special XML entities like <
, >
, "
with <
, >
, "e;
OTHER TIPS
Use entity references: replace <
with <
and >
with >
etc. in your XML document. Your XML parser will then handle conversion between actual character and its entity reference. That is, in your code you get the actual <
or >
character.