Search in sources :

Example 51 with DOMSource

use of javax.xml.transform.dom.DOMSource in project spring-framework by spring-projects.

the class AbstractUnmarshallerTests method unmarshalDomSource.

@Test
public void unmarshalDomSource() throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.newDocument();
    Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
    document.appendChild(flightsElement);
    Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight");
    flightsElement.appendChild(flightElement);
    Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
    flightElement.appendChild(numberElement);
    Text text = document.createTextNode("42");
    numberElement.appendChild(text);
    DOMSource source = new DOMSource(document);
    Object flights = unmarshaller.unmarshal(source);
    testFlights(flights);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 52 with DOMSource

use of javax.xml.transform.dom.DOMSource in project spring-framework by spring-projects.

the class XStreamUnmarshallerTests method unmarshalDomSource.

@Test
public void unmarshalDomSource() throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(INPUT_STRING)));
    DOMSource source = new DOMSource(document);
    Object flight = unmarshaller.unmarshal(source);
    testFlight(flight);
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 53 with DOMSource

use of javax.xml.transform.dom.DOMSource in project dbeaver by serge-rider.

the class DBDDocumentXML method serializeDocument.

@Override
public void serializeDocument(@NotNull DBRProgressMonitor monitor, @NotNull OutputStream stream, String encoding) throws DBException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        Result output = new StreamResult(new OutputStreamWriter(stream, encoding));
        transformer.transform(new DOMSource(document), output);
    } catch (Exception e) {
        throw new DBException("Error serializing XML document", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DBException(org.jkiss.dbeaver.DBException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DOMResult(javax.xml.transform.dom.DOMResult)

Example 54 with DOMSource

use of javax.xml.transform.dom.DOMSource in project nokogiri by sparklemotion.

the class XmlRelaxng method getSchema.

private Schema getSchema(Source source, ThreadContext context) {
    InputStream is;
    VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
    if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        is = ss.getInputStream();
    } else {
        //if (this.source instanceof DOMSource)
        DOMSource ds = (DOMSource) source;
        StringWriter xmlAsWriter = new StringWriter();
        StreamResult result = new StreamResult(xmlAsWriter);
        try {
            TransformerFactory.newInstance().newTransformer().transform(ds, result);
        } catch (TransformerConfigurationException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        } catch (TransformerException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
        try {
            is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
    }
    try {
        return factory.compileSchema(is);
    } catch (VerifierConfigurationException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (SAXException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
}
Also used : VerifierFactory(org.iso_relax.verifier.VerifierFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Example 55 with DOMSource

use of javax.xml.transform.dom.DOMSource in project nokogiri by sparklemotion.

the class XmlSchema method validate.

protected void validate(Document document) throws SAXException, IOException {
    DOMSource docSource = new DOMSource(document);
    validator.validate(docSource);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource)

Aggregations

DOMSource (javax.xml.transform.dom.DOMSource)392 StreamResult (javax.xml.transform.stream.StreamResult)231 Transformer (javax.xml.transform.Transformer)204 Document (org.w3c.dom.Document)161 TransformerFactory (javax.xml.transform.TransformerFactory)112 TransformerException (javax.xml.transform.TransformerException)107 DocumentBuilder (javax.xml.parsers.DocumentBuilder)102 StringWriter (java.io.StringWriter)97 IOException (java.io.IOException)93 Element (org.w3c.dom.Element)81 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)77 Source (javax.xml.transform.Source)67 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)62 SAXException (org.xml.sax.SAXException)56 File (java.io.File)55 InputSource (org.xml.sax.InputSource)50 StreamSource (javax.xml.transform.stream.StreamSource)47 Node (org.w3c.dom.Node)45 InputStream (java.io.InputStream)35 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35