- package com.xml.parseXml;
- import java.io.InputStream;
- import javax.xml.stream.XMLEventReader;
- import javax.xml.stream.XMLInputFactory;
- import javax.xml.stream.events.StartElement;
- import javax.xml.stream.events.XMLEvent;
- import com.xml.bean.Address;
- import com.xml.bean.ReadXmlFileStream;
- public class StAXParseXml {
- private static Address address = new Address();
- public static void main(String[] args) {
- long lasting = System.currentTimeMillis();
- try {
- InputStream in = ReadXmlFileStream.getXmlFileStream();
- XMLInputFactory xmlif = XMLInputFactory.newInstance();
- // 用于解析 XML 事件的顶层接口
- XMLEventReader reader = xmlif.createXMLEventReader(in);
- // 处理标记事件的基础事件接口
- XMLEvent event = null ;
- while (reader.hasNext()) {
- event = reader.nextEvent();
- if (event.isStartElement()) { // 起始元素
- StartElement startElt = event.asStartElement();
- if (startElt.getName().getLocalPart().equals( "no" )) {
- address.setNo(reader.getElementText());
- } else if (startElt.getName().getLocalPart().equals( "addr" )) {
- address.setAddr(reader.getElementText());
- System.out.println(address);
- }
- } else if (event.isCharacters()) { // 文本内容
- // 相邻标记之间都是文本内容
- System.out.println( "解析的是文本内容:"
- + event.asCharacters().getData());
- } else if (event.isEndElement()) { // 结束元素
- System.out.println( "解析的是结束标记:"
- + event.asEndElement().getName());
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println( "运行时间:" + (System.currentTimeMillis() - lasting)
- + " 毫秒" );
- }
- }
- package com.xml.parseXml;
- import java.io.InputStream;
- import javax.xml.stream.XMLEventReader;
- import javax.xml.stream.XMLInputFactory;
- import javax.xml.stream.events.StartElement;
- import javax.xml.stream.events.XMLEvent;
- import com.xml.bean.Address;
- import com.xml.bean.ReadXmlFileStream;
- public class StAXParseXml {
- private static Address address = new Address();
- public static void main(String[] args) {
- long lasting = System.currentTimeMillis();
- try {
- InputStream in = ReadXmlFileStream.getXmlFileStream();
- XMLInputFactory xmlif = XMLInputFactory.newInstance();
- // 用于解析 XML 事件的顶层接口
- XMLEventReader reader = xmlif.createXMLEventReader(in);
- // 处理标记事件的基础事件接口
- XMLEvent event = null ;
- while (reader.hasNext()) {
- event = reader.nextEvent();
- if (event.isStartElement()) { // 起始元素
- StartElement startElt = event.asStartElement();
- if (startElt.getName().getLocalPart().equals( "no" )) {
- address.setNo(reader.getElementText());
- } else if (startElt.getName().getLocalPart().equals( "addr" )) {
- address.setAddr(reader.getElementText());
- System.out.println(address);
- }
- } else if (event.isCharacters()) { // 文本内容
- // 相邻标记之间都是文本内容
- System.out.println( "解析的是文本内容:"
- + event.asCharacters().getData());
- } else if (event.isEndElement()) { // 结束元素
- System.out.println( "解析的是结束标记:"
- + event.asEndElement().getName());
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println( "运行时间:" + (System.currentTimeMillis() - lasting)
- + " 毫秒" );
- }
- }
package com.xml.parseXml; import java.io.InputStream; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; import com.xml.bean.Address; import com.xml.bean.ReadXmlFileStream; public class StAXParseXml { private static Address address = new Address(); public static void main(String[] args) { long lasting = System.currentTimeMillis(); try { InputStream in = ReadXmlFileStream.getXmlFileStream(); XMLInputFactory xmlif = XMLInputFactory.newInstance(); // 用于解析 XML 事件的顶层接口 XMLEventReader reader = xmlif.createXMLEventReader(in); // 处理标记事件的基础事件接口 XMLEvent event = null; while (reader.hasNext()) { event = reader.nextEvent(); if (event.isStartElement()) { // 起始元素 StartElement startElt = event.asStartElement(); if (startElt.getName().getLocalPart().equals("no")) { address.setNo(reader.getElementText()); } else if (startElt.getName().getLocalPart().equals("addr")) { address.setAddr(reader.getElementText()); System.out.println(address); } } else if (event.isCharacters()) { // 文本内容 // 相邻标记之间都是文本内容 System.out.println("解析的是文本内容:" + event.asCharacters().getData()); } else if (event.isEndElement()) { // 结束元素 System.out.println("解析的是结束标记:" + event.asEndElement().getName()); } } } catch (Exception e) { e.printStackTrace(); } System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); } }
依赖的类:
1.Address .java
- package com.xml.bean;
- public class Address {
- private String no;
- private String addr;
- /**
- * @return the no
- */
- public String getNo() {
- return no;
- }
- /**
- * @param no the no to set
- */
- public void setNo(String no) {
- this .no = no;
- }
- /**
- * @return the addr
- */
- public String getAddr() {
- return addr;
- }
- /**
- * @param addr the addr to set
- */
- public void setAddr(String addr) {
- this .addr = addr;
- }
- @Override
- public String toString() {
- return "编号:" + this .no + ",地址:" + this .addr;
- }
- }
- package com.xml.bean;
- public class Address {
- private String no;
- private String addr;
- /**
- * @return the no
- */
- public String getNo() {
- return no;
- }
- /**
- * @param no the no to set
- */
- public void setNo(String no) {
- this .no = no;
- }
- /**
- * @return the addr
- */
- public String getAddr() {
- return addr;
- }
- /**
- * @param addr the addr to set
- */
- public void setAddr(String addr) {
- this .addr = addr;
- }
- @Override
- public String toString() {
- return "编号:" + this .no + ",地址:" + this .addr;
- }
- }
package com.xml.bean; public class Address { private String no; private String addr; /** * @return the no */ public String getNo() { return no; } /** * @param no the no to set */ public void setNo(String no) { this.no = no; } /** * @return the addr */ public String getAddr() { return addr; } /** * @param addr the addr to set */ public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { return "编号:" + this.no + ",地址:" + this.addr; } }
2.ReadXmlFileStream .java
- /**
- *
- */
- package com.xml.bean;
- import java.io.InputStream;
- public class ReadXmlFileStream {
- private static final String XML_FILE = "com/xml/bean/addresses.xml" ;
- public static InputStream getXmlFileStream() {
- return Thread.currentThread().getContextClassLoader()
- .getResourceAsStream(XML_FILE);
- }
- }
- /**
- *
- */
- package com.xml.bean;
- import java.io.InputStream;
- public class ReadXmlFileStream {
- private static final String XML_FILE = "com/xml/bean/addresses.xml" ;
- public static InputStream getXmlFileStream() {
- return Thread.currentThread().getContextClassLoader()
- .getResourceAsStream(XML_FILE);
- }
- }
/** * */ package com.xml.bean; import java.io.InputStream; public class ReadXmlFileStream { private static final String XML_FILE = "com/xml/bean/addresses.xml"; public static InputStream getXmlFileStream() { return Thread.currentThread().getContextClassLoader() .getResourceAsStream(XML_FILE); } }
被解析的XML结构:
addresses.xml
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < addresses >
- < value >
- < no > A100 </ no >
- < addr > 广州天河区天河路 </ addr >
- </ value >
- < value >
- < no > A101 </ no >
- < addr > 广州天河区五山路 </ addr >
- </ value >
- </ addresses >
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < addresses >
- < value >
- < no > A100 </ no >
- < addr > 广州天河区天河路 </ addr >
- </ value >
- < value >
- < no > A101 </ no >
- < addr > 广州天河区五山路 </ addr >
- </ value >
- </ addresses >