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

static public Document getDocument(InputStream stream)
        throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

    Document doc = dBuilder.parse(stream);
    doc.getDocumentElement().normalize();

    return doc;//w  w  w .  ja va  2 s. c o  m
}

From source file:Main.java

public static String getXMLValueByName(String xmlFilePath, String name) {
    String returnValue = "No Value!";
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    try {//from   w  ww. j a v  a  2  s. c  om
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        InputStream is = new FileInputStream(xmlFilePath);
        Document doc = builder.parse(is);
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    String test = node.getNodeName();
                    if (test.equals(name)) {
                        returnValue = node.getTextContent().trim();
                    }
                }
            }
        }
    } catch (Exception e) {

    }
    return returnValue;
}

From source file:Main.java

public static Element GetCustomer(JFrame mainFrame, long lookupValue, Document CustomerDoc) {
    Element Customer = null;/*from   w  w w  .  ja v  a2s .c o m*/
    Element Root = CustomerDoc.getDocumentElement();
    if (Root.hasChildNodes()) {
        NodeList Customers = Root.getChildNodes();
        for (int i = 0; i < Customers.getLength(); i++) {
            if (Customers.item(i).getNodeName() == "#text")
                continue;
            Element Current = (Element) Customers.item(i);
            if (lookupValue == Integer.parseInt(Current.getAttribute("Id"))) {
                Customer = Current;
            }
        }
    }
    return Customer;
}

From source file:Main.java

/**
 * Method to get formatted Xml string from Xml document
 *
 * @param doc/*from w ww  . j  av  a 2  s .  c o  m*/
 * @return a formatted Xml string
 */
public static String getXmlStringFromDom(Document doc) {
    Source payload = new DOMSource(doc.getDocumentElement());
    return getXmlStringFromSource(payload);
}

From source file:Main.java

public static Element parseElement(String xml) {
    try {/*from  ww  w  .j a v a2 s. co  m*/
        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
        return doc.getDocumentElement();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String xmlDocToString(Document doc) {
    return elementToString(doc.getDocumentElement());
}

From source file:Main.java

public static Map<String, String> getConfigs(InputStream is)
        throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document dt = db.parse(is);
    Element element = dt.getDocumentElement();
    Map<String, String> config = new TreeMap<String, String>();

    NodeList propertyList = element.getElementsByTagName("property");
    int length = propertyList.getLength();
    for (int i = 0; i < length; i++) {
        Node property = propertyList.item(i);
        String key = property.getChildNodes().item(0).getTextContent();
        String value = property.getChildNodes().item(1).getTextContent();
        config.put(key, value);//from w  w w  . j a  v  a 2s .  c o  m
    }
    return config;
}

From source file:com.cxf.sts.WSO2STSTest.java

private static Element loadPolicy(String xmlPath) {
    try {//from w  w  w  . j  a  v  a2s. com
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document d = db.parse(new File(xmlPath));
        return d.getDocumentElement();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * sets the value of an Element in the Xml Document.
 * finds the first occurance of the element and sets its value.
 * @param root the root Element./*w w w . j av a2 s  .co  m*/
 * @param elemName the name of the element to search for.
 * @return String the element value or null if not found.
 */
public static void setNodeValue(Document doc, String elemName, String value) {
    Element root = doc.getDocumentElement();
    NodeList nl = root.getElementsByTagName(elemName);
    if (null != nl) {
        Node n = nl.item(0);
        if (null != n) {
            n.getFirstChild().setNodeValue(value);
        } else {
            addNode(doc, elemName, value);
        }
    } else {
        addNode(doc, elemName, value);
    }

}

From source file:Main.java

/**
 * Serialize XML from a {@link Document} to an OutputStream.
 * @param doc/*from w ww .j a va2  s  . c  om*/
 * @param out
 */
public static void serialiseXml(Document doc, Writer out) {
    DOMSource domSource = new DOMSource(doc.getDocumentElement());
    StreamResult streamResult = new StreamResult(out);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = null;
    try {
        serializer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
    }
    serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    try {
        serializer.transform(domSource, streamResult);
    } catch (TransformerException e) {
    }
}