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:com.msopentech.odatajclient.engine.data.impl.JSONDOMTreeUtils.java

/**
 * Serializes DOM content as JSON./*w w  w  . j  a va  2  s.  c om*/
 *
 * @param client OData client.
 * @param jgen JSON generator.
 * @param content content.
 * @param propType whether to output type information in the way needed for property values or not.
 * @throws IOException in case of write error.
 */
public static void writeSubtree(final ODataClient client, final JsonGenerator jgen, final Node content,
        final boolean propType) throws IOException {

    for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) {
        final String childName = XMLUtils.getSimpleName(child);

        final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
        if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) {
            jgen.writeStringField(
                    propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE,
                    typeAttr.getTextContent());

            jgen.writeObjectFieldStart(childName);
            GeospatialJSONHandler.serialize(client, jgen, (Element) child, typeAttr.getTextContent());
            jgen.writeEndObject();
        } else if (XMLUtils.hasOnlyTextChildNodes(child)) {
            if (child.hasChildNodes()) {
                final String out;
                if (typeAttr == null) {
                    out = child.getChildNodes().item(0).getNodeValue();
                } else {
                    final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent());
                    final ODataPrimitiveValue value = client.getPrimitiveValueBuilder().setType(type)
                            .setText(child.getChildNodes().item(0).getNodeValue()).build();
                    out = value.toString();

                    jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString());
                }
                jgen.writeStringField(childName, out);
            } else {
                if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) {
                    if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) {
                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE,
                                typeAttr.getTextContent());
                        jgen.writeStringField(childName, StringUtils.EMPTY);
                    } else {
                        jgen.writeArrayFieldStart(childName);
                        jgen.writeEndArray();
                    }
                } else {
                    jgen.writeNullField(childName);
                }
            }
        } else {
            if (XMLUtils.hasElementsChildNode(child)) {
                jgen.writeArrayFieldStart(childName);

                for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) {
                    if (XMLUtils.hasOnlyTextChildNodes(nephew)) {
                        jgen.writeString(nephew.getChildNodes().item(0).getNodeValue());
                    } else {
                        jgen.writeStartObject();
                        writeSubtree(client, jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

                jgen.writeEndArray();
            } else {
                jgen.writeObjectFieldStart(childName);
                if (typeAttr != null) {
                    jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent());
                }

                writeSubtree(client, jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}

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

public OpenHABLinkedPage(Node startNode) {
    if (startNode.hasChildNodes()) {
        NodeList childNodes = startNode.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeName().equals("id")) {
                this.setId(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("title")) {
                this.setTitle(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("icon")) {
                this.setIcon(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("link")) {
                this.setLink(childNode.getTextContent());
            }/*from   w  w w .  j a  va 2  s.c  o m*/
        }
    }
}

From source file:Main.java

private DefaultMutableTreeNode builtTreeNode(Node root) {
    DefaultMutableTreeNode dmtNode;

    dmtNode = new DefaultMutableTreeNode(root.getNodeName());
    NodeList nodeList = root.getChildNodes();
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);

        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            if (tempNode.hasChildNodes()) {
                dmtNode.add(builtTreeNode(tempNode));
            }/*  w  w w  . ja va2s. c o  m*/
        }
    }
    return dmtNode;
}

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

public OpenHABItem(Node startNode) {
    if (startNode.hasChildNodes()) {
        NodeList childNodes = startNode.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeName().equals("type")) {
                this.setType(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("name")) {
                this.setName(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("state")) {
                if (childNode.getTextContent().equals("Uninitialized")) {
                    this.setState(null);
                } else {
                    this.setState(childNode.getTextContent());
                }//from   w  w w.ja v a  2 s . c o m
            } else if (childNode.getNodeName().equals("link")) {
                this.setLink(childNode.getTextContent());
            }
        }
    }
}

From source file:com.jereksel.rommanager.XMLParser.java

/**
 * Getting node value//from  w w w  . ja  va  2 s  .  c o  m
 *
 * @param elem element
 */
public final String getElementValue(Node elem) {
    Node child;
    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:me.willowcheng.makerthings.model.OpenHABSitemap.java

public OpenHABSitemap(Node startNode) {
    if (startNode.hasChildNodes()) {
        NodeList childNodes = startNode.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeName().equals("name")) {
                this.setName(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("label")) {
                this.setLabel(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("link")) {
                this.setLink(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("icon")) {
                this.setIcon(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("homepage")) {
                if (childNode.hasChildNodes()) {
                    NodeList homepageNodes = childNode.getChildNodes();
                    for (int j = 0; j < homepageNodes.getLength(); j++) {
                        Node homepageChildNode = homepageNodes.item(j);
                        if (homepageChildNode.getNodeName().equals("link")) {
                            this.setHomepageLink(homepageChildNode.getTextContent());
                        } else if (homepageChildNode.getNodeName().equals("leaf")) {
                            if (homepageChildNode.getTextContent().equals("true")) {
                                setLeaf(true);
                            } else {
                                setLeaf(false);
                            }/* w ww . jav  a  2  s  . com*/
                        }
                    }
                }
            }
        }
    }
}

From source file:eu.planets_project.pp.plato.services.characterisation.jhove.JHoveAdaptor.java

/**
 * Creates the complete {@link JHoveTreeNode} from the given {@link Node}.
 * To display the informations property they have to be stored in a tree: the {@link JHoveTreeNode}
 * in this case/*w  ww  . j a v  a  2 s .c  o  m*/
 * 
 * @param node
 * @return
 */
private JHoveTreeNode getJHoveTreeNodeFromXPathNode(Node node) {
    JHoveTreeNode tmpNode;
    if (node.hasChildNodes()) {
        tmpNode = new JHoveTreeNode(node.getNodeName(), "node");
        NodeList nodelist = node.getChildNodes();
        for (int i = 0; i < node.getChildNodes().getLength(); i++) {
            tmpNode.addChild(getJHoveTreeNodeFromXPathNode(nodelist.item(i)));
        }
    } else {
        String tmpNodeContext = node.getNodeValue();

        // node value can be null. if it is null we change it to an empty string
        if (tmpNodeContext == null) {
            tmpNodeContext = "";
        }
        tmpNodeContext = tmpNodeContext.replaceAll("\n", "");
        tmpNodeContext = tmpNodeContext.replaceAll(" ", "");
        tmpNode = new JHoveTreeNode(tmpNodeContext, "leaf");
    }
    return tmpNode;
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Receive the node, the DOM object and the new name of the node
 * /*  w w  w. ja  v a 2 s.c  om*/
 * @param nodo
 * @param doc
 * @param newname
 */
public static Node changeNodename(Node nodo, org.w3c.dom.Document doc, String newname) {
    // Create an element with the new name
    Node element2 = doc.createElement(newname);

    // Copy the attributes to the new element
    NamedNodeMap attrs = nodo.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        element2.getAttributes().setNamedItem(attr2);
    }

    // Move all the children
    while (nodo.hasChildNodes()) {
        element2.appendChild(nodo.getFirstChild());
    }

    // Replace the old node with the new node
    nodo.getParentNode().replaceChild(element2, nodo);
    return element2;

}

From source file:com.nickandjerry.dynamiclayoutinflator.DynamicLayoutInflator.java

public static View inflate(Context context, Node node, ViewGroup parent) {
    View mainView = getViewForName(context, node.getNodeName());
    if (parent != null)
        parent.addView(mainView); // have to add to parent to enable certain layout attrs
    applyAttributes(mainView, getAttributesMap(node), parent);
    if (mainView instanceof ViewGroup && node.hasChildNodes()) {
        parseChildren(context, node, (ViewGroup) mainView);
    }/*from  w ww.j av a2s .  c  o m*/
    return mainView;
}

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

public OpenHABSitemapPage(Document document) {
    Node rootNode = document.getFirstChild();
    if (rootNode == null)
        return;/*from   ww w.  j av  a2 s.  com*/
    mRootWidget = new OpenHABWidget();
    mRootWidget.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")) {
                OpenHABWidget newOpenHABWidget = new OpenHABWidget(mRootWidget, childNode);
                mWidgets.add(newOpenHABWidget);
            } else if (childNode.getNodeName().equals("title")) {
                this.setTitle(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("id")) {
                this.setPageId(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("icon")) {
                this.setIcon(childNode.getTextContent());
            } else if (childNode.getNodeName().equals("link")) {
                this.setLink(childNode.getTextContent());
            }
        }
    }

}