List of usage examples for javax.xml.stream XMLEventReader nextEvent
public XMLEvent nextEvent() throws XMLStreamException;
From source file:Main.java
public static void main(String args[]) throws Exception { XMLInputFactory xmlif = XMLInputFactory.newInstance(); XMLEventReader xmler = xmlif.createXMLEventReader(new FileReader("yourFile.xml")); XMLEvent event;/*from w w w . ja v a 2 s . c om*/ while (xmler.hasNext()) { event = xmler.nextEvent(); if (event.isStartElement()) { System.out.println(event.asStartElement().getName()); } else if (event.isCharacters()) { System.out.println(event.asCharacters().getData()); } } }
From source file:XMLEventReaderDemo.java
public static void main(String[] args) throws Exception { XMLInputFactory factory = XMLInputFactory.newInstance(); Reader fileReader = new FileReader("Source.xml"); XMLEventReader reader = factory.createXMLEventReader(fileReader); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement element = (StartElement) event; System.out.println("Start Element: " + element.getName()); Iterator iterator = element.getAttributes(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); QName name = attribute.getName(); String value = attribute.getValue(); System.out.println("Attribute name/value: " + name + "/" + value); }/*from w ww . ja v a 2 s.c o m*/ } if (event.isEndElement()) { EndElement element = (EndElement) event; System.out.println("End element:" + element.getName()); } if (event.isCharacters()) { Characters characters = (Characters) event; System.out.println("Text: " + characters.getData()); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { String filename = "yourXML.xml"; XMLInputFactory factory = XMLInputFactory.newInstance(); System.out.println("FACTORY: " + factory); XMLEventReader r = factory.createXMLEventReader(filename, new FileInputStream(filename)); while (r.hasNext()) { XMLEvent e = r.nextEvent(); System.out.println(e.toString()); }//w w w . j a v a 2 s.co m }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLEventReader xmlr = xif.createXMLEventReader((new FileInputStream(new File("./file.xml")))); boolean inline = false; StringBuffer sb = new StringBuffer(); while (xmlr.hasNext()) { XMLEvent event = xmlr.nextEvent(); if (event.isStartElement()) { StartElement element = (StartElement) event; if ("data".equals(element.getName().toString().trim())) { inline = true;/*from w w w.ja va 2s . c o m*/ } } if (inline) { sb.append(xmlr.peek()); } if (event.isEndElement()) { EndElement element = (EndElement) event; if ("data".equals(element.getName().toString().trim())) { inline = false; System.out.println(sb.toString()); sb.setLength(0); } } } }
From source file:org.eclipse.swordfish.core.configuration.xml.SaxParsingPrototype.java
/** * @param args/*from w ww .j a va 2 s.c o m*/ */ public static void main(String[] args) throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); LinkedList<String> currentElements = new LinkedList<String>(); InputStream in = SaxParsingPrototype.class.getResource("ComplexPidXmlProperties.xml").openStream(); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); Map<String, List<String>> props = new HashMap<String, List<String>>(); // Read the XML document while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) { putElement(props, getQualifiedName(currentElements), event.asCharacters().getData()); } else if (event.isStartElement()) { System.out.println(event.asStartElement().getName()); currentElements.add(event.asStartElement().getName().getLocalPart()); for (Iterator attrIt = event.asStartElement().getAttributes(); attrIt.hasNext();) { Attribute attribute = (Attribute) attrIt.next(); putElement(props, getQualifiedName(currentElements) + "[@" + attribute.getName() + "]", attribute.getValue()); } } else if (event.isAttribute()) { } else if (event.isEndElement()) { String lastElem = event.asEndElement().getName().getLocalPart(); if (!currentElements.getLast().equals(lastElem)) { throw new UnsupportedOperationException(lastElem + "," + currentElements.getLast()); } currentElements.removeLast(); } } eventReader.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { File source = new File(args[0]); File target = new File(source + ".new"); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); InputStream in = new FileInputStream(source); XMLEventReader reader = inputFactory.createXMLEventReader(in); OutputStream out = new FileOutputStream(target); XMLEventWriter writer = outputFactory.createXMLEventWriter(out); XMLEvent event;//www . j ava 2s .co m boolean deleteSection = false; while (reader.hasNext()) { event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT && event.asStartElement().getName().toString().equalsIgnoreCase("companies")) { deleteSection = true; continue; } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT && (event.asEndElement().getName().toString().equalsIgnoreCase("companies"))) { deleteSection = false; continue; } else if (deleteSection) { continue; } else { writer.add(event); } } }
From source file:Main.java
/** * Calls xml.nextEvent() and unchecks the exception. * /*www . j av a 2 s. co m*/ * @param xml * @return xml.nextEvent(), or throws a RuntimeException. */ public static XMLEvent nextEvent(XMLEventReader xml) { try { return xml.nextEvent(); } catch (XMLStreamException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String digest(InputStream in) throws Exception { MessageDigest messageDigest = null; XMLInputFactory inputFactory = XMLInputFactory.newFactory(); messageDigest = MessageDigest.getInstance("MD5"); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { messageDigest.update(event.asStartElement().getName().toString().getBytes()); } else if (event.isEndElement()) { messageDigest.update(event.asEndElement().getName().toString().getBytes()); }//from w ww .j av a2 s .co m } StringBuffer result = new StringBuffer(); byte[] digest = messageDigest.digest(); for (byte b : digest) { result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1)); } return result.toString(); }
From source file:Main.java
private static String flattenXmlRecord(final String xmlRecord) throws XMLStreamException { StringBuilder flatXmlRecord = new StringBuilder(); XMLEventReader eventReader = inFactory.createXMLEventReader(new StringReader(xmlRecord)); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.getEventType() != XMLEvent.START_ELEMENT) { continue; }//from w w w. j a va2 s . c om String elementName = event.asStartElement().getName().getLocalPart(); event = eventReader.nextEvent(); if (event.getEventType() != XMLEvent.CHARACTERS) { continue; } if (event.asCharacters().getData().trim().isEmpty()) { continue; } flatXmlRecord.append(elementName + ", " + event.asCharacters().getData() + "\n"); } return flatXmlRecord.toString(); }
From source file:Main.java
/** * Describe <code>getInternalXML</code> method here. * * @param is an <code>InputStream</code> value * @param tagName a <code>String</code> value * @param namespace a <code>String</code> value * @return a <code>byte[]</code> value * @exception Exception if an error occurs *//* w w w . j a va 2s . co m*/ public static byte[] getInternalXML(InputStream is, String tagName, String namespace) throws Exception { if (namespace == null) namespace = ""; ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = outputFactory.createXMLEventWriter(bos); // Loop over XML input stream and process events boolean bStartWritingEvents = false; XMLEventReader reader = inputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (checkNameAndNamespace(event, tagName, namespace) == true) { if (event.isStartElement() && !bStartWritingEvents) { writer.add(eventFactory.createStartDocument()); bStartWritingEvents = true; continue; } else if (event.isEndElement()) { writer.add(eventFactory.createEndDocument()); bStartWritingEvents = false; break; } } if (bStartWritingEvents) { writer.add(event); } } writer.close(); reader.close(); return bos.toByteArray(); }