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

/**
 * Add the service references to the document.
 *
 * @param document//  w ww .  j av  a 2s . c o  m
 */
// FIXME: This provides trivial information. Necessary?
public static void addServices(Document document) {

    if (document != null) {

        Element root = document.getDocumentElement();

        if (root != null) {

            Element element = document.createElement("services");

            Element service = document.createElement("service");
            service.setAttribute("name", "run");
            service.setAttribute("url", "run");
            element.appendChild(service);

            service = document.createElement("service");
            service.setAttribute("name", "module");
            service.setAttribute("url", "module");
            element.appendChild(service);

            root.appendChild(element);
        }
    }
}

From source file:org.shareok.data.documentProcessor.FileHandlerFactory.java

/**
 *
 * @param fileExtension/*w w w . ja  va 2 s .  c o m*/
 * @return
 */
public static FileHandler getFileHandlerByFileExtension(String fileExtension) {

    FileHandler fh = null;
    String beanName = "";

    try {
        String fileTypePath = DocumentProcessorUtil.getFilePathFromResources("filetypes.xml");
        Document fileTypeDoc = loadXMLFromString(fileTypePath);
        fileTypeDoc.getDocumentElement().normalize();
        Element docEle = fileTypeDoc.getDocumentElement();
        NodeList nl = docEle.getChildNodes();

        if (nl != null && nl.getLength() > 0) {
            for (int i = 0; i < nl.getLength(); i++) {
                if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                    Element el = (Element) nl.item(i);
                    String nodeVal = el.getTextContent();
                    if (nodeVal.equals(fileExtension)) {
                        beanName = el.getAttribute("bean");
                        break;
                    }
                }
            }
        }

        ApplicationContext context = new ClassPathXmlApplicationContext("documentProcessorContext.xml");
        fh = (FileHandler) context.getBean(beanName);

    } catch (Exception ex) {
        Logger.getLogger(DocumentProcessorUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    return fh;
}

From source file:Main.java

public static Node appendXMLChildFromString(Node parent, String child)
        throws ParserConfigurationException, SAXException, IOException {
    if (parent instanceof Document)
        parent = ((Document) parent).getDocumentElement();
    Document newDoc = getXMLFromString(child);
    Node newNode = parent.getOwnerDocument().importNode(newDoc.getDocumentElement(), true);
    return parent.appendChild(newNode);
}

From source file:Main.java

public static String toXML(Document document, boolean format) throws Exception {

    if (format) {
        removeWhitespaceNodes(document.getDocumentElement());
    }//from w w w .  j a  v a 2 s  .  co  m
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 2);
    Transformer transformer = transformerFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Writer out = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(out));
    return out.toString();
}

From source file:DOMEdit.java

public static void addCDATA(Document doc) {
        Element root = doc.getDocumentElement();
        Element place = (Element) root.getFirstChild();
        Element directions = (Element) place.getLastChild();
        String dirtext = "cdData.";
        CDATASection dirdata = doc.createCDATASection(dirtext);
        directions.replaceChild(dirdata, directions.getFirstChild());
    }// ww  w .  j  a va  2  s .  c om

From source file:Main.java

public static Node prependXMLChildFromString(Node parent, String child)
        throws ParserConfigurationException, SAXException, IOException {
    if (parent instanceof Document)
        parent = ((Document) parent).getDocumentElement();
    Document newDoc = getXMLFromString(child);
    Node newNode = parent.getOwnerDocument().importNode(newDoc.getDocumentElement(), true);
    return parent.insertBefore(newNode, parent.getFirstChild());
}

From source file:Main.java

public static Map<String, String> load(Reader reader) throws IOException {
    try {/*from   ww w . j  a  v a 2  s . co  m*/
        // Load XML into JDOM Document
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db;

        db = dbf.newDocumentBuilder();

        Document doc = db.parse(new InputSource(reader));
        doc.getDocumentElement().normalize();

        Map<String, String> result = new HashMap<String, String>();
        loadFromElements(result, doc.getDocumentElement().getChildNodes(),
                new StringBuffer(doc.getDocumentElement().getNodeName()));
        return result;
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }
}

From source file:DOMEdit.java

public static void addAttribute(Document doc) {
        Element root = doc.getDocumentElement();
        Element person = (Element) root.getFirstChild();
        person.setAttribute("company", "yourCom");
    }/*from   www.j a  v a  2 s. c  om*/

From source file:Main.java

/**
 * Convert and xml String to an org.w3c.dom.Element
 * @param xml - the XML/*from   w  ww. j  a  v a 2  s.  c  o  m*/
 * @return - the Element
 */
public static Element stringToElement(String xml) {
    Element element = null;
    try {
        Document document = getDocumentBuilder().parse(new InputSource(new StringReader(xml)));
        element = (Element) document.getDocumentElement().cloneNode(true);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return element;
}

From source file:DOMEdit.java

public static void delAttribute(Document doc) {
        Element root = doc.getDocumentElement();
        Element person = (Element) root.getFirstChild();
        person.removeAttribute("number");
        person.removeAttribute("dept");
    }/*from www . j  a  v  a  2s  .  c o m*/