Example usage for org.w3c.dom Element hasChildNodes

List of usage examples for org.w3c.dom Element hasChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Element hasChildNodes.

Prototype

public boolean hasChildNodes();

Source Link

Document

Returns whether this node has any children.

Usage

From source file:Main.java

public static List<Element> elements(Element element, Set<String> allowedTagNames) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }// w w w . j  av a2s . co  m

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            if (allowedTagNames.contains(child.getLocalName())) {
                elements.add(childElement);
            }
        }
    }
    return elements;
}

From source file:Main.java

public static List<Element> elements(Element element, String namespace, String localName) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }//  w  w w  .j  a  v  a 2 s  .c  o  m

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        String childNamespace = element.getNamespaceURI();
        if (child.getNodeType() == Node.ELEMENT_NODE
                && (namespace != null ? namespace.equals(childNamespace) : childNamespace == null)
                && localName.equals(element.getLocalName())) {
            elements.add((Element) child);
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Returns the text content of the provided element as is.  If multiple text
 * DOM nodes are present, they are concatenated together. 
 * /*  ww w  .  j a v a  2  s  .c om*/
 * @param element The element that contains the desired text content.
 * @return The text content of the given element.
 * @throws Exception If an exception occurs during the traversal of the DOM
 * objects.
 */
public static String getElementTextData(Element element) throws Exception {
    if (!element.hasChildNodes()) {
        throw new Exception("Element has no children.");
    }

    Node n = element.getFirstChild();

    if ((n.getNodeType() != Node.TEXT_NODE) && (n.getNodeType() != Node.CDATA_SECTION_NODE)) {
        // must be a textual node
        // for now throw an exception, but later need to just skip this module and
        // resume initialization
        throw new Exception("Element child node is not textual.");
    }

    CharacterData value = (CharacterData) n;
    return value.getData();
}

From source file:Main.java

/**
 *
 * @param document/*w  ww  .j  av  a  2  s .co  m*/
 * @param tagName
 * @return
 */
public static String getTagValueAsString(Document document, String tagName) {
    if (document == null || tagName == null || tagName.length() == 0)
        return null;

    String tagValue = null;
    NodeList nlist = null;
    Element element = null;

    Element root = document.getDocumentElement();

    nlist = root.getElementsByTagName(tagName);
    element = (Element) nlist.item(0);
    if (element.hasChildNodes()) {
        tagValue = element.getFirstChild().getNodeValue();
    }
    return tagValue;
}

From source file:com.ibm.soatf.tool.ValidateTransferedValues.java

private static boolean isLeafElement(Element e) {
    boolean leaf = true;
    if (e.hasChildNodes()) {
        NodeList list = e.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
                leaf = false;//www .jav a  2 s .  c  o m
                break;
            }
        }
    }
    return leaf;
}

From source file:Main.java

public static void edit(Document doc) {
    Element element = doc.getDocumentElement();
    Element element2 = doc.createElement("newname");
    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        element2.getAttributes().setNamedItem(attr2);
    }/*from   w w  w  . ja v a2 s. co  m*/
    while (element.hasChildNodes()) {
        element2.appendChild(element.getFirstChild());
    }
    element.getParentNode().replaceChild(element2, element);
}

From source file:org.zaizi.oauth2utils.OAuthUtils.java

public static void parseXMLDoc(Element element, Document doc, Map<String, String> oauthResponse) {
    NodeList child = null;/*from  w  w  w. j a  v a  2 s.c  om*/
    if (element == null) {
        child = doc.getChildNodes();

    } else {
        child = element.getChildNodes();
    }
    for (int j = 0; j < child.getLength(); j++) {
        if (child.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            org.w3c.dom.Element childElement = (org.w3c.dom.Element) child.item(j);
            if (childElement.hasChildNodes()) {
                System.out.println(childElement.getTagName() + " : " + childElement.getTextContent());
                oauthResponse.put(childElement.getTagName(), childElement.getTextContent());
                parseXMLDoc(childElement, null, oauthResponse);
            }

        }
    }
}

From source file:de.ingrid.portal.global.UtilsMapServiceManager.java

public static HashMap createKmlFromIDF(String iPlugId, int documentId)
        throws ConfigurationException, Exception {
    IBUSInterface ibus = IBUSInterfaceImpl.getInstance();
    IngridHit ingridHit = new IngridHit();
    String kmlFile = "";
    HashMap data = new HashMap();
    Record record;/*from  w  w  w.  j a v a  2  s.co  m*/

    ingridHit.setDocumentId(documentId);
    ingridHit.setPlugId(iPlugId);
    record = ibus.getRecord(ingridHit);

    if (record != null) {
        String idfString = record.getString("data");
        if (idfString != null) {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db;
            db = dbf.newDocumentBuilder();
            Document idfDoc = db.parse(new InputSource(new StringReader(idfString)));

            Node node;
            XPathUtils.getXPathInstance(new IDFNamespaceContext());

            Element rootNode = idfDoc.getDocumentElement();
            if (rootNode != null) {
                if (rootNode.hasChildNodes()) {
                    Node kmlNode = XPathUtils.getNode(rootNode, "./idf:body/kml:kml");
                    evaluateKmlNode(data, kmlNode);
                }
            }

            if (data != null) {
                try {
                    kmlFile = UtilsMapServiceManager.createTemporaryService((String) data.get("coord_class"),
                            (ArrayList) data.get("placemarks"), UtilsFileHelper.KML);
                } catch (ConfigurationException e) {
                    log.error("ConfigurationException" + e);
                } catch (Exception e) {
                    log.error("Exception" + e);
                }
            }
        }
    }

    if (kmlFile != null && kmlFile.length() > 0) {
        data.put("kml_url", "./kml/" + kmlFile);
        data.put("kml_path", getTmpDirectory(getConfig().getString("temp_service_path", null)));
    }
    return data;
}

From source file:Main.java

/**
 * Rename an element in a DOM document. It happens to involves a node
 * replication./*  ww w.jav a2  s .  c  om*/
 * 
 * @param document
 *            The document containing the element (some way to verify
 *            that?).
 */
public static Element renameElement(Document document, Element element, String newName, String namespace) {
    if (namespace == null) {
        throw new IllegalArgumentException("No namespace provided for element " + element);
    }
    Element newElement = document.createElementNS(namespace, newName);
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0, iEnd = attributes.getLength(); i < iEnd; i++) {
        Attr attr2 = (Attr) document.importNode(attributes.item(i), true);
        newElement.getAttributes().setNamedItem(attr2);
    }
    while (element.hasChildNodes()) {
        newElement.appendChild(element.getFirstChild());
    }
    element.getParentNode().replaceChild(newElement, element);
    return newElement;
}

From source file:com.msopentech.odatajclient.engine.data.ODataBinder.java

private static PropertyType guessPropertyType(final Element property) {
    PropertyType res = null;/*w w w.  j  a va2s  .  c om*/

    if (property.hasChildNodes()) {
        final NodeList children = property.getChildNodes();

        for (int i = 0; res == null && i < children.getLength(); i++) {
            final Node child = children.item(i);

            if (child.getNodeType() == Node.ELEMENT_NODE
                    && !child.getNodeName().startsWith(ODataConstants.PREFIX_GML)) {

                res = ODataConstants.ELEM_ELEMENT.equals(XMLUtils.getSimpleName(child))
                        ? PropertyType.COLLECTION
                        : PropertyType.COMPLEX;
            }
        }
    } else {
        res = PropertyType.EMPTY;
    }

    if (res == null) {
        res = PropertyType.PRIMITIVE;
    }

    return res;
}