Example usage for javax.xml.transform.dom DOMSource DOMSource

List of usage examples for javax.xml.transform.dom DOMSource DOMSource

Introduction

In this page you can find the example usage for javax.xml.transform.dom DOMSource DOMSource.

Prototype

public DOMSource(Node n) 

Source Link

Document

Create a new input source with a DOM node.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Document doc = null;//from  w w w . j  ava 2  s . c o  m
    String filename = "name.xml";

    Source source = new DOMSource(doc);

    File file = new File(filename);
    Result result = new StreamResult(file);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Document doc = null;/*from   w w w  .j  a v  a  2 s.co m*/
    XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(doc), new StAXResult(writer));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from   w  w w.ja  v  a  2  s. c o  m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Source source = new DOMSource(doc);

    URI uri = new File("infilename.xml").toURI();
    source.setSystemId(uri.toString());

    DefaultHandler handler = new MyHandler();
    SAXResult result = new SAXResult(handler);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
}

From source file:JAXPTransformNode.java

public static void main(String args[]) throws Exception, TransformerException, FileNotFoundException {

    TransformerFactory factory = TransformerFactory.newInstance();

    DOMSource stylesheet = new DOMSource(buildDoc(args[1]));
    StreamSource xmlDoc = new StreamSource(args[0]);
    StreamResult result = new StreamResult(new FileOutputStream(args[2]));

    Transformer transFormer = factory.newTransformer(stylesheet);

    transFormer.transform(xmlDoc, result);

}

From source file:PrintDOM.java

public static void main(String[] args) throws Exception {
    DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = parser.parse(new InputSource("zooinventory.xml"));
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource(document);
    Result output = new StreamResult(System.out);
    transformer.transform(source, output);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from   www  . j a v a  2 s.  c o m*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Transformer xformer = TransformerFactory.newInstance().newTransformer();

    xformer.setOutputProperty(OutputKeys.METHOD, "text");

    Source source = new DOMSource(doc);
    Result result = new StreamResult(new File("outfilename.xml"));
    xformer.transform(source, result);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*ww w .j a  v a2s. c  om*/
    factory.setXIncludeAware(true);
    DocumentBuilder parser = factory.newDocumentBuilder();
    System.out.println("aware: " + parser.isXIncludeAware());
    Document document = parser.parse(args[0]);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource(document);
    Result output = new StreamResult(System.out);
    transformer.transform(source, output);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<company><year id='2000'><quarter id='1' sales='80'/></year><year id='2001'><quarter id='1' sales='20'/></year></company>";
    String xpath = "/company/year[@id=2001]";
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node node = (Node) xPath.evaluate(xpath, new InputSource(new StringReader(xml)), XPathConstants.NODE);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(node), new StreamResult(System.out));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
    Document document = parser.parse(new File("NewFile.xml"));

    Schema schema = schemaFactory.newSchema(new File("NewFile.xsd"));
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(document));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new File("test1.xml"));

    removeEmptyChildElements(doc.getDocumentElement());

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("clean.xml"));
    transformer.transform(source, result);
}