Problem with square brackets in xml file

Hi

I have an xml file containing the following tag

<custom name="custom1">

<value>acqTypeMaxNumber[Line](plot);imageSelection[Line](plot);imagePositionSet1[Line];imagePositionSet2[Line];imageSet[Surface]</value>

</custom>

Previously I was using j2se 1.4.7, and this was not a problem to parse.

Now, after uprading to j2se 5.0, this can no longer be parsed.

I am using a class with the following structure for the parsing:

publicclass SaxParserextends DefaultHandler{

publicstaticvoid parseXmlFile(File input, String schemaLoc, DefaultHandler handler)throws Exception{

// Create a builder factory

SAXParserFactory factory = SAXParserFactory.newInstance();

factory.setNamespaceAware(true);

factory.setValidating(true);

// Create a parser and parse the xml file.

SAXParser parser = factory.newSAXParser();

parser.parse(input, handler);

}

publicvoid startElement(String namespaceURI, String localName,

String qName, Attributes atts){

// Do stuff

}

publicvoid characters(char[] buf,int offset,int len)throws SAXException{

name =new String(buf, offset, len).trim();

// Do stuff

}

publicvoid endElement(String namespaceURI, String sName, String qName)throws SAXException{

// Do stuff

}

privatevoid setAttributes(SchemaElement e, Attributes atts){

// Do stuff

}

privateboolean anyNextToFound(String name){

// Do stuff

}

}

What I see is that the characters method is being called once for every square bracket in the value tag, and once for all characters after and before the square bracket.

I have tried, without success, to write the tag using CDATA, i.e. looking like this:

<value>![CDATA[acqTypeMaxNumber[Line](plot);imageSelection[Line](plot);imagePositionSet1[Line];imagePositionSet2[Line];imageSet[Surface]]]</value>

I have searched a lot on the internet without finding anything concerning this problem.

Does anyone have a clue of what's going on here?

Any help at all is appreciated.

[3627 byte] By [powallina] at [2007-10-3 5:25:32]
# 1

Check the JavaDocs.

You have overlooked the fact that the characters method can be called many times (as you have just demonstrated). There is no rule that tells the parsers how to do this. It could change release to release, or parser to parser.

The safest and most economical way is to build a StringBuffer or StringBuilder in the startElement method. In the characters method append what you are given to the buffer/builder There is even an append method with the same paramters and sequence as in the call to characters. When you get to the endElement, you can do something with the StringBuilder or StringBuffer and you can be sure that it is complete.

Dave Patterson

d.pattersona at 2007-7-14 23:32:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Thanks a lot for your help!It's working just fine now.
powallina at 2007-7-14 23:32:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...