📄 staxeventsprinter.java
字号:
package org.chinthaka.articles.stax;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamException;import java.io.FileInputStream;import java.io.FileNotFoundException;/* * Copyright 2004,2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */public class StaxEventsPrinter { public static final String RESOURCES_FOLDER = "resources"; public void print(String fileLocation) { try { FileInputStream fileInputStream = new FileInputStream(fileLocation); XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fileInputStream); while (xmlStreamReader.hasNext()) { printEventInfo(xmlStreamReader); System.out.println("========================"); } xmlStreamReader.close(); } catch (XMLStreamException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (FileNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } private static void printEventInfo(XMLStreamReader reader) throws XMLStreamException { int eventCode = reader.next(); switch (eventCode) { case 1 : System.out.println("event = START_ELEMENT"); System.out.println("Localname = "+reader.getLocalName()); break; case 2 : System.out.println("event = END_ELEMENT"); System.out.println("Localname = "+reader.getLocalName()); break; case 3 : System.out.println("event = PROCESSING_INSTRUCTION"); System.out.println("PIData = " + reader.getPIData()); break; case 4 : System.out.println("event = CHARACTERS"); System.out.println("Characters = " + reader.getText()); break; case 5 : System.out.println("event = COMMENT"); System.out.println("Comment = " + reader.getText()); break; case 6 : System.out.println("event = SPACE"); System.out.println("Space = " + reader.getText()); break; case 7 : System.out.println("event = START_DOCUMENT"); System.out.println("Document Started."); break; case 8 : System.out.println("event = END_DOCUMENT"); System.out.println("Document Ended"); break; case 9 : System.out.println("event = ENTITY_REFERENCE"); System.out.println("Text = " + reader.getText()); break; case 11 : System.out.println("event = DTD"); System.out.println("DTD = " + reader.getText()); break; case 12 : System.out.println("event = CDATA"); System.out.println("CDATA = " + reader.getText()); break; } } public static void main(String[] args) { StaxEventsPrinter eventsPrinter = new StaxEventsPrinter(); eventsPrinter.print(RESOURCES_FOLDER + "/sample1.xml"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -