Stax Parsing
Here, I will provide STAX parsing Outline.
STAX is streaming parsing API.
Now the question arises, that why should we use STAX, instead of DOM,SAX,JAXP (well known tools for working with the XML infosets) and other XML parsing API's, what extra benefit does STAX provides us, so here it is:
1) StAX is a streaming PULL-PUSH parsing java API for reading and writing XMLs.
2) StAX API is a set of interfaces providing a framework for bi-directional parser. It provides two sets of APIs, cursor and iterator based.
3) Unlike DOM it doesn't load the entire Document tree in the memory(i.e. In Memory Objects), but it uses the Streaming Architecture just like File Streaming and thus reducing memory footprint.
4) Like JDBC StAX is a standard, and there are different implementations confirming to those standards like stax.codehaus, sjsxp,woodstox etc.
5) The Streaming API provides us with advantage of reading and writing large XML documents sequentially, performing operation on a particular element at a time.
6) In PULL model, the Application thread asks the PARSING API for an element and the PARSING API parses the XML infoset and returns the element to the Application.
7) In PUSH model, it is the PARSING API that parses the XML infoset and returns the elements to the client when an event is encountered by the PARSING API.
For any furthur reference, the utility links are:
a) http://docs.oracle.com/javase/tutorial/jaxp/stax/why.html
b) http://docs.oracle.com/javaee/5/tutorial/doc/bnbfl.html
Now the XML, we are parsing here is the SAMLresponse XML from the from the SAML ticket validator. The XML infoset is fairly complex, so the code here used for parsing can go through almost all possible scenarios.
THE XML USED HERE IS:
Only XML Parsing code is attached here, If XML forming code is required, Please mention it in the comment i will post it.
As during CAS confuguration I do not came across situation to write XML file so I have not mentioned it here.
So this is all in short about CAS configuration. So, happy reading and if any issues are encountered please do mention it in the comment, I would love to encounter those issues.
STAX is streaming parsing API.
Now the question arises, that why should we use STAX, instead of DOM,SAX,JAXP (well known tools for working with the XML infosets) and other XML parsing API's, what extra benefit does STAX provides us, so here it is:
1) StAX is a streaming PULL-PUSH parsing java API for reading and writing XMLs.
2) StAX API is a set of interfaces providing a framework for bi-directional parser. It provides two sets of APIs, cursor and iterator based.
3) Unlike DOM it doesn't load the entire Document tree in the memory(i.e. In Memory Objects), but it uses the Streaming Architecture just like File Streaming and thus reducing memory footprint.
4) Like JDBC StAX is a standard, and there are different implementations confirming to those standards like stax.codehaus, sjsxp,woodstox etc.
5) The Streaming API provides us with advantage of reading and writing large XML documents sequentially, performing operation on a particular element at a time.
6) In PULL model, the Application thread asks the PARSING API for an element and the PARSING API parses the XML infoset and returns the element to the Application.
7) In PUSH model, it is the PARSING API that parses the XML infoset and returns the elements to the client when an event is encountered by the PARSING API.
For any furthur reference, the utility links are:
a) http://docs.oracle.com/javase/tutorial/jaxp/stax/why.html
b) http://docs.oracle.com/javaee/5/tutorial/doc/bnbfl.html
Now the XML, we are parsing here is the SAMLresponse XML from the from the SAML ticket validator. The XML infoset is fairly complex, so the code here used for parsing can go through almost all possible scenarios.
THE XML USED HERE IS:
public class EventBasedRead { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String inputXML = "";; XMLInputFactory xmlInputFactory; XMLEventReader xmlEventReader; StartElement startElement = null; XMLEvent parEvent = null; try{ xmlInputFactory = getXMLInputFactory(); xmlEventReader = xmlInputFactory.createXMLEventReader(new StringReader(inputXML)); while(xmlEventReader.hasNext()) { XMLEvent xmlEvent = xmlEventReader.nextEvent(); if(xmlEvent.isStartElement()) { startElement = xmlEvent.asStartElement(); System.out.println(" ******** Start of Element: "+
startElement.getName().getLocalPart()+" ******** "); System.out.println("NameSpace:"+startElement.getName().getNamespaceURI()); System.out.println("Prefix:"+startElement.getName().getPrefix()); IteratorattrIterator = startElement.getAttributes(); while(attrIterator.hasNext()) { Attribute attr = (Attribute)attrIterator.next(); System.out.println(attr.getName()+":"+attr.getValue()); } /*if(startElement.getName().getLocalPart().equals("Type")) { xmlEvent = xmlEventReader.nextEvent(); System.out.println("Data for Type:"+xmlEvent.asCharacters().getData()); }*/ } else if(xmlEvent.isStartDocument()) { System.out.println(" *** Start of Document *** "); /*System.out.println(" #### Value #### "); System.out.println(xmlEventReader.nextEvent().asCharacters().toString()); System.out.println(" #### Value End #### ");*/ } else if(xmlEvent.isEndElement()) { //System.out.println(" *** End of element *** "); EndElement endElement = xmlEvent.asEndElement(); System.out.println(" **** End of Element: "+
endElement.getName().getLocalPart()+" **** \n\n"); } else if(xmlEvent.isEndDocument()) { System.out.println(" *** End of Document *** "); //EndDocument endDocument = xmlEvent.a } else { System.out.println(" The data for "+
((StartElement)parEvent).getName().getLocalPart()+" is:"+xmlEvent.asCharacters().getData()); } parEvent = xmlEvent; } } catch(Exception e) { e.printStackTrace(); } } public static XMLInputFactory getXMLInputFactory() { XMLInputFactory xmlFactory = null; try{ xmlFactory = XMLInputFactory.newInstance(); xmlFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,Boolean.TRUE); xmlFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,Boolean.FALSE); //IS_COALESCING property to true //gets whole text data as one event xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); return xmlFactory; } catch(Exception e) { xmlFactory = null; e.printStackTrace(); return xmlFactory; } }
Only XML Parsing code is attached here, If XML forming code is required, Please mention it in the comment i will post it.
As during CAS confuguration I do not came across situation to write XML file so I have not mentioned it here.
So this is all in short about CAS configuration. So, happy reading and if any issues are encountered please do mention it in the comment, I would love to encounter those issues.
Comments
Post a Comment