Jsp My Titles3Java

From Fxp Wiki

Jump to: navigation, search
 
package vendo;
 
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import java.io.*;
 
public class MyTitles3 extends DefaultHandler {
 
private Writer out;
String local0="";
 
	// constructor
    	public MyTitles3 (String xmlFile, Writer out) {
 
        		this.out = out;
        		//  create a Xerces SAX parser
        		SAXParser parser = new SAXParser();
 
        		//  set the content handler
        		parser.setContentHandler(this);
        		//  parse the document
        		try {
            			parser.parse(xmlFile);
			out.flush();
        		} catch (SAXException e) {
            			// something went wrong!
        		} catch (IOException e) {
            			// something went wrong!
        		}
    	}
 
	// call at document start
	public void startDocument() {
 
		try {
			out.write ("<h1>Document begins</h1><br>");
		} catch (IOException e) {
			// do nothing
		}
	}
 
	// call at element start
    	public void startElement (String uri, String local, String qName,
Attributes atts) throws SAXException {
 
		try {
			if (!local0.equals("jw:titles")){
		        out.write ("<h2>Element begins: \"" + local + "\"</h2>");
			out.write ("<ul><li>uri = " + uri + "</li><li>local = " + local + "</li><li>qName = " + qName + "</li></ul>");
				if (qName.equals("jw:titles")){
					local0 = qName;
				
				String AttributeName,AttributeType,AttributeValue = "";
					for (int i = 0; i < atts.getLength(); i++) {
						AttributeName = atts.getLocalName(i);
						AttributeType = atts.getType(AttributeName);
						AttributeValue = atts.getValue(AttributeName);
						out.write ("<h3>Attribute: \"" + AttributeName + "\"<br>");
						out.write ("&nbsp;&nbsp;&nbsp;Type: \"" + AttributeType + "\"<br>");
						out.write ("&nbsp;&nbsp;&nbsp;Value: \"" + AttributeValue +
		"\"<br></h3>");
					}
				}
			}
			else{
				out.write ("<h2>Element begins: \"" + local + "\"</h2>");
			out.write ("&nbsp; rdf:Alt <br />" + qName);
				if (local.equals("span")){
					out.write ("&nbsp; &nbsp; rdf:li <br />");
				
					String AttributeName,AttributeType,AttributeValue = "";
					for (int i = 0; i < atts.getLength(); i++) 
					{
						AttributeName = atts.getLocalName(i);
						AttributeType = atts.getType(AttributeName);
						AttributeValue = atts.getValue(AttributeName);
						
						if (AttributeName.equals("class")) 
						{
								if (AttributeValue.equals("lgen")){
									out.write ("&lt;dc:title xml:lang=\"" + AttributeValue + "\"&gt;<br>");
									}
								
								
								if (AttributeValue.equals("lgfr")){
									out.write ("&lt;dc:title xml:lang=\"" + AttributeValue + "\"&gt;<br>");
									}
						throw new SAXException("finished");
						}
					}
					
				}
			}
			
		}
		catch (IOException e) {
			// do nothing
		}
    	}
 
	// call when cdata found
    	public void characters(char[] text, int start, int length) {
 
		try {
			String Content = new String(text, start, length);
	        		if (!Content.trim().equals("")){
				out.write("<h4>Character data: \"" + Content + "\"<br></h4>");
			}
		} catch (IOException e) {
			// do nothing
		}
    	}
 
	// call at element end
	public void endElement (String uri, String local, String qName){
		try {
			out.write("<h2> Element ends: \"" + local + "\"<br></h2>");
		} catch (IOException e) {
			// do nothing
		}
	}
 
	// call at document end
	public void endDocument() {
		try {
			out.write ("<h1>Document ends</h1><br>");
		} catch (IOException e) {
			// do nothing
		}
	}
}
 
 
Personal tools