/** * Create a JAXP 1.4 {@link StAXSource} for the given {@link XMLStreamReader}. * @param streamReader the StAX stream reader * @return a source wrapping the {@code streamReader} */ public static Source createStaxSource(XMLStreamReader streamReader) { return new StAXSource(streamReader); }
/** * Create a JAXP 1.4 {@link StAXSource} for the given {@link XMLEventReader}. * @param eventReader the StAX event reader * @return a source wrapping the {@code eventReader} */ public static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException { return new StAXSource(eventReader); }
/** * Create a JAXP 1.4 {@link StAXSource} for the given {@link XMLStreamReader}. * @param streamReader the StAX stream reader * @return a source wrapping the {@code streamReader} */ public static Source createStaxSource(XMLStreamReader streamReader) { return new StAXSource(streamReader); }
/** * Create a JAXP 1.4 {@link StAXSource} for the given {@link XMLEventReader}. * @param eventReader the StAX event reader * @return a source wrapping the {@code eventReader} */ public static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException { return new StAXSource(eventReader); }
public static Source getSource(XMLEventReader r) throws XMLStreamException { return new StAXSource(r); }
public void validateModel(XMLStreamReader xmlStreamReader) throws Exception { Schema schema = createSchema(); Validator validator = schema.newValidator(); validator.validate(new StAXSource(xmlStreamReader)); }
private Source readStAXSource(InputStream body, HttpInputMessage inputMessage) { try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, isSupportDtd()); inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities()); if (!isProcessExternalEntities()) { inputFactory.setXMLResolver(NO_OP_XML_RESOLVER); } XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body); return new StAXSource(streamReader); } catch (XMLStreamException ex) { throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage); } }
@Test public void readCorrect() throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); StAXSource source = new StAXSource(streamReader); StringWriter writer = new StringWriter(); transformer.transform(source, new StreamResult(writer)); Predicate<Node> nodeFilter = n -> n.getNodeType() != Node.DOCUMENT_TYPE_NODE && n.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE; assertThat(writer.toString(), isSimilarTo(XML).withNodeFilter(nodeFilter)); }
private Source readStAXSource(InputStream body, HttpInputMessage inputMessage) { try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, isSupportDtd()); inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities()); if (!isProcessExternalEntities()) { inputFactory.setXMLResolver(NO_OP_XML_RESOLVER); } XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body); return new StAXSource(streamReader); } catch (XMLStreamException ex) { throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage); } }
@Test public void unmarshalJaxp14StaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); StAXSource source = new StAXSource(streamReader); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void isStaxSourceJaxp14() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); String expected = "<element/>"; XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected)); StAXSource source = new StAXSource(streamReader); assertTrue("Not a StAX Source", StaxUtils.isStaxSource(source)); }
@Test public void unmarshalJaxp14StaxSourceXmlEventReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING)); StAXSource source = new StAXSource(eventReader); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
public synchronized <T extends Source> T getSource(Class<T> sourceClass) throws SQLException { checkFreed(); ensureInitialized(); if (_data == null) { return null; } try { if (sourceClass == null || DOMSource.class.equals(sourceClass)) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new NonPrintingErrorHandler()); InputSource input = new InputSource(new StringReader(_data)); return (T) new DOMSource(builder.parse(input)); } else if (SAXSource.class.equals(sourceClass)) { InputSource is = new InputSource(new StringReader(_data)); return (T) new SAXSource(is); } else if (StreamSource.class.equals(sourceClass)) { return (T) new StreamSource(new StringReader(_data)); } else if (StAXSource.class.equals(sourceClass)) { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(_data)); return (T) new StAXSource(xsr); } } catch (Exception e) { throw new PSQLException(GT.tr("Unable to decode xml data."), PSQLState.DATA_ERROR, e); } throw new PSQLException(GT.tr("Unknown XML Source class: {0}", sourceClass), PSQLState.INVALID_PARAMETER_TYPE); }
return new StAXSource(xsr);
private static Source createStaxSource(XMLStreamReader streamReader) { return new StAXSource(streamReader); }
private static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException { return new StAXSource(eventReader); }
import java.io.*; import javax.xml.stream.*; import javax.xml.transform.*; import javax.xml.transform.stax.StAXSource; import javax.xml.transform.dom.DOMResult; import org.w3c.dom.* public class Demo { public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); xsr.nextTag(); // Advance to statements element TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) { DOMResult result = new DOMResult(); t.transform(new StAXSource(xsr), result); Node domNode = result.getNode(); } } }
import java.io.File; import java.io.FileReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stax.StAXSource; import javax.xml.transform.stream.StreamResult; public class Demo { public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); xsr.nextTag(); // Advance to statements element TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) { File file = new File("out/" + xsr.getAttributeValue(null, "account") + ".xml"); t.transform(new StAXSource(xsr), new StreamResult(file)); } } }
public void validate(InputStream inputStream) throws Exception { XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(XmlDataImporter.findResource("schema/artemis-import-export.xsd")); Validator validator = schema.newValidator(); validator.validate(new StAXSource(reader)); reader.close(); }
private void processWork(XMLStreamReader reader, ParsedResponse response) { StAXSource source = new StAXSource(reader); try { Work work = (Work) unmarshaller.unmarshal(source); response.getWorks().add(work); } catch (XmlMappingException | IOException e) { LOGGER.warn("Error while parsing orcid document", e); } }