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:DOMCopy.java

private static void write(Document doc) throws IOException {
        outputHeading(doc);//www . j av a2  s  . co m
        outputElement(doc.getDocumentElement(), "");
    }

From source file:Main.java

public static Element loadDocument(InputStream in, String defElement) throws Exception {
    Document doc = null;
    try {/*from ww w .j ava 2  s  . c o  m*/
        doc = getDocumentBuilder().parse(in);
        return doc.getDocumentElement();
    } catch (Exception se) {
        if (defElement != null)
            return getDocumentBuilder().newDocument().createElement(defElement);
        throw new Exception(se.getMessage());
    }
}

From source file:Main.java

public static List<Map<String, String>> ReadFolderItemsFromFile(String path, String filename, String folderID) {

    List<Map<String, String>> results = new ArrayList<Map<String, String>>();

    try {/* w  ww. jav  a 2s .  c om*/
        File fXmlFile = new File(path, filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        NodeList nList = null;

        if (folderID == null) {
            // get all the children of the root
            nList = doc.getChildNodes().item(0).getChildNodes();
        } else {
            //Log.d("ReadItemsFromFile", "Current FolderID: " + folderID);

            NodeList folderList = doc.getElementsByTagName("folder");

            for (int i = 0; i < folderList.getLength(); i++) {

                Node curNode = folderList.item(i);
                if (curNode.getNodeType() == Node.ELEMENT_NODE) {
                    String curNodeId = ((Element) curNode).getElementsByTagName("id").item(0).getTextContent();
                    //Log.d("ReadItemsFromFile", "Number of items: " + curNodeId);

                    if (curNodeId.equals(folderID)) {
                        //Log.d("ReadItemsFromFile", "Found the folder");
                        NodeList folderChildren = curNode.getChildNodes();
                        for (int j = 0; j < folderChildren.getLength(); j++) {
                            //Log.d("ReadItemsFromFile", "node name: " + folderChildren.item(j).getNodeName());

                            if (folderChildren.item(j).getNodeName().equals("contents")) {
                                //Log.d("ReadItemsFromFile", "found the contents child");
                                nList = folderChildren.item(j).getChildNodes();
                                break;
                            }
                        }
                        break;
                    }
                }
            }
        }

        if (nList != null) {
            Log.d("ReadItemsFromFile", "-----------------------");
            Log.d("ReadItemsFromFile", "Number of items: " + nList.getLength());

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);
                //Log.d("ReadItemsFromFile", temp + ". node type: " + nNode.getNodeType());
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;

                    Node elementNameNode = eElement.getElementsByTagName("name").item(0);
                    String elementNameString = elementNameNode.getTextContent();
                    //Log.d("ReadItemsFromFile", "Name: " + elementNameString);
                    HashMap<String, String> mapElement = new HashMap<String, String>();
                    mapElement.put("name", elementNameString);
                    mapElement.put("id", eElement.getElementsByTagName("id").item(0).getTextContent());
                    mapElement.put("type", nNode.getNodeName());
                    results.add(mapElement);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return results;

}

From source file:Main.java

/**
 * Returns a namespaced root element of a document
 * Useful to create a namespace holder element
 * @param namespace/*from  w  ww .  j  a v  a  2s . co m*/
 * @return the root Element
 */
public static Element getRootElement(String elementName, String namespace, String prefix)
        throws TransformerException {
    Element rootNS = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();
        Document namespaceHolder = impl.createDocument(namespace,
                (prefix == null ? "" : prefix + ":") + elementName, null);
        rootNS = namespaceHolder.getDocumentElement();
        rootNS.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace);
    } catch (Exception e) {
        String err = "Error creating a namespace holder document: " + e.getLocalizedMessage();
        throw new TransformerException(err);
    }
    return rootNS;
}

From source file:Main.java

public static String getTextByFullName(Document paramDocument, String paramString) {
    String[] arrayOfString = toStringArray(paramString, ".");
    Element localElement = paramDocument.getDocumentElement();
    for (int i = 1; i < arrayOfString.length; ++i)
        localElement = getChildByName(localElement, arrayOfString[i]);
    return getText(localElement);
}

From source file:br.gov.lexml.parser.documentoarticulado.LexMLUtil.java

public static Document toDocument(String xml) {
    try {/*from   w ww  .ja v  a 2s  .  com*/
        InputStream input = IOUtils.toInputStream(xml, ENCODING);
        try {
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
            doc.getDocumentElement().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink",
                    "http://www.w3.org/1999/xlink");
            return doc;
        } finally {
            input.close();
        }
    } catch (Throwable e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:DOMEdit.java

public static void newEmail(Document doc, String newname, String newemail) {
        Element root = doc.getDocumentElement();
        Element person = (Element) root.getFirstChild();
        while (person != null) {
            Element name = (Element) person.getFirstChild();
            Text nametext = (Text) name.getFirstChild();
            String oldname = nametext.getData();
            if (oldname.equals(newname)) {
                Element email = (Element) name.getNextSibling();
                Text emailtext = (Text) email.getFirstChild();
                emailtext.setData(newemail);
            }//from w ww  . j  a  va 2 s  . c  o m
            person = (Element) person.getNextSibling();
        }
    }

From source file:DOMEdit.java

private static void makeNamelist(Document doc) {
        String names = null;/*from   w  w  w  .j a  v  a  2 s  . c o  m*/
        Element root = doc.getDocumentElement();
        NodeList nameElements = root.getElementsByTagName("name");
        for (int i = 0; i < nameElements.getLength(); i++) {
            Element name = (Element) nameElements.item(i);
            Text nametext = (Text) name.getFirstChild();
            if (names == null) {
                names = nametext.getData();
            } else {
                names += ", " + nametext.getData();
            }
        }
        Element namelist = doc.createElement("names");
        Text namelisttext = doc.createTextNode(names);
        namelist.appendChild(namelisttext);
        root.insertBefore(namelist, root.getFirstChild());
    }

From source file:Main.java

private static void addOneElement(Document file, String text, String elementName) {
    Element newNode = file.createElement(elementName);
    Text textMsg = file.createTextNode(text);
    newNode.appendChild(textMsg);/*from  w w w .  ja  va2 s  .c  o m*/
    file.getDocumentElement().appendChild(newNode);
    file.getDocumentElement().appendChild(file.createTextNode("\n  "));
}

From source file:DOMEdit.java

public static void newEmail(Document doc, String newname, String newemail) {
        Element root = doc.getDocumentElement();
        NodeList rootlist = root.getChildNodes();
        for (int i = 0; i < rootlist.getLength(); i++) {
            Element person = (Element) rootlist.item(i);
            NodeList personlist = person.getChildNodes();
            Element name = (Element) personlist.item(0);
            NodeList namelist = name.getChildNodes();
            Text nametext = (Text) namelist.item(0);
            String oldname = nametext.getData();
            if (oldname.equals(newname)) {
                Element email = (Element) personlist.item(1);
                NodeList emaillist = email.getChildNodes();
                Text emailtext = (Text) emaillist.item(0);
                emailtext.setData(newemail);
            }//  w ww.j a v a2 s  . c  o  m
        }
    }