Example usage for org.w3c.dom Node hasChildNodes

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

Introduction

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

Prototype

public boolean hasChildNodes();

Source Link

Document

Returns whether this node has any children.

Usage

From source file:Main.java

private static String toString(Node node, int level, boolean indent) {

    StringBuilder nodeBuilder = new StringBuilder();

    switch (node.getNodeType()) {
    case Node.TEXT_NODE:
        if (indent) {
            nodeBuilder.append(setIndent(level));
        }/*from w ww  .j ava  2s  .c o  m*/
        nodeBuilder.append(node.getNodeValue());
        if (indent) {
            nodeBuilder.append("\n");
        }
        break;
    case Node.ELEMENT_NODE:

        NamedNodeMap attributeMap;
        NodeList childList;

        if (indent) {
            nodeBuilder.append(setIndent(level));
        }
        nodeBuilder.append("<");
        nodeBuilder.append(((Element) node).getTagName());

        attributeMap = node.getAttributes();
        for (int loop = 0; loop < attributeMap.getLength(); loop++) {
            nodeBuilder.append(" ");
            nodeBuilder.append(attributeMap.item(loop).getNodeName());
            nodeBuilder.append("=\"");
            nodeBuilder.append(attributeMap.item(loop).getNodeValue());
            nodeBuilder.append("\"");
        }

        if (node.hasChildNodes()) {
            nodeBuilder.append(">");
            if (indent) {
                nodeBuilder.append("\n");
            }

            childList = node.getChildNodes();
            for (int loop = 0; loop < childList.getLength(); loop++) {
                nodeBuilder.append(toString(childList.item(loop), level + 1, indent));
            }

            if (indent) {
                nodeBuilder.append(setIndent(level));
            }
            nodeBuilder.append("</");
            nodeBuilder.append(((Element) node).getTagName());
            nodeBuilder.append(">");
            if (indent) {
                nodeBuilder.append("\n");
            }
        } else {
            nodeBuilder.append("/>");
            if (indent) {
                nodeBuilder.append("\n");
            }
        }
        break;
    default:
        nodeBuilder.append("<Unkown Node Type (");
        nodeBuilder.append(node.getNodeType());
        nodeBuilder.append(")/>");
        if (indent) {
            nodeBuilder.append("\n");
        }
    }

    return nodeBuilder.toString();
}

From source file:com.mingo.parser.xml.dom.ContextConfigurationParser.java

/**
 * Parse <querySetConfig/> tag.//from   ww  w  .  j  av  a 2 s  .  co m
 *
 * @param element element of XML document
 * @return set of paths queries definitions
 */
private QuerySetConfiguration parseQuerySetConfigTag(Element element) throws ParserException {
    Set<String> querySets = ImmutableSet.of();
    // expected what xml contains single <querySetConfig> tag
    // therefore take first element from node list is normal
    Node querySetConfigNode = getFirstNecessaryTagOccurrence(element, QUERY_SET_CONFIG_TAG);
    if (querySetConfigNode.hasChildNodes()) {
        querySets = parseQuerySets(querySetConfigNode.getChildNodes());
    }
    QuerySetConfiguration querySetConfiguration = new QuerySetConfiguration();
    querySetConfiguration.setDatabaseName(getAttributeString(querySetConfigNode, MONGO_DBNAME_ATTR));
    querySetConfiguration.addQuerySet(querySets);
    return querySetConfiguration;

}

From source file:com.connectutb.xfuel.FuelPlanner.java

public final String getElementValue(Node elem) {
    Node child;/* w w w  . ja  v a  2 s  .co  m*/
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.action.validation.BcrExperimentFieldValidator.java

/**
 * Determines if a tag is a simple text element
 * @param node to determine if it is a simple text element
 * @return true if a node is a simple text eleement
 *//*from ww  w  .j a  v  a  2s . c o m*/
private boolean isSimpleTextElement(final Node node) {
    // either the node has no children, or it has just one child of type text node
    // (i.e. false if it has other element nodes nested inside of it
    return !node.hasChildNodes()
            || (node.getChildNodes().getLength() == 1 && node.getFirstChild().getNodeType() == Node.TEXT_NODE);
}

From source file:de.elbe5.base.data.XmlData.java

public String getCData(Node node) {
    if (node.hasChildNodes()) {
        Node child = node.getFirstChild();
        while (child != null) {
            if (child instanceof CDATASection) {
                return ((CDATASection) child).getData();
            }//  w w w . j a  v  a2s . co m
            child = child.getNextSibling();
        }
    }
    return "";
}

From source file:org.dasein.cloud.ibm.sce.identity.keys.SSHKeys.java

private @Nullable SSHKeypair toKeyPair(@Nonnull ProviderContext ctx, @Nullable Node node, boolean post)
        throws CloudException, InternalException {
    if (node == null || !node.hasChildNodes()) {
        return null;
    }/*from   ww w .  j a v  a  2 s  . c  om*/
    NodeList attributes = node.getChildNodes();
    String regionId = ctx.getRegionId();

    if (regionId == null) {
        throw new CloudException("No region established for context");
    }
    SSHKeypair kp = new SSHKeypair();

    kp.setProviderOwnerId(ctx.getAccountNumber());
    kp.setProviderRegionId(regionId);
    kp.setFingerprint("Fake out test cases because SCE does not provide a fingerprint");
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attr = attributes.item(i);
        String nodeName = attr.getNodeName();

        if (nodeName.equalsIgnoreCase("KeyName") && attr.hasChildNodes()) {
            String id = attr.getFirstChild().getNodeValue().trim();

            kp.setProviderKeypairId(id);
            kp.setName(id);
        } else if (nodeName.equalsIgnoreCase("KeyMaterial") && attr.hasChildNodes()) {
            String material = attr.getFirstChild().getNodeValue().trim();

            try {
                if (post) {
                    kp.setPrivateKey(material.getBytes("utf-8"));
                } else {
                    kp.setPublicKey(material);
                }
            } catch (UnsupportedEncodingException e) {
                throw new InternalException(e);
            }
        }
    }
    if (kp.getProviderKeypairId() == null) {
        return null;
    }
    return kp;
}

From source file:me.willowcheng.makerthings.model.OpenHABWidgetDataSource.java

public void setSourceNode(Node rootNode) {
    Log.i(TAG, "Loading new data");
    if (rootNode == null)
        return;/*ww  w  .j a  va2s.c om*/
    rootWidget = new OpenHABWidget();
    rootWidget.setType("root");
    if (rootNode.hasChildNodes()) {
        NodeList childNodes = rootNode.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeName().equals("widget")) {
                new OpenHABWidget(rootWidget, childNode);
            } else if (childNode.getNodeName().equals("title")) {
                this.setTitle(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("id")) {
                this.setId(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("icon")) {
                this.setIcon(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("link")) {
                this.setLink(childNode.getTextContent());
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.xml.XmlReaderXPath.java

/**
 * Add the text in current node to document text buffer, create and add to index a Field
 * annotation out of the text. This usually processes a document.
 *///w w w.  java 2 s.com
private void processNode(CAS cas, Node node, StringBuilder documentText) {
    if (node.hasChildNodes()) {
        if (docIdTag != null) {
            ensureIdValidity(node);
        }

        NodeList docFields = node.getChildNodes();

        for (int i = 0; i < docFields.getLength(); i++) {
            Node field = docFields.item(i);
            int begin = documentText.length();
            String nodeTag = field.getLocalName();

            if (nodeTag != null && isIncluded(nodeTag)) {
                String nodeText = field.getTextContent();

                documentText = documentText.append(nodeText);
                int end = documentText.length();

                documentText = documentText.append("\n");

                // Substitue tag if specified
                if (useSubstitution && substitution.containsKey(nodeTag)) {
                    nodeTag = substitution.get(nodeTag);
                }

                createFieldAnnotation(cas, nodeTag, begin, end);
            }
        }
    }
}

From source file:de.elbe5.base.data.XmlData.java

private void findChildElements(Node parent, String tagName, boolean recursive, List<Element> list) {
    if (parent.hasChildNodes()) {
        NodeList childNodes = parent.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if ((node instanceof Element) && ((Element) node).getTagName().equals(tagName))
                list.add((Element) node);
            if (recursive) {
                findChildElements(node, tagName, true, list);
            }/*from  ww w  .ja va 2s  .co m*/
        }
    }
}

From source file:org.liberty.android.fantastischmemopro.downloader.DownloaderFE.java

private List<DownloadItem> retrieveList(String criterion) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    String url;//  w  ww  .j  a v a 2  s.  c  o  m
    if (validateEmail(criterion)) {
        url = FE_API_EMAIL;
    } else {
        url = FE_API_TAG;
    }
    url += URLEncoder.encode(criterion);
    Log.i(TAG, "Url: " + url);
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;
    response = httpclient.execute(httpget);
    Log.i(TAG, "Response: " + response.getStatusLine().toString());
    HttpEntity entity = response.getEntity();

    if (entity == null) {
        throw new Exception("Null entity error");
    }

    InputStream instream = entity.getContent();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    Document doc = documentBuilder.parse(instream);
    NodeList nodes = doc.getElementsByTagName("cardSet");
    nodes = doc.getElementsByTagName("error");

    if (nodes.getLength() > 0) {
        throw new Exception("Invalid user name or no such tag to search!");
    }

    List<DownloadItem> diList = new LinkedList<DownloadItem>();
    nodes = doc.getElementsByTagName("cardSet");

    int nodeNumber = nodes.getLength();
    for (int i = 0; i < nodeNumber; i++) {
        Node node = nodes.item(i);
        if (!node.hasChildNodes()) {
            continue;
        }
        NodeList childNodes = node.getChildNodes();

        int childNodeNumber = childNodes.getLength();
        DownloadItem di = new DownloadItem();
        for (int j = 0; j < childNodeNumber; j++) {
            Node childNode = childNodes.item(j);
            if (childNode.hasChildNodes()) {
                di.setType(DownloadItem.TYPE_DATABASE);
                if (childNode.getNodeName().equals("title")) {
                    di.setTitle(childNode.getFirstChild().getNodeValue());
                } else if (childNode.getNodeName().equals("cardSetId")) {
                    di.setAddress(FE_API_FLASHCARDS + childNode.getFirstChild().getNodeValue());
                } else if (childNode.getNodeName().equals("description")) {
                    di.setDescription(childNode.getFirstChild().getNodeValue());
                }
            }
        }
        if (!di.getTitle().equals("")) {
            diList.add(di);
        }
    }
    instream.close();
    return diList;
}