Parse XML with SAX in java case insensitive.

I can simply parse the xml using SAXParserFactory in Java, but in some files, there are some attributes other than lowercase, e.g. linear3D="0.5"etc.

I would like to do something

attributes.getValue(attr)

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

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

+5
source share
1 answer

. .

DefaultHandler

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;
}
+4

All Articles