Example usage for java.io File File

List of usage examples for java.io File File

Introduction

In this page you can find the example usage for java.io File File.

Prototype

public File(URI uri) 

Source Link

Document

Creates a new File instance by converting the given file: URI into an abstract pathname.

Usage

From source file:Main.java

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

    factory.setExpandEntityReferences(false);

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

    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction("target", "instruction");

    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        element = (Element) list.item(i);
        pi = doc.createProcessingInstruction("target", "instruction=" + i);
        element.appendChild(pi);
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    builder.setEntityResolver(new MyResolver());
    Document doc = builder.parse(new File("infilename.xml"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new File("Sample.xml"));
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression exp = xPath.compile(
            "/article/body/section/region[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'perfect')]");
    NodeList nl = (NodeList) exp.evaluate(doc.getFirstChild(), XPathConstants.NODESET);
    for (int index = 0; index < nl.getLength(); index++) {
        Node node = nl.item(index);
        System.out.println(node.getTextContent());
    }/*from  w w  w.ja  v  a 2  s. c  om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Foo.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    File xml = new File("input.xml");
    Foo foo = (Foo) unmarshaller.unmarshal(xml);

    System.out.println(foo.getBar());

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(foo, System.out);
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    File documentFile = new File(args[0]);
    File schemaFile = new File(args[1]);

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = null;/*from  ww  w . j  av a2  s. c  o  m*/
    try {
        schema = factory.newSchema(schemaFile);
    } catch (SAXException e) {
        fail(e);
    }

    Validator validator = schema.newValidator();

    SAXSource source = new SAXSource(new InputSource(new FileReader(documentFile)));

    try {
        validator.validate(source);
    } catch (SAXException e) {
        fail(e);
    }
}

From source file:EventFilterExample.java

public static void main(String[] args) throws Exception {
    File file = new File("text.xml");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file));

    System.out.println("Unfiltered Count = " + countEvents(reader));

    reader = inputFactory.createXMLEventReader(new FileInputStream(file));

    EventFilter filter = new ElementOnlyFilter();
    reader = inputFactory.createFilteredReader(reader, filter);

    System.out.println("Filtered Count = " + countEvents(reader));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Root.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    File xml = new File("input.xml");
    Root root = (Root) unmarshaller.unmarshal(xml);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(root, System.out);
}

From source file:Main.java

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

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

    String fragment = "<fragment>aaa</fragment>";

    factory = DocumentBuilderFactory.newInstance();
    Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment)));

    Node node = doc.importNode(d.getDocumentElement(), true);

    DocumentFragment docfrag = doc.createDocumentFragment();

    while (node.hasChildNodes()) {
        docfrag.appendChild(node.removeChild(node.getFirstChild()));
    }

    Element element = doc.getDocumentElement();
    element.appendChild(docfrag);
}

From source file:Transform2.java

public static void main(String[] args) throws Exception {
    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(new File("r.xml"));
    Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("program.xsl"));
    JDOMResult out = new JDOMResult();
    transformer.transform(new JDOMSource(document), out);
    XMLOutputter xmlOutputter = new XMLOutputter();
    xmlOutputter.output(out.getDocument(), new FileWriter("JDOMAlteredRichard.html"));
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);//  www. j ava  2s  . c o  m
    fc.setCurrentDirectory(new File("C:\\tmp"));
    JButton btn1 = new JButton("Show Dialog");
    btn1.addActionListener(e -> fc.showDialog(frame, "Choose"));
    JButton btn2 = new JButton("Show Open Dialog");
    btn2.addActionListener(e -> {
        int retVal = fc.showOpenDialog(frame);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            File[] selectedfiles = fc.getSelectedFiles();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < selectedfiles.length; i++) {
                sb.append(selectedfiles[i].getName());
                sb.append("\n");
            }
            JOptionPane.showMessageDialog(frame, sb.toString());
        }
    });
    JButton btn3 = new JButton("Show Save Dialog");
    btn3.addActionListener(e -> fc.showSaveDialog(frame));
    Container pane = frame.getContentPane();
    pane.setLayout(new GridLayout(3, 1, 10, 10));
    pane.add(btn1);
    pane.add(btn2);
    pane.add(btn3);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}