开发手册 欢迎您!
软件开发者资料库

Java StAX Parser - 解析XML文档

Java StAX Parser解析XML文档 - 从简单和简单的步骤学习Java XML,从基本到高级概念,包括概述,Java XML解析器,DOM解析器,解析XML文档,查询XML文档,创建XML文档,修改XML文档,SAX Parser,JDOM XML Parser,StAX Parser,XPath Parser,DOM4J Parser,XSLT,JAXB。

演示示例

以下是我们需要解析和减去的输入xml文件;

         dinkar      kad      dinkar      85               Vaneet      Gupta      vinni      95               jasvir      singn      jazz      90   

StAXParserDemo.java

package com.it1352.xml; import java.io.FileNotFoundException;import java.io.FileReader;import java.util.Iterator;import javax.xml.stream.XMLEventReader;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import javax.xml.stream.events.Attribute;import javax.xml.stream.events.Characters;import javax.xml.stream.events.EndElement;import javax.xml.stream.events.StartElement;import javax.xml.stream.events.XMLEvent;public class StAXParserDemo {   public static void main(String[] args) {      boolean bFirstName = false;      boolean bLastName = false;      boolean bNickName = false;      boolean bMarks = false;            try {         XMLInputFactory factory = XMLInputFactory.newInstance();         XMLEventReader eventReader =         factory.createXMLEventReader(new FileReader("input.txt"));         while(eventReader.hasNext()) {            XMLEvent event = eventReader.nextEvent();                           switch(event.getEventType()) {                              case XMLStreamConstants.START_ELEMENT:                  StartElement startElement = event.asStartElement();                  String qName = startElement.getName().getLocalPart();               if (qName.equalsIgnoreCase("student")) {                  System.out.println("Start Element : student");                  Iterator attributes = startElement.getAttributes();                  String rollNo = attributes.next().getValue();                  System.out.println("Roll No : " + rollNo);               } else if (qName.equalsIgnoreCase("firstname")) {                  bFirstName = true;               } else if (qName.equalsIgnoreCase("lastname")) {                  bLastName = true;               } else if (qName.equalsIgnoreCase("nickname")) {                  bNickName = true;               }               else if (qName.equalsIgnoreCase("marks")) {                  bMarks = true;               }               break;               case XMLStreamConstants.CHARACTERS:                  Characters characters = event.asCharacters();               if(bFirstName) {                  System.out.println("First Name: " + characters.getData());                  bFirstName = false;               }               if(bLastName) {                  System.out.println("Last Name: " + characters.getData());                  bLastName = false;               }               if(bNickName) {                  System.out.println("Nick Name: " + characters.getData());                  bNickName = false;               }               if(bMarks) {                  System.out.println("Marks: " + characters.getData());                  bMarks = false;               }               break;               case XMLStreamConstants.END_ELEMENT:                  EndElement endElement = event.asEndElement();                                 if(endElement.getName().getLocalPart().equalsIgnoreCase("student")) {                  System.out.println("End Element : student");                  System.out.println();               }               break;            }          }      } catch (FileNotFoundException e) {         e.printStackTrace();      } catch (XMLStreamException e) {         e.printStackTrace();      }   }}

这会产生以下结果 :

Start Element : studentRoll No : 393First Name: dinkarLast Name: kadNick Name: dinkarMarks: 85End Element : studentStart Element : studentRoll No : 493First Name: VaneetLast Name: GuptaNick Name: vinniMarks: 95End Element : studentStart Element : studentRoll No : 593First Name: jasvirLast Name: singnNick Name: jazzMarks: 90End Element : student
T" }) F-8" id="LA_COLLECT" src="//sdk.51.la/js-sdk-pro.min.js">