Example usage for javax.xml.parsers ParserConfigurationException printStackTrace

List of usage examples for javax.xml.parsers ParserConfigurationException printStackTrace

Introduction

In this page you can find the example usage for javax.xml.parsers ParserConfigurationException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:TryDOM.java

public static void main(String args[]) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {//w w  w . j  a  va 2s  .  co  m
        builder = builderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        System.exit(1);
    }
    System.out.println("Builder Factory = " + builderFactory + "\nBuilder = " + builder);
}

From source file:Main.java

public static void main(String args[]) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true); // Set namespace aware
    builderFactory.setValidating(true); // and validating parser feaures
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = null;
    try {/* www .  ja v a  2  s  .c  o  m*/
        builder = builderFactory.newDocumentBuilder(); // Create the parser
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document xmlDoc = null;

    try {
        xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    } catch (SAXException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }
    DocumentType doctype = xmlDoc.getDoctype();
    if (doctype == null) {
        System.out.println("DOCTYPE is null");
    } else {
        System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
    }

    System.out.println("\nDocument body contents are:");
    listNodes(xmlDoc.getDocumentElement(), ""); // Root element & children
}

From source file:MainClass.java

public static void main(String args[]) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setValidating(true); // and validating parser feaures
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = null;
    try {//from w  w  w .j ava 2 s .co  m
        builder = builderFactory.newDocumentBuilder(); // Create the parser
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document xmlDoc = null;

    try {
        xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    } catch (SAXException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }
    DocumentType doctype = xmlDoc.getDoctype();
    if (doctype == null) {
        System.out.println("DOCTYPE is null");
    } else {
        System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
    }

    System.out.println("\nDocument body contents are:");
    listNodes(xmlDoc.getDocumentElement(), ""); // Root element & children
}

From source file:XMLInfo.java

public static void main(String args[]) {
    try {/*from   w w  w.ja  va  2s. co m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("xmlFileName.xml");
        Node root = document.getDocumentElement();
        System.out.print("Here is the document's root node:");
        System.out.println(" " + root.getNodeName());
        System.out.println("Here are its child elements: ");
        NodeList childNodes = root.getChildNodes();
        Node currentNode;

        for (int i = 0; i < childNodes.getLength(); i++) {
            currentNode = childNodes.item(i);
            System.out.println(currentNode.getNodeName());
        }

        // get first child of root element
        currentNode = root.getFirstChild();

        System.out.print("The first child of root node is: ");
        System.out.println(currentNode.getNodeName());

        // get next sibling of first child
        System.out.print("whose next sibling is: ");
        currentNode = currentNode.getNextSibling();
        System.out.println(currentNode.getNodeName());

        // print value of next sibling of first child
        System.out.println("value of " + currentNode.getNodeName() + " element is: "
                + currentNode.getFirstChild().getNodeValue());

        // print name of parent of next sibling of first child
        System.out.print("Parent node of " + currentNode.getNodeName() + " is: "
                + currentNode.getParentNode().getNodeName());
    }
    // handle exception creating DocumentBuilder
    catch (ParserConfigurationException parserError) {
        System.err.println("Parser Configuration Error");
        parserError.printStackTrace();
    }

    // handle exception reading data from file
    catch (IOException fileException) {
        System.err.println("File IO Error");
        fileException.printStackTrace();
    }

    // handle exception parsing XML document
    catch (SAXException parseException) {
        System.err.println("Error Parsing Document");
        parseException.printStackTrace();
    }
}

From source file:OldExtractor.java

/**
 * @param args the command line arguments
 *///from  ww w.j a  va 2s . com
public static void main(String[] args) {
    // TODO code application logic here
    String bingUrl = "https://api.datamarket.azure.com/Bing/Search/Web?$top=10&$format=Atom&Query=%27gates%27";
    //Provide your account key here.
    String accountKey = "ghTYY7wD6LpyxUO9VRR7e1f98WFhHWYERMcw87aQTqQ";
    //  String accountKey = "xqbCjT87/MQz25JWdRzgMHdPkGYnOz77IYmP5FUIgC8";

    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
    String accountKeyEnc = new String(accountKeyBytes);

    try {
        URL url = new URL(bingUrl);
        URLConnection urlConnection = url.openConnection();

        urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc);
        InputStream inputStream = (InputStream) urlConnection.getContent();
        byte[] contentRaw = new byte[urlConnection.getContentLength()];
        inputStream.read(contentRaw);
        String content = new String(contentRaw);
        //System.out.println(content);
        try {
            File file = new File("Results.xml");
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write(content);
            //fileWriter.write("a test");
            fileWriter.flush();
            fileWriter.close();

        } catch (IOException e) {
            System.out.println(e);
        }

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {
            //System.out.println("here");
            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file
            Document dom = db.parse("Results.xml");
            Element docEle = (Element) dom.getDocumentElement();

            //get a nodelist of elements

            NodeList nl = docEle.getElementsByTagName("d:Url");
            if (nl != null && nl.getLength() > 0) {
                for (int i = 0; i < nl.getLength(); i++) {

                    //get the employee element
                    Element el = (Element) nl.item(i);
                    // System.out.println("here");
                    System.out.println(el.getTextContent());

                    //get the Employee object
                    //Employee e = getEmployee(el);

                    //add it to list
                    //myEmpls.add(e);
                }
            }
            NodeList n2 = docEle.getElementsByTagName("d:Title");
            if (n2 != null && n2.getLength() > 0) {
                for (int i = 0; i < n2.getLength(); i++) {

                    //get the employee element
                    Element e2 = (Element) n2.item(i);
                    // System.out.println("here");
                    System.out.println(e2.getTextContent());

                    //get the Employee object
                    //Employee e = getEmployee(el);

                    //add it to list
                    //myEmpls.add(e);
                }
            }

            NodeList n3 = docEle.getElementsByTagName("d:Description");
            if (n3 != null && n3.getLength() > 0) {
                for (int i = 0; i < n3.getLength(); i++) {

                    //get the employee element
                    Element e3 = (Element) n3.item(i);
                    // System.out.println("here");
                    System.out.println(e3.getTextContent());

                    //get the Employee object
                    //Employee e = getEmployee(el);

                    //add it to list
                    //myEmpls.add(e);
                }
            }

        } catch (SAXException se) {
            se.printStackTrace();
        } catch (ParserConfigurationException pe) {
            pe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    } catch (IOException e) {
        System.out.println(e);
    }

    //The content string is the xml/json output from Bing.

}

From source file:it.wami.map.mongodeploy.OsmToMongoDB.java

/**
 * @param args/*from  w ww  . ja  va  2 s.  c o m*/
 */
public static void main(String[] args) {

    if (args == null || args.length == 0 || args.length < 2) {
        System.out.println(Options.USAGE);
        return;
    }

    parseCommandOptions(args);

    MongoClientOptions mco = new MongoClientOptions.Builder().connectionsPerHost(150000)
            .threadsAllowedToBlockForConnectionMultiplier(10000).build();

    MongoClient mongoClient = createMongo(options.getHost(), options.getPort(), mco);

    DB db = mongoClient.getDB(options.getDbName());
    DBCollection nodes = db.createCollection(OsmSaxHandler.COLL_NODES, null);
    createNodesIndex(nodes);
    /*DBCollection ways = */
    DBCollection ways = db.createCollection(OsmSaxHandler.COLL_WAYS, null);
    createWaysIndex(ways);
    /*DBCollection relations = */
    DBCollection relations = db.createCollection(OsmSaxHandler.COLL_RELATIONS, null);
    createRelationsIndex(relations);
    db.createCollection(OsmSaxHandler.COLL_TAGS, null);

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = null;
    try {
        saxParser = factory.newSAXParser();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }

    DefaultHandler handler = new OsmSaxHandler(db, options);

    try {
        if (options.getInput().contains(".bz2")) {
            try {
                saxParser.parse(getInputStreamForBZ2File(options.getInput()), handler);
            } catch (CompressorException e) {
                e.printStackTrace();
            }
        } else if (options.getInput().contains(".osm")) {
            saxParser.parse(options.getInput(), handler);
        } else {
            throw new IllegalArgumentException("input must be an osm file or a bz2.");
        }
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static Document create() {
    try {/*from   w  w  w  .  j a v a2s.c  om*/
        return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static synchronized DocumentBuilder db() {
    if (null != db)
        return db;
    try {//from   w w  w.j  ava2 s  .co  m
        db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return db;
}

From source file:Main.java

public static Document createDocument() {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    try {// w w w .ja  va 2  s  .  c o  m
        docBuilder = dbfac.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document doc = docBuilder.newDocument();
    return doc;
}

From source file:Main.java

private static void builder() {
    try {//from  ww w  . j  a va  2 s . c o  m
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e1) {
        e1.printStackTrace();
    }
}