htmlxmlxml-parsinghtml-entitiesxml-entities

XML character entities not parsing


I'm working with a server.xml file...

Case 1:

<?xml version="1.0" encoding="UTF-8"?>

<Resource name="${app.name}" />

In catalina.properties i have declared the app.name

app.name=&#x6F;&#x72;

Case 2:

<?xml version="1.0" encoding="UTF-8"?>

<Resource name="&#x6F;&#x72;" />

The problem is why case 2 is working and case 1 not? Why in case 1 XML entities not parsing?

I.e the output is :

<Resource name= "&#x6F;&#x72;"  />    //in case 1
<Resource name= "or"  />              //in case 2

Solution

  • Key point: Entity expansion happens during XML parsing.

    Case 1

    In case 1, during parsing, there are no entities in Resources/@name – just ${app.name}, which the program calling the XML parser would presumably go on to substitute the literal text, &#x6F;&#x72;, for the variable:

    <Resource name="&#x6F;&#x72;" />
    

    Downstream processing likely doesn't know how to deal with &#x6F;&#x72;, and you have your "not working" case.

    Case 2

    In case 2, &#x6F;&#x72; exists in the XML file prior to parsing. After parsing, effectively, the program calling the XML parser sees the entities expanded:

    <Resource name="or" />
    

    and is able to "work" because it knows what to do when @name is "or".

    Note that had catalina.properties been an XML file, the expansion would have occurred then that file was parsed, and you'd be back to your "working" case.

    Solution

    Options include one of the following:

    1. Hardwire the entities in server.xml rather than in catalina.properties.
    2. Force the property substitution to happen prior to XML parsing of server.xml.
    3. Use Unicode characters directly (not encoded as XML entities) in your catalina.properties file.