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 getElement(Document doc, String tagName) {
    return getElement(doc.getDocumentElement(), tagName);
}

From source file:Main.java

/**
 * Appends two documents// w w  w  . j a v  a 2 s. c om
 * 
 * @param curDoc      source document
 * @param appendDoc      document to append to source
 * @param eleNameToAdd   element tag name in source to which the appendDoc has to be appended as a child
 * @throws Exception
 */
public static void appendDocs(Document curDoc, Document appendDoc, String eleNameToAdd) throws Exception {

    Node importNode = curDoc.importNode(appendDoc.getDocumentElement(), true);
    curDoc.getElementsByTagName(eleNameToAdd).item(0).appendChild(importNode);

}

From source file:Main.java

/**
 * Parse une string contenant un document XML dans un DOM Element.
 * /*from   w  ww. j  av  a  2  s .  c  om*/
 * @param xml le document XML sous forme de string
 * @return l'element racine du document (root element), ou null si erreur
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public static Element stringToDomElement(final String xml)
        throws IOException, SAXException, ParserConfigurationException {
    Element element = null;
    if (xml != null) {
        final DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        final Document doc = db.parse(new InputSource(new StringReader(xml)));
        element = doc.getDocumentElement();
    }
    return element;
}

From source file:Main.java

public static String DocumentToString(Document doc) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ElementToStream(doc.getDocumentElement(), baos);
    return new String(baos.toByteArray());
}

From source file:Main.java

public static Element getDocumentElment(final byte[] xmlFragement) throws IOException, SAXException {
    Document doc = builder.parse(new ByteArrayInputStream(xmlFragement));
    return doc.getDocumentElement();
}

From source file:Main.java

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

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

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

        NodeList songList = doc.getElementsByTagName("song");
        Log.d("ReadItemsFromFile", "List Length: " + songList.getLength());

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

            Node nNode = songList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                HashMap<String, String> mapElement = new HashMap<String, String>();

                mapElement.put("name", eElement.getElementsByTagName("name").item(0).getTextContent());
                mapElement.put("artist", eElement.getElementsByTagName("artist").item(0).getTextContent());
                mapElement.put("startTime",
                        eElement.getElementsByTagName("startTime").item(0).getTextContent());
                mapElement.put("url", eElement.getElementsByTagName("url").item(0).getTextContent());

                results.add(mapElement);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return results;

}

From source file:Main.java

/**
 * Inserts a new value for an XML tag specified by <code>tagName</code> name
 * in a <code>Document</code> object.
 * /*  w ww  .j a v  a 2  s  .  co m*/
 * @param doc
 *            Document object.
 * @param tagName
 *            Name of the tag as String.
 * @param tagValue
 *            Value of the tag as String.
 */
public static Element insertTagValue(Document doc, String tagName, String tagValue) {
    Element element = doc.createElement(tagName);
    doc.getDocumentElement().appendChild(element);
    if (tagValue != null) {
        element.appendChild(doc.createTextNode(tagValue));
    }
    return element;
}

From source file:Main.java

public static Element buildDoc(String content) throws Exception {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document dom = db.parse(new ByteArrayInputStream(content.getBytes("UTF-8")));
    Element docEle = dom.getDocumentElement();

    return docEle;
}

From source file:Main.java

/**
 * Return he dom root element of an xml file
 * @param filePathInClassPath  the file path relative to the classpath
 * @return  the root element/*from ww w  . j ava2  s. c om*/
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 */
public static Element getRootElementFromFileInClasspath(String filePathInClassPath)
        throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root elements
    Document doc = docBuilder.parse(new ClassPathResource(filePathInClassPath).getInputStream());
    return doc.getDocumentElement();
}

From source file:Main.java

public static Element getElement(Document doc, String tagName, int index) {
    return getElement(doc.getDocumentElement(), tagName, index);
}