Example usage for org.w3c.dom Document getDocumentElement

List of usage examples for org.w3c.dom Document getDocumentElement

Introduction

In this page you can find the example usage for org.w3c.dom Document getDocumentElement.

Prototype

public Element getDocumentElement();

Source Link

Document

This is a convenience attribute that allows direct access to the child node that is the document element of the document.

Usage

From source file:Main.java

public static Element loadDocument(File location) {
    Document doc = null;
    try {/*from  ww  w. java2 s  .co m*/
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
        doc = parser.parse(location);
        Element root = doc.getDocumentElement();
        root.normalize();

        return root;
    } catch (SAXParseException err) {
        System.err.println("URLMappingsXmlDAO ** Parsing error, line " + err.getLineNumber() + ", uri "
                + err.getSystemId());
        System.err.println("URLMappingsXmlDAO error: " + err.getMessage());
    } catch (SAXException e) {
        System.err.println("URLMappingsXmlDAO error: " + e);
    } catch (MalformedURLException mfx) {
        System.err.println("URLMappingsXmlDAO error: " + mfx);
    } catch (IOException e) {
        System.err.println("URLMappingsXmlDAO error: " + e);
    } catch (Exception pce) {
        System.err.println("URLMappingsXmlDAO error: " + pce);
    }
    return null;
}

From source file:gov.nih.nci.cagrid.opensaml.SAMLObject.java

/**
 *  Allows parsing of objects from a stream of XML
 *
 * @param  in                   A stream containing XML
 * @return                      The root of the XML document instance
 * @exception  SAMLException  Raised if an exception occurs while constructing
 *                              the object
 *//* www . jav  a  2 s.co  m*/
static protected Element fromStream(InputStream in) throws SAMLException {
    try {
        Document doc = XML.parserPool.parse(in);
        return doc.getDocumentElement();
    } catch (Exception e) {
        NDC.push("fromStream");
        Logger.getLogger(SAMLObject.class.getName())
                .error("caught an exception while parsing a stream:\n" + e.getMessage());
        NDC.pop();
        throw new MalformedException("SAMLObject.fromStream() caught exception while parsing a stream", e);
    }
}

From source file:de.itomig.itoplib.GetItopData.java

private static ArrayList<Organization> parseOrganizationDoc(Document doc) {
    // get the root elememt
    Element root = doc.getDocumentElement();
    NodeList items = root.getElementsByTagName("Organization");

    organizations.clear();/*w  w  w  . j  a v  a 2 s . c o  m*/
    for (int i = 0; i < items.getLength(); i++) {
        Node item = items.item(i);
        String org_id = ((Element) items.item(i)).getAttribute("id");
        NodeList properties = item.getChildNodes();
        String org_name = "";
        for (int j = 0; j < properties.getLength(); j++) {
            Node property = properties.item(j);
            String name = property.getNodeName();
            if (name.equalsIgnoreCase("name")) {
                org_name = getConcatNodeValues(property);
            }
        }
        // there should ALLWAYS be an id.

        int id;
        try {
            if (org_name != "") {
                id = Integer.parseInt(org_id);
                organizations.add(new Organization(id, org_name));
            } else {
                Log.e(TAG, "no name for org with id=" + org_id);
            }
        } catch (NumberFormatException e) {
            Log.e("TAG", "id of person not parseable");
        }
    }
    return organizations;
}

From source file:Main.java

/**
 * Convert XML string to a XML DOM document
 *
 * @param strXML/*www.  j ava 2 s. c o m*/
 *            XML
 * @return XML DOM document
 * @throws Exception
 *             in error case
 */
public static Document xmlStringToDOMDocument(String strXML) throws Exception {
    if (strXML == null) {
        throw new RuntimeException("No XML input given(null)!");
    }

    StringReader reader = null;
    Document doc = null;
    try {
        reader = new StringReader(strXML);
        InputSource inputSource = new InputSource(reader);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(inputSource);
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Parsing of XML input failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }

    return doc;
}

From source file:Main.java

public static Element loadDocument(File location) {
    Document doc = null;
    try {/*from   w  w w.  j  av  a 2 s.  c  om*/

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
        doc = parser.parse(location);
        Element root = doc.getDocumentElement();
        root.normalize();
        /*
         * //Output to standard output ; use Sun's reference imple for now
         * XmlDocument xdoc = (XmlDocument) doc; xdoc.write(new
         * OutputStreamWriter(System.out));
         */
        return root;
    } catch (SAXParseException err) {
        System.err.println("URLMappingsXmlDAO ** Parsing error" + ", line " + err.getLineNumber() + ", uri "
                + err.getSystemId());
        System.err.println("URLMappingsXmlDAO error: " + err.getMessage());
    } catch (SAXException e) {
        System.err.println("URLMappingsXmlDAO error: " + e);
    } catch (java.net.MalformedURLException mfx) {
        System.err.println("URLMappingsXmlDAO error: " + mfx);
    } catch (java.io.IOException e) {
        System.err.println("URLMappingsXmlDAO error: " + e);
    } catch (Exception pce) {
        System.err.println("URLMappingsXmlDAO error: " + pce);
    }
    return null;
}

From source file:fr.ece.epp.tools.Utils.java

public static void updateProduct(String path, String[] feature, boolean outOrno) {
    Document document = null;
    try {/* w w  w  . j  a v a  2 s . c  om*/
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
        Element root = document.getDocumentElement();
        Node features = root.getElementsByTagName("features").item(0);
        Element basefeature = document.createElement("feature");
        basefeature.setAttribute("id", "org.eclipse.platform");
        features.appendChild(basefeature);
        for (int i = 0; i < feature.length; i++) {
            if (feature[i] != null && feature[i].trim().length() > 0) {
                Element fea = document.createElement("feature");
                if (feature[i].endsWith(".feature.group")) {
                    int count = feature[i].length() - ".featyre.group".length();
                    String id = feature[i].substring(0, count);
                    fea.setAttribute("id", id);
                } else {
                    fea.setAttribute("id", feature[i]);
                }
                features.appendChild(fea);
            }
        }
        output(root, path);
        if (outOrno) {
            output(root, null);
        }

    } catch (SAXException e) {
    } catch (IOException e) {
    } catch (ParserConfigurationException e) {
    }
}

From source file:fr.ece.epp.tools.Utils.java

public static void updatePom(String path, String[] repo, boolean outOrno) {
    Document document = null;
    try {/*from   www  .  j  a v a  2 s  .c  om*/
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path);
        Element root = document.getDocumentElement();
        Node repositories = root.getElementsByTagName("repositories").item(0);
        for (int i = 0; i < repo.length; i++) {
            Element repository = document.createElement("repository");

            Element id = document.createElement("id");
            id.appendChild(document.createTextNode("repository" + i));
            repository.appendChild(id);

            Element layout = document.createElement("layout");
            layout.appendChild(document.createTextNode("p2"));
            repository.appendChild(layout);

            Element url = document.createElement("url");
            url.appendChild(document.createTextNode(repo[i]));

            repository.appendChild(url);
            repositories.appendChild(repository);
        }
        output(root, path);
        if (outOrno) {
            output(root, null);
        }

    } catch (SAXException e) {
    } catch (IOException e) {
    } catch (ParserConfigurationException e) {
    }
}

From source file:Main.java

public static String transformXmlToString(Document doc, String encoding)
        throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();

    Document document = builder.newDocument();

    Element e = doc.getDocumentElement();
    Node n = document.importNode(e, true);
    document.appendChild(n);//  www.j  a v  a2  s.  c o  m

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(document);

    StringWriter sw = new StringWriter();

    StreamResult result = new StreamResult(sw);

    transformer.transform(source, result);

    return sw.toString();
}

From source file:de.itomig.itoplib.GetItopData.java

private static ArrayList<Person> parsePersonDoc(Document doc) {
    // get the root elememt
    Element root = doc.getDocumentElement();
    NodeList items = root.getElementsByTagName("Person");

    persons.clear();//from w  w  w . j  a  v a  2s .  c  om
    for (int i = 0; i < items.getLength(); i++) {
        Node item = items.item(i);
        String person_id = ((Element) items.item(i)).getAttribute("id");
        NodeList properties = item.getChildNodes();

        String person_first = "";
        String person_name = "";
        String phone = "";
        String org = "0";
        for (int j = 0; j < properties.getLength(); j++) {
            Node property = properties.item(j);
            String name = property.getNodeName();
            if (name.equalsIgnoreCase("name")) {
                person_name = getConcatNodeValues(property);
            } else if (name.equalsIgnoreCase("first_name")) {
                person_first = getConcatNodeValues(property);
            } else if (name.equalsIgnoreCase("phone")) {
                phone = getConcatNodeValues(property);
            } else if (name.equalsIgnoreCase("org_id")) {
                org = getConcatNodeValues(property);
            }
        }
        // there should ALLWAYS be an id.

        int id, org_id;
        try {
            id = Integer.parseInt(person_id);
            org_id = Integer.parseInt(org);
            persons.add(new Person(id, person_first + " " + person_name, phone, org_id));
        } catch (NumberFormatException e) {
            // should never happen
            Log.e("TAG", "id of person not parseable");
        }
    }
    return persons;
}

From source file:com.textocat.textokit.corpus.statistics.dao.corpus.XmiFileTreeCorpusDAO.java

static private String getXMLRootElement(File xmlFile)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);

    // optional, but recommended
    // read this -
    // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    doc.getDocumentElement().normalize();

    return doc.getDocumentElement().getNodeName();
}