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?

5
  • You should rather change your model. XML is case-sensitive. Don't go against conventions. Commented Jul 30, 2011 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. Commented Jul 30, 2011 at 20:59
  • What does that mean? You don't have any guarantee that it will always linear3d or linear3D? Commented Jul 30, 2011 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... Commented Jul 31, 2011 at 4:35
  • You should really normalize class and fields names rather than transforming the XML files over and over again. Commented Jul 31, 2011 at 8:56

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.