Read xml with Stax, examples for CURSOR and ITERATOR api
If you are familiar with SAX it will be much easier to understand
To choose between them you can visit (bottom of the page)
http://docs.oracle.com/javase/tutorial/jaxp/stax/api.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
public class JavaStaxReadEvent { public static void main(String[] args) { try { String fileLocation = "file.xml"; XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); FileInputStream input = new FileInputStream(fileLocation); XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(input); while (xmlEventReader.hasNext()) { xmlEventReader.next(); // check for start element tag if (xmlEventReader.peek() != null && xmlEventReader.peek().isStartElement()) { StartElement startelement = xmlEventReader.peek().asStartElement(); System.out.println("Elementname: " + startelement.getName().toString()); // get attribute iterator Iterator iterator = startelement.getAttributes(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); //print out the attribute name and value System.out.print(attribute.getName() + ": "); System.out.println(attribute.getValue()); } } // the current event is characters and the content is not all white space if (xmlEventReader.peek() != null && xmlEventReader.peek().isCharacters()) { Characters characters = xmlEventReader.peek().asCharacters(); if (characters.getData().trim().length() > 0) { System.out.println("Text: " + characters.getData()); } } if (xmlEventReader.peek() != null && xmlEventReader.peek().isEndElement()) { System.out.println(); } } } catch (FileNotFoundException ex) { } catch (XMLStreamException ex) { } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
public class JavaStaxRead { public static void main(String[] args) { FileInputStream fileInputStream; try { String fileLocation = "file.xml"; fileInputStream = new FileInputStream(fileLocation); // Iterator class for XML, works just like normal iterator wit next and hasNext XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fileInputStream); // iterate as long as there are more events while (xmlStreamReader.hasNext()) { // go to next event xmlStreamReader.next(); // get current event type if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) { System.out.println("Elementname: " + xmlStreamReader.getLocalName()); if (xmlStreamReader.getAttributeCount() > 0) { // get attribute count for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { System.out.print(xmlStreamReader.getAttributeName(i).toString() + ": "); System.out.println(xmlStreamReader.getAttributeValue(i).toString()); } } } // the current event is characters and the content is not all white space if ((xmlStreamReader.getEventType() == XMLStreamConstants.CHARACTERS) && (xmlStreamReader.getText().trim().length() > 0)) { System.out.println("Text: " + xmlStreamReader.getText()); } if (xmlStreamReader.getEventType() == XMLStreamConstants.END_ELEMENT) { System.out.println(); } } xmlStreamReader.close(); } catch (XMLStreamException ex) { } catch (FileNotFoundException ex) { } } } |