5

I can parse xml just fine with SAXParserFactory in Java, BUT in some files, there are some non-lowercase attributes present, like linear3D="0.5" etc.

I would like to somehow make

attributes.getValue(attr)

case-insensitive, so that attributes.getValue("linear3d") returns "0.5".

One solution would be to read the file as a string first, convert to lowercase, and then parse, since there is no ambiguity in doing this in this type of xml. However, can this be done more simply, by adding some flag to the factory or similar?

Per Alexandersson
  • 2,443
  • 1
  • 22
  • 28
  • You should rather change your model. XML is case-sensitive. Don't go against conventions. – Michael-O Jul 30 '11 at 19:44
  • Well, unfortunantely, I am trying to parse .flame files, which almost fulfils .xml standards. I do not have control over its syntax, since they are created by a 3rd party software. – Per Alexandersson Jul 30 '11 at 20:59
  • What does that mean? You don't have any guarantee that it will always linear3d or linear3D? – Michael-O Jul 30 '11 at 21:03
  • Well, in theory, it is probably always linear3D, however, attributes are 1-to-1 in correspondence with class files, and following Java standard, these should be capitalized. A solution would be to rename my Linear3d class to Linear3D... – Per Alexandersson Jul 31 '11 at 04:35
  • You should really normalize class and fields names rather than transforming the XML files over and over again. – Michael-O Jul 31 '11 at 08:56

1 Answers1

4

Instead of converting the entire file in to lower case the following approach is better. This iterates through all the attributes of the selected tag and comperes it ignoring the case.

Inside the implementaion of DefaultHandler write the following method

private String getValueIgnoreCase(Attributes attributes, String qName){
    for(int i = 0; i < attributes.getLength(); i++){
        String qn = attributes.getQName(i);
        if(qn.equalsIgnoreCase(qName)){
            return attributes.getValue(i);
        }
    }
    return null;
}
M S
  • 3,995
  • 3
  • 25
  • 36