Stax處理XML(一)——基於游標的查詢

林竟發表於2013-12-31

Stax處理XML,基於游標的查詢,關鍵在於:

使用XMLInputFactory建立XMLStreamReader,獲取了streamReader(游標)之後,(在while迴圈hasNext中)判斷節點型別,是否為開始、結束、文字節點,之後根據情況進行處理。

(1)測試一:輸出節點名稱和text值

	/**
	 * 基於游標的查詢
	 * 輸出節點名稱和text值
	 */
	@Test
	public void test01() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			//輸入流
			is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
			XMLStreamReader reader = factory.createXMLStreamReader(is);
			while(reader.hasNext()) {
				int type = reader.next();
				//判斷節點型別,是否為開始、結束、文字節點,之後根據情況進行處理
				if (type == XMLStreamConstants.START_ELEMENT) {
					System.out.println(reader.getName());
				} else if (type == XMLStreamConstants.CHARACTERS) {
					System.out.println(reader.getText().trim());
				} else if (type == XMLStreamConstants.END_ELEMENT) {
					System.out.println("/" + reader.getName());
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				if(is != null)	is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

輸出結果:

bookstore

book

title
Everyday Italian
/title

author
Giada De Laurentiis
/author

year
2005
/year

price
30.00
/price

/book

book
...(此處省略)
/book

/bookstore

(2)測試二:輸出屬性,如果是book,就輸出category

 

	/**
	 * 輸出屬性
	 * 如果是book,就輸出category
	 */
	@Test
	public void test02() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
			XMLStreamReader reader = factory.createXMLStreamReader(is);
			while(reader.hasNext()) {
				int type = reader.next();
				if (type == XMLStreamConstants.START_ELEMENT) {
					String name = reader.getName().toString();
					if (name.equals("book")) {
						System.out.println(reader.getAttributeName(0) + ": " + reader.getAttributeValue(0));
					}
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				if(is != null)	is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


輸出結果:

category: COOKING
category: CHILDREN
category: WEB
category: WEB

(3)測試三:輸出書的標題和價格

	/**
	 * 輸出書的標題和價格
	 */
	@Test
	public void test03() {
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		try {
			is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
			XMLStreamReader reader = factory.createXMLStreamReader(is);
			while(reader.hasNext()) {
				int type = reader.next();
				if (type == XMLStreamConstants.START_ELEMENT) {
					String name = reader.getName().toString();
					if (name.equals("title")) {
						System.out.print(reader.getElementText() + ": ");
					}
					if (name.equals("price")) {
						System.out.print(reader.getElementText() + "\n");
					}
				}
			}
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} finally {
			try {
				if(is != null)	is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


輸出結果:

Everyday Italian: 30.00
Harry Potter: 29.99
XQuery Kick Start: 49.99
Learning XML: 39.95

相關文章