Forum Discussion
ATokeley
14 years agoContributor
Does this code make sense to you guys? It runs without error, but doesn't seem to be validating my response xml correctly. All feedback welcome!
import java.util.ArrayList
import javax.xml.XMLConstants
import javax.xml.transform.Source
import javax.xml.transform.stream.StreamSource
import org.xml.sax.InputSource;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
def resultXML = context.expand('${#resultsXML}' )
def schemaPath = context.expand ( '${#schemaPath}' )
def schemaFiles = context.expand ( '${#schemaFiles}' )
def schemaSources = new ArrayList()
def st = new StringTokenizer(schemaFiles,';')
while (st.hasMoreTokens()) { schemaSources.add( new StreamSource(new FileInputStream(schemaPath + st.nextToken() ) ) ) }
def schemaFactory = javax.xml.validation.SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
def schema = schemaFactory.newSchema((Source[])schemaSources.toArray());
def validator = schema.newValidator();
def stringReader = new StringReader(resultXML);
def inputSource = new InputSource(stringReader);
// set it so that it is validating and namespace aware
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setValidating( true );
parserFactory.setNamespaceAware( true );
parserFactory.setSchema( schema );
def parseHandler = new org.xml.sax.helpers.DefaultHandler();
try {
log.info "Starting to parse file...";
def saxReader = parserFactory.newSAXParser();
saxReader.parse(inputSource, parseHandler);
log.info "Done parsing file...";
}
catch (Exception e) {
log.info(e);
}
Related Content
- 2 years ago
- 2 years ago
- 13 years ago
Recent Discussions
- 6 days ago
- 10 days ago