List of usage examples for javax.xml.stream XMLInputFactory newInstance
public static XMLInputFactory newInstance() throws FactoryConfigurationError
From source file:Main.java
/** * //from ww w . j a va2s. c o m * @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
/** * /*from w w w .j ava 2 s. co m*/ * @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
private static XMLEventReader getXMLEventReader(String filename) throws Exception { XMLInputFactory xmlif = null; XMLEventReader xmlr = null;//from w w w . j a va2s .c om xmlif = XMLInputFactory.newInstance(); xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE); xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); FileInputStream fis = new FileInputStream(filename); xmlr = xmlif.createXMLEventReader(filename, fis); return xmlr; }
From source file:eu.arthepsy.sonar.plugins.scapegoat.util.XmlUtils.java
public static SMInputFactory createFactory() { XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); SMInputFactory inputFactory = new SMInputFactory(xmlFactory); return inputFactory; }
From source file:edu.harvard.i2b2.previousquery.QueryListNamesClient.java
public static OMElement getQueryPayLoad(String XMLstr) throws Exception { StringReader strReader = new StringReader(XMLstr); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader reader = xif.createXMLStreamReader(strReader); StAXOMBuilder builder = new StAXOMBuilder(reader); OMElement lineItem = builder.getDocumentElement(); //System.out.println("Line item string " + lineItem.toString()); return lineItem; }
From source file:Main.java
/** * /* w ww . j a v a2 s .c om*/ * @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
/** * Creates an XMLInputFactory with unsafe features disabled. Such an XMLInputFactory is to be used for parsing * untrusted xml such as incoming post requests. * //from ww w. j a v a 2 s . c o m * @return */ public static XMLInputFactory newSafeInstance() { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false); return inputFactory; }
From source file:de.bloxel.engine.util.JAXBUtils.java
public static <T> T unmarschal(final InputStream inputStream, final Class<T> aClass) { try {//ww w.j a v a 2 s .c om // http://jaxb.java.net/faq/index.html#classloader final JAXBContext jc = JAXBContext.newInstance(getPackageCanonicalName(aClass)); final Unmarshaller unmarshaller = jc.createUnmarshaller(); // http://jaxb.java.net/guide/Unmarshalling_is_not_working__Help_.html unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler()); final XMLInputFactory staxFactory = XMLInputFactory.newInstance(); final XMLStreamReader xmlReader = staxFactory.createXMLStreamReader(inputStream); return unmarshaller.unmarshal(xmlReader, aClass).getValue(); } catch (final JAXBException e) { throw new RuntimeException(String.format("Can't load unmarschal '%s'", aClass), e); } catch (final XMLStreamException e) { throw new RuntimeException(String.format("Can't load unmarschal '%s'", aClass), e); } }
From source file:edu.harvard.i2b2.timeline.lifelines.QueryClient.java
public static OMElement getQueryPayLoad(String queryXML) throws XMLStreamException { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://mgh.harvard.edu/i2b2", ""); OMElement method = fac.createOMElement("queryPatient", omNs); StringReader strReader = new StringReader(queryXML); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader reader = xif.createXMLStreamReader(strReader); StAXOMBuilder builder = new StAXOMBuilder(reader); OMElement lineItem = builder.getDocumentElement(); method.addChild(lineItem);/*from w w w.jav a 2 s .c om*/ return method; }
From source file:MDRevealer.ResistanceTests.java
private static ArrayList<HashMap<String, String>> parse_test(File rt_input) throws XMLStreamException, FileNotFoundException { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(rt_input)); ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>(); HashMap<String, String> hm = new HashMap<String, String>(); String key = "null"; while (xsr.hasNext() == true) { int constant = xsr.next(); switch (constant) { case XMLStreamConstants.START_ELEMENT: key = xsr.getLocalName();/* w w w . java 2s .co m*/ if (xsr.getAttributeCount() > 0) { for (int i = 0; i < xsr.getAttributeCount(); i++) { hm.put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } test.add(hm); hm = new HashMap<String, String>(); } break; case XMLStreamConstants.CHARACTERS: if (!xsr.isWhiteSpace()) { hm.put(key, xsr.getText()); test.add(hm); } hm = new HashMap<String, String>(); break; case XMLStreamConstants.END_ELEMENT: if (xsr.getLocalName().equals("condition")) { hm.put("condition_over", "true"); test.add(hm); hm = new HashMap<String, String>(); } break; } } return test; }