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

/** Extract all of the child nodes as a Properties object from a node in an XML document */
public static Properties extractChildNodes(Document document) {
    try {/*from  www.java 2  s  .c  o  m*/
        Properties props = new Properties();
        Element top = document.getDocumentElement();
        NodeList children = top.getChildNodes();
        Node child;
        String name;
        String value;
        for (int i = 0; i < children.getLength(); i++) {
            child = children.item(i);
            name = child.getNodeName();
            value = child.getTextContent().trim();
            props.setProperty(name, value);
        }
        return props;
    } catch (Exception e) {
        return new Properties();
    }
}

From source file:Main.java

public static Set<String> getUniqueElementNames(Document dom) {
    HashSet<String> hNames = new HashSet<String>();
    Node docNode = dom.getDocumentElement();
    walkNodes(docNode, hNames);//  w w w  .  ja  v a2  s.c  o m
    return hNames;
}

From source file:Main.java

public static void addHook(String hookName, String interfaceName, String identity, boolean superOverride,
        String superOverrideClass) {
    try {//w w  w  . j a v  a  2  s. c o  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBldr = dFactory.newDocumentBuilder();
        Document doc = dBldr.parse(new File("hooks.xml"));

        Element hooks = doc.getDocumentElement();

        Element hook = doc.createElement("hook");
        hook.setAttribute("name", hookName);

        Element eIdentity = doc.createElement("identity");
        eIdentity.appendChild(doc.createTextNode(identity));
        hook.appendChild(eIdentity);

        Element eInterface = doc.createElement("interface");
        eInterface.appendChild(doc.createTextNode(interfaceName));
        hook.appendChild(eInterface);

        Element eSuper = doc.createElement("super");
        if (superOverride) {
            eSuper.appendChild(doc.createTextNode(superOverrideClass));
        } else {
            eSuper.appendChild(doc.createTextNode("null"));
        }
        hook.appendChild(eSuper);

        hooks.insertBefore(hook, hooks.getLastChild());
        write(doc);
    } catch (SAXException | IOException | ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String write(Document doc) throws Exception {
    return write(doc.getDocumentElement());
}

From source file:Main.java

public static Node readXML(URI uri) {
    try {//from  w w  w.  jav  a  2s  . co m
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        Document doc = builder.parse(uri.toString());
        return doc.getDocumentElement();
    } catch (SAXException saxEx) {
        throw new IllegalArgumentException("readXML() Caught SAXException: ", saxEx);
    } catch (IOException ioEx) {
        throw new IllegalArgumentException("readXML() Caught IOException: " + ioEx.getMessage(), ioEx);
    } catch (ParserConfigurationException parsEx) {
        throw new IllegalArgumentException("readXML() Caught ParserConfigurationException: ", parsEx);
    } // try - catch
}

From source file:Main.java

public static String getRequestNameFull(String xml)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));

    NodeList nodeList = document.getDocumentElement().getChildNodes();
    Node reqName = document.getElementsByTagName("request-name").item(0);
    System.out.println(reqName.getTextContent());

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        System.out.println(i + "--" + node);
        if (node instanceof Element) {
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);
                System.out.println(i + "--" + j + "--" + cNode);
                if (cNode instanceof Element) {
                    String content = cNode.getLastChild().getTextContent().trim();
                    System.out.println(i + "--" + j + "--" + content);
                }// ww w  .ja  v  a2 s.c  o  m
            }
        }
    }
    /*
     * Do the parsing for reqname
     */
    return reqName.getTextContent();
}

From source file:Main.java

/**
 * Adds stylesheet informations to an xml document. See
 * <a href='http://stackoverflow.com/questions/2651647/add-xml-stylesheet-and-get-standalone-yes'>Stack Overflow</a>.
 *
 * @param aDocument/*from   w  ww. j  av a  2  s .com*/
 * @param aFilename
 */
public static void addStylesheet(Document aDocument, String aFilename) {

    aDocument.setXmlStandalone(true);

    Element root = aDocument.getDocumentElement();

    String data = DATA_STYLESHEET.replace(LOCATION_PLACEHOLDER, aFilename);
    ProcessingInstruction processingInstruction = aDocument.createProcessingInstruction(TARGET_STYLESHEET,
            data);

    aDocument.insertBefore(processingInstruction, root);
}

From source file:DOMEdit.java

public static void importName(Document doc1, Document doc2) {
        Element root1 = doc1.getDocumentElement();
        Element personInDoc1 = (Element) root1.getFirstChild();

        Node importedPerson = doc2.importNode(personInDoc1, true);

        Element root2 = doc2.getDocumentElement();
        root2.appendChild(importedPerson);
    }//w  w w  .ja  v a2  s.  c  o m

From source file:com.grameenfoundation.ictc.utils.XMLParser.java

public static Document parseXmlText(String requestType) {
    //get the factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {/*from ww  w  .  java2 s.  c  o m*/

        //Using factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();

        //InputSource is = new InputSource();
        //is.setCharacterStream(new StringReader(data));

        //File f = new File("/home/nii-amon/Documents/cardlessXML/XMLFile.xml");
        File f = new File(requestType);
        //File f = new File("/home/nii-amon/Documents/cardlessXML/txnType.xml");
        Document doc = db.parse(f);
        // normalize text representation
        doc.getDocumentElement().normalize();
        System.out.println("Root element of the doc is " + doc.getDocumentElement().getNodeName());

        return doc;

    } catch (ParserConfigurationException pce) {
        //Log.info("ParserConfigurationException. Reason: "+pce.getMessage());
        pce.printStackTrace();
        return null;
    } catch (IOException ioe) {
        //Log.info("IOException. Reason: "+ioe.getMessage());
        ioe.printStackTrace();
        return null;
    } catch (org.xml.sax.SAXException ex) {
        Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);

    }
    return null;

}

From source file:Main.java

public static void ReadXMLFile2() {

    try {/*from  w  w  w  .j  a  va  2  s  . c o m*/

        File file = new File("D:\\FAR_Documents\\__Startamap\\Home.xml");

        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        Document doc = dBuilder.parse(file);

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        if (doc.hasChildNodes()) {

            printNote(doc.getChildNodes());

        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}