使用Java StAX API(XML Stream Process API)写的XMLParser

最近我使用Java StAX API(XML Stream Process API)写的XMLParser。具体说明以后补充。代码如下:

public class XmlParser implements IParser {
	private XMLEventReader reader; 
	private BufferedReader bufferedReader;
	private static final String filepath = "";
	
	public XmlParser() throws FileNotFoundException, XMLStreamException{
		
		File file = new File(XmlParser.class.getResource(filepath).getFile());
//		System.out.println("File Path:" + file.getPath());
		bufferedReader = new BufferedReader(new FileReader(file));
		XMLInputFactory factory = XMLInputFactory.newInstance();
		reader = factory.createXMLEventReader(bufferedReader);		
		
	}
	
	/*
	 * first invoke existTag() function
	 * get the element text of the given element
	 */
	public String parseTagValue(String tag) {
		// TODO Auto-generated method stub
		QName qname = new QName(tag);
		String value = null;

		try {
			if (existTag(tag)) {
				XMLEvent event = reader.nextEvent();
				if (event.isStartElement()) {
					StartElement element = event.asStartElement();
					if (qname.equals(element.getName())) {
						value = reader.getElementText();
					}
				}
			}

		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		return value;
	}

	/*
	 * get the attribute value of the given attribute of the given element
	 */
	public String parseTagAttributeValue(String tag, String attribute) {
		// TODO Auto-generated method stub
		QName elementName = new QName(tag);
		QName attributeName = new QName(attribute);
		String value = null;
		
		try{
			if(existTag(tag)){
				XMLEvent event = reader.peek();
				if (event.isStartElement()) {
					StartElement element = event.asStartElement();
					if (elementName.equals(element.getName())) {
						Attribute a = element.getAttributeByName(attributeName);
						if(a != null)
							value = a.getValue();
					}
				}
			}
		}catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		return value;
		
	}

	/*
	 * judge if exist the given element
	 * and move the pointer to the previous XMLEvent of the given XMLEvent
	 */
	public boolean existTag(String tag) {
		// TODO Auto-generated method stub
        QName qname = new QName(tag);
		
			try {	
				while(reader.hasNext()){
					XMLEvent event = reader.peek();
					if(event.isStartElement()) {
						StartElement element = event.asStartElement();
						if(qname.equals(element.getName())){
							return true;
						}
					}
					
					reader.nextEvent();
				}
				
			} catch (XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		
		return false;
	}
	/*
	 * get the name of the next tag
	 */
	public String getNextTagName(){
		String name = "";
		try {
			while (reader.hasNext()) {
				XMLEvent event;
				event = reader.peek();
				if (event.isStartElement()) {
					StartElement start = event.asStartElement();
					name = start.getName().getLocalPart();
					break;
				}
				reader.nextEvent();

			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}

		return name;
	}
	
	/*
	 * move the pointer previous to the next start tag 
	 */
	public void nextStartTag(){
			try {	
				while(reader.hasNext()){
					XMLEvent event = reader.nextEvent();
					if(event.isStartElement()) 
						break;				
					
					reader.nextEvent();
				}
				
			} catch (XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}

	public void close() throws XMLStreamException, IOException{
		reader.close();
		bufferedReader.close();
	}
}


你可能感兴趣的:(Java)