Friday, September 11, 2009

"The processing instruction target matching "[xX][mM][lL]" is not allowed."

I often put the Xml I use for test cases inside of Groovy classes. This allows me to make use of multiline Strings.

String inputXml = '''
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<records>
<record>
<recordid>833432</recordid>
<name>Lorem ipsum dolor</name>
<parentid>0</parentid>
<link>http://consectetueradipiscingelit</link>
<date>9/10/2009</date>
<description>Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</description>
<category ID="48015"></category>
<category>In enim justo</category>
</record>
</records>
'''


I ran into this error today :

"The processing instruction target matching "[xX][mM][lL]" is not allowed."

I'm surprised I hadn't run into it before. The problem was that I had white space before the xml prolog.

There are two easy solutions :
  1. Remove the whitespace.
  2. Call "trim()" on the String.

Whitespace removed :
String inputXml = '''<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<records>
<record>
<recordid>833432</recordid>
<name>Lorem ipsum dolor</name>
<parentid>0</parentid>
<link>http://consectetueradipiscingelit</link>
<date>9/10/2009</date>
<description>Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</description>
<category ID="48015"></category>
<category>In enim justo</category>
</record>
</records>


trim() called :

String inputXml = '''
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<records>
<record>
<recordid>833432</recordid>
<name>Lorem ipsum dolor</name>
<parentid>0</parentid>
<link>http://consectetueradipiscingelit</link>
<date>9/10/2009</date>
<description>Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</description>
<category ID="48015"></category>
<category>In enim justo</category>
</record>
</records>
'''.trim()


1 comment:

Walter Holladay said...

Thanks a lot. I just ran into this issue today!