Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
31284 WSD: Lab 4 Exercise 2 - Using the StAX parser 31268 Web Services Development Lab 4: XML parsing Exercise 3 - Using the StAX parser This exercise uses the StAX parser to parse an XML document and print out the document in a JSP. Level of Difficulty: Difficult (many new concepts, difficult program) Estimated time: 0.5 - 1 hours Pre-requisites: Created directory structure in your public_html directory as indicated in lab 2. Read one or more articles introducing StAX. The Streaming API for XML We have seen from the DOM and SAX examples that you can either do a Document-centric parse of an XML file, or you can have an event driven parse of XML. In this exercise, you will use the 3rd alternative - the pull parser. First, this exercise assumes that you are running Java 6 We will use the javax.xml.stream.* package, which comes by default with Java 6. However, one important package is missing from the default Sun configuration. Running this exercise may result in a message called Provider com.bea.xml.stream.MXParserFactory not found If you see this, you will need to copy the sjsxp.jar file into your /public_html/WEB-INF/lib directory. See http://forums.java.net/jive/thread.jspa?messageID=117971 If you are using eclipse and your own tomcat server, you can also copy the Metro webservices-rt.jar file from /pub/dca/code/lib directory We already have this installed on the faculty tomcat server, so you don't have to do this step. Coding StAX Unlike DOM and SAX, with StAX you need to "pull" each line from the XML document as you read it. You then choose the course of action to use depending on what event is returned by the parser. The general format of your code would look like: // create the factory & parse the file XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader parser = factory.createXMLStreamReader(inputstream); // inputstream is the XML file... // Begin traversing the document while (parser.hasNext()) { // Find the type of the current node int event = parser.next(); // Check the node type, and process it accordingly switch (event) { case XMLStreamConstants.START_ELEMENT: { String elementName = parser.getLocalName(); out.println("ELEMENT: [" + elementName + "]); /* get attributes (if any) */ for (int i=0; i < parser.getAttributeCount(); i++) { out.print("ATTRIBUTE: [" + parser.getAttributeLocalName(i) + "]="); out.println(parser.getAttributeValue(i)); } break; } // don't forget the other types of events .... // ???? default: break; } } These events can be found in the class javax.xml.stream.XMLStreamConstants and the particular constants are: Field Summary ATTRIBUTE           Indicates an event is an attribute CDATA           Indicates an event is a CDATA section CHARACTERS           Indicates an event is characters COMMENT           Indicates an event is a comment DTD           Indicates an event is a DTD END_DOCUMENT           Indicates an event is an end document END_ELEMENT           Indicates an event is an end element ENTITY_DECLARATION           Indicates a Entity Declaration ENTITY_REFERENCE           Indicates an event is an entity reference NAMESPACE           Indicates the event is a namespace declaration NOTATION_DECLARATION           Indicates a Notation PROCESSING_INSTRUCTION           Indicates an event is a processing instruction SPACE           The characters are white space (see [XML], 2.10 "White Space Handling"). START_DOCUMENT           Indicates an event is a start document START_ELEMENT           Indicates an event is a start element Exercise: converting the SAX lab to StAX Now you can try converting the SAX example from the previous labs to use StAX. Note that you need to import javax.xml.stream.* and javax.xml.parsers.* in your JSP. Final hints: Don't forget to catch exceptions, and also have a default: statement in your switch statement to catch other cases References Java 5 StAX tutorial : http://java.sun.com/javaee/5/docs/tutorial/doc/bnbem.html JavaBoutique: http://javaboutique.internet.com/tutorials/stax/ Read and create RSS feeds via Java (Stax): http://www.vogella.de/articles/RSSFeed/article.html © University of Technology, Sydney. All Rights Reserved. Redistribution without permission prohibited.