List of usage examples for javax.xml.stream XMLStreamConstants START_DOCUMENT
int START_DOCUMENT
To view the source code for javax.xml.stream XMLStreamConstants START_DOCUMENT.
Click Source Link
From source file:Main.java
public static void printStartDocument(XMLStreamReader xmlr) { if (XMLStreamConstants.START_DOCUMENT == xmlr.getEventType()) { System.out.println("<?xml version=\"" + xmlr.getVersion() + "\"" + " encoding=\"" + xmlr.getCharacterEncodingScheme() + "\"" + "?>"); }/* www .java2 s . c om*/ }
From source file:Main.java
/** Get the human readable event name for the numeric event id */ public static String getEventName(int eventId) { switch (eventId) { case XMLStreamConstants.START_ELEMENT: return "StartElementEvent"; case XMLStreamConstants.END_ELEMENT: return "EndElementEvent"; case XMLStreamConstants.PROCESSING_INSTRUCTION: return "ProcessingInstructionEvent"; case XMLStreamConstants.CHARACTERS: return "CharacterEvent"; case XMLStreamConstants.COMMENT: return "CommentEvent"; case XMLStreamConstants.START_DOCUMENT: return "StartDocumentEvent"; case XMLStreamConstants.END_DOCUMENT: return "EndDocumentEvent"; case XMLStreamConstants.ENTITY_REFERENCE: return "EntityReferenceEvent"; case XMLStreamConstants.ATTRIBUTE: return "AttributeBase"; case XMLStreamConstants.DTD: return "DTDEvent"; case XMLStreamConstants.CDATA: return "CDATA"; }/*from w w w. ja va 2 s .com*/ return "UNKNOWN_EVENT_TYPE"; }
From source file:Main.java
/** * /*from www . j a v a 2s .c om*/ * @param is * @param os * @param elementNames * @throws XMLStreamException * @throws FactoryConfigurationError * @throws UnsupportedEncodingException */ public static void stripElements(final InputStream is, final OutputStream os, final Collection<String> elementNames) throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError { final XMLEventReader xmlEventReader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final XMLEventWriter xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(os); String elementName = null; while (xmlEventReader.peek() != null) { final XMLEvent event = (XMLEvent) xmlEventReader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { final StartElement startElement = event.asStartElement(); final QName name = startElement.getName(); if (elementNames.contains(name.getLocalPart())) { elementName = name.getLocalPart(); } if (elementName == null) { xmlEventWriter.add(event); } break; } case XMLStreamConstants.END_ELEMENT: { final EndElement endElement = event.asEndElement(); final QName name = endElement.getName(); if (elementName == null) { xmlEventWriter.add(event); } else if (elementName.equals(name.getLocalPart())) { elementName = null; } break; } default: { if (elementName == null) { xmlEventWriter.add(event); } } } } xmlEventWriter.flush(); }
From source file:Main.java
/** * Get the string representation of a given StAX event type. The returned * value is the name of the constant in {@link XMLStreamConstants} * corresponding to the event type./*w w w. ja v a 2 s . c o m*/ * * @param event * the event type as returned by * {@link javax.xml.stream.events.XMLEvent#getEventType()}, * {@link javax.xml.stream.XMLStreamReader#getEventType()} or * {@link javax.xml.stream.XMLStreamReader#next()} * @return a string representation of the event type */ public static String getEventTypeString(int event) { String state = null; switch (event) { case XMLStreamConstants.START_ELEMENT: state = "START_ELEMENT"; break; case XMLStreamConstants.START_DOCUMENT: state = "START_DOCUMENT"; break; case XMLStreamConstants.CHARACTERS: state = "CHARACTERS"; break; case XMLStreamConstants.CDATA: state = "CDATA"; break; case XMLStreamConstants.END_ELEMENT: state = "END_ELEMENT"; break; case XMLStreamConstants.END_DOCUMENT: state = "END_DOCUMENT"; break; case XMLStreamConstants.SPACE: state = "SPACE"; break; case XMLStreamConstants.COMMENT: state = "COMMENT"; break; case XMLStreamConstants.DTD: state = "DTD"; break; case XMLStreamConstants.PROCESSING_INSTRUCTION: state = "PROCESSING_INSTRUCTION"; break; case XMLStreamConstants.ENTITY_REFERENCE: state = "ENTITY_REFERENCE"; break; default: state = "UNKNOWN_STATE: " + event; } return state; }
From source file:Main.java
/** * /*from w w w .j a v a 2 s . c om*/ * @param elementName * @param attributeValue * @param is * @return Collection * @throws XMLStreamException * @throws FactoryConfigurationError * @throws UnsupportedEncodingException */ public static Collection<String> getElementValues(final String elementName, final String attributeValue, final InputStream is) throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError { final Collection<String> elementValues = new ArrayList<>(); final XMLEventReader xmlEventReader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final StringBuffer characters = new StringBuffer(); boolean read = false; while (xmlEventReader.peek() != null) { final XMLEvent event = (XMLEvent) xmlEventReader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { read = elementName.equals(event.asStartElement().getName().getLocalPart()); if (read && attributeValue != null) { read = false; for (Iterator<Attribute> iterator = event.asStartElement().getAttributes(); iterator .hasNext();) { Attribute attribute = iterator.next(); if (attribute.getValue().equals(attributeValue)) { read = true; break; } } } break; } case XMLStreamConstants.CHARACTERS: { if (read) { characters.append(event.asCharacters().getData()); } break; } case XMLStreamConstants.END_ELEMENT: { if (read) { elementValues.add(characters.toString()); characters.setLength(0); } read = false; break; } default: { // Ignore break; } } } return elementValues; }
From source file:Main.java
/** * /*from w w w. j a va 2s. co m*/ * @param elementName * @param is * @param onlyValues * @return Collection * @throws XMLStreamException * @throws UnsupportedEncodingException */ public static Collection<String> getElements(final String elementName, final InputStream is, final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException { final Collection<String> elements = new ArrayList<>(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); final XMLEventReader reader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final XMLEventWriter writer = XMLOutputFactory.newInstance() .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name())); boolean read = false; String characters = null; while (reader.peek() != null) { final XMLEvent event = (XMLEvent) reader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { read = read || elementName.equals(event.asStartElement().getName().getLocalPart()); if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.ATTRIBUTE: { if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.CHARACTERS: { if (read && !onlyValues) { writer.add(event); } characters = event.asCharacters().getData(); break; } case XMLStreamConstants.END_ELEMENT: { if (read && !onlyValues) { writer.add(event); } if (elementName.equals(event.asEndElement().getName().getLocalPart())) { writer.flush(); if (characters != null) { elements.add(characters); } os.reset(); read = false; } break; } default: { // Ignore break; } } } return elements; }
From source file:Main.java
/** * Copy an XML event stream./*from w w w . java2 s. co m*/ * @param reader The event reader. * @param writer The event writer. * @param omitDoc if true, ignore start/end document events. * @throws XMLStreamException For errors writing to the XML event writer. */ public static void copyXMLEventStream(final XMLEventReader reader, final XMLEventWriter writer, final boolean omitDoc) throws XMLStreamException { if (omitDoc) { while (reader.hasNext()) { final XMLEvent event = reader.nextEvent(); final int type = event.getEventType(); if ((type != XMLStreamConstants.START_DOCUMENT) && (type != XMLStreamConstants.END_DOCUMENT)) { writer.add(event); } } } else { writer.add(reader); } writer.flush(); }
From source file:Main.java
public static String parse(XMLStreamReader reader) { StringBuffer result = new StringBuffer(); if (reader != null) { try {//from w w w .ja va 2 s .c om while (reader.hasNext()) { switch (reader.getEventType()) { case XMLStreamConstants.START_ELEMENT: result.append("<"); printName(reader, result); printNamespaces(reader, result); printAttributes(reader, result); result.append(">"); break; case XMLStreamConstants.END_ELEMENT: result.append("</"); printName(reader, result); result.append(">"); break; case XMLStreamConstants.SPACE: case XMLStreamConstants.CHARACTERS: int start = reader.getTextStart(); int length = reader.getTextLength(); result.append(new String(reader.getTextCharacters(), start, length)); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: result.append("<?"); if (reader.hasText()) result.append(reader.getText()); result.append("?>"); break; case XMLStreamConstants.CDATA: result.append("<![CDATA["); start = reader.getTextStart(); length = reader.getTextLength(); result.append(new String(reader.getTextCharacters(), start, length)); result.append("]]>"); break; case XMLStreamConstants.COMMENT: result.append("<!--"); if (reader.hasText()) result.append(reader.getText()); result.append("-->"); break; case XMLStreamConstants.ENTITY_REFERENCE: result.append(reader.getLocalName()).append("="); if (reader.hasText()) result.append("[").append(reader.getText()).append("]"); break; case XMLStreamConstants.START_DOCUMENT: result.append("<?xml"); result.append(" version='").append(reader.getVersion()).append("'"); result.append(" encoding='").append(reader.getCharacterEncodingScheme()).append("'"); if (reader.isStandalone()) result.append(" standalone='yes'"); else result.append(" standalone='no'"); result.append("?>"); break; } reader.next(); } // end while } catch (XMLStreamException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (XMLStreamException e) { } } } return result.toString(); }
From source file:StAXEventTreeViewer.java
public void buildTree(DefaultTreeModel treeModel, DefaultMutableTreeNode current, File file) throws XMLStreamException, FileNotFoundException { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file)); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: StartDocument startDocument = (StartDocument) event; DefaultMutableTreeNode version = new DefaultMutableTreeNode(startDocument.getVersion()); current.add(version);//from www .j a v a2 s .com current.add(new DefaultMutableTreeNode(startDocument.isStandalone())); current.add(new DefaultMutableTreeNode(startDocument.standaloneSet())); current.add(new DefaultMutableTreeNode(startDocument.encodingSet())); current.add(new DefaultMutableTreeNode(startDocument.getCharacterEncodingScheme())); break; case XMLStreamConstants.START_ELEMENT: StartElement startElement = (StartElement) event; QName elementName = startElement.getName(); DefaultMutableTreeNode element = new DefaultMutableTreeNode(elementName.getLocalPart()); current.add(element); current = element; if (!elementName.getNamespaceURI().equals("")) { String prefix = elementName.getPrefix(); if (prefix.equals("")) { prefix = "[None]"; } DefaultMutableTreeNode namespace = new DefaultMutableTreeNode( "prefix=" + prefix + ",URI=" + elementName.getNamespaceURI()); current.add(namespace); } for (Iterator it = startElement.getAttributes(); it.hasNext();) { Attribute attr = (Attribute) it.next(); DefaultMutableTreeNode attribute = new DefaultMutableTreeNode("Attribute (name=" + attr.getName().getLocalPart() + ",value=" + attr.getValue() + "')"); String attURI = attr.getName().getNamespaceURI(); if (!attURI.equals("")) { String attPrefix = attr.getName().getPrefix(); if (attPrefix.equals("")) { attPrefix = "[None]"; } attribute.add(new DefaultMutableTreeNode("prefix = " + attPrefix + ", URI = " + attURI)); } current.add(attribute); } break; case XMLStreamConstants.END_ELEMENT: current = (DefaultMutableTreeNode) current.getParent(); break; case XMLStreamConstants.CHARACTERS: Characters characters = (Characters) event; if (!characters.isIgnorableWhiteSpace() && !characters.isWhiteSpace()) { String data = characters.getData(); if (data.length() != 0) { current.add(new DefaultMutableTreeNode(characters.getData())); } } break; case XMLStreamConstants.DTD: DTD dtde = (DTD) event; current.add(new DefaultMutableTreeNode(dtde.getDocumentTypeDeclaration())); default: System.out.println(event.getClass().getName()); } } }
From source file:com.widowcrawler.exo.parse.Parser.java
private static String getEventTypeString(int eventType) { switch (eventType) { case XMLStreamConstants.START_ELEMENT: return "START_ELEMENT"; case XMLStreamConstants.END_ELEMENT: return "END_ELEMENT"; case XMLStreamConstants.PROCESSING_INSTRUCTION: return "PROCESSING_INSTRUCTION"; case XMLStreamConstants.CHARACTERS: return "CHARACTERS"; case XMLStreamConstants.COMMENT: return "COMMENT"; case XMLStreamConstants.START_DOCUMENT: return "START_DOCUMENT"; case XMLStreamConstants.END_DOCUMENT: return "END_DOCUMENT"; case XMLStreamConstants.ENTITY_REFERENCE: return "ENTITY_REFERENCE"; case XMLStreamConstants.ATTRIBUTE: return "ATTRIBUTE"; case XMLStreamConstants.DTD: return "DTD"; case XMLStreamConstants.CDATA: return "CDATA"; case XMLStreamConstants.SPACE: return "SPACE"; case XMLStreamConstants.ENTITY_DECLARATION: return "ENTITY_DECLARATION"; case XMLStreamConstants.NAMESPACE: return "NAMESPACE"; case XMLStreamConstants.NOTATION_DECLARATION: return "NOTATION_DECLARATION"; }//from www . j a va2 s. c o m return "UNKNOWN_EVENT_TYPE: " + eventType; }