commons-user mailing list archives

Oliver Heger wrote:
> Borut Bolčina wrote:
>
>> Hi,
>>
>> when having an attribute value with commas in XML configuration file,
>> this value gets splitted. An example:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <questionnaire>
>> <set week="42" text="Questions for first week">
>> <question text="First question, something after comma.">
>> <=== offending
>> <answers>
>> <answer>(I) 1.a</answer>
>> <answer correct="true">(I) 1.b</answer>
>> <answer>(I) 1.c</answer>
>> </answers>
>> </question>
>> <question text="Second question, which includes comma in the
>> sentence."> <=== offending
>> <answers>
>> <answer correct="true">(I) 2.a</answer>
>> <answer>(I) 2.b</answer>
>> <answer>(I) 2.c</answer>
>> </answers>
>> </question>
>> ...
>>
>> When calling this method
>>
>>
>> /**
>> * @return all questions from XML configuration file
>> */
>> @SuppressWarnings("unchecked")
>> public Collection<String> getQuestions() {
>> String ofQuestions = "set.question[@text]";
>> return config.getList(ofQuestions);
>> }
>>
>> the returned list contains *four* instead of two questions:
>> 1.) First question
>> 2.) something after comma
>> 3.) Second question
>> 4.) which includes comma in the sentence
>>
>> Is this by design? I think this should not work this way.
>>
>> Regards,
>> Borut
>>
> The "," character is per default interpreted as property value
> delimiter. That means if a comma is found in an attribute or in the
> text value of an XML element, the text will be splitted and two values
> will be stored. This mechanism is described in the section "Lists and
> arrays" of the properties howto
> (http://jakarta.apache.org/commons/configuration/howto_properties.html),
> and XMLConfiguration adapted this behavior to be compatible.
>
> The easiest way to solve your problem is to escape the commas in your
> attributes with a backslash, e.g.
>
> <question text="Second question\, which includes comma in the sentence.">
>
> Then the strings won't get splitted.
>
> Oliver
Oliver thanks for quick reply. I tried as you suggested and it works. I
was aware of the "Lists and arrays" feature and it makes sense for the
flat world of ordinary property files. IMO this should not be the way in
structured world.
In the meantime I wrote XPath extraction methods (nothing general, just
for my purposes) to mimic the behavior, but with greater flexibility.
Keep up the good work, bye
Borut
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org