Example usage for org.w3c.dom Node getAttributes

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

Introduction

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

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:Main.java

private static Set<String> getTree(Node doc) {
    final Set<String> set = new TreeSet<String>();
    if (doc == null)
        return set;

    String path = new String();
    if (doc.getPrefix() != null && doc.getLocalName() != null) {
        path = new String(doc.getPrefix() + ":" + doc.getLocalName());
        set.add(path);/*from w  ww  . j  av  a 2  s .c om*/
    }

    final NamedNodeMap attributes = doc.getAttributes();
    for (int i = 0; attributes != null && i < attributes.getLength(); i++) {
        final Node attribute = attributes.item(i);
        String name = attribute.getLocalName();
        if (attribute.getPrefix() != null)
            name = attribute.getPrefix() + ":" + name;
        set.add(path + "/@" + name);
    }

    final NodeList nodes = doc.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        final Node node = nodes.item(i);
        if (node instanceof Element) {
            final Set<String> children = getTree(node);
            for (final String child : children) {
                set.add(path + "/" + child);
            }
        }
    }
    return set;
}

From source file:de.matzefratze123.heavyspleef.util.I18NNew.java

private static void readEntry(Node node, String parentEntry) {
    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node childNode = list.item(i);
        String nodeName = childNode.getNodeName();

        if (nodeName.equalsIgnoreCase(MESSAGE_ENTRY)) {
            NamedNodeMap attributes = childNode.getAttributes();
            Node idNode = attributes.getNamedItem(ID_ATTRIBUTE);
            if (idNode == null) {
                Logger.warning("Warning: No id for message in " + MESSAGES_FILE + ". Ignoring message...");
                continue;
            }// www. jav  a  2  s. c o m

            String id = idNode.getNodeValue();
            String value = childNode.getTextContent();

            messages.put(parentEntry + id, value);
        } else if (nodeName.equalsIgnoreCase(ENTRY_ENTRY)) {
            NamedNodeMap attributes = childNode.getAttributes();
            Node nameNode = attributes.getNamedItem(ENTRY_NAME);
            String entryName = nameNode.getNodeValue();

            readEntry(childNode, parentEntry + entryName + ".");
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtilsV4.java

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(/*from www.  j  a  v a2  s .c o m*/
                    propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE,
                    typeAttr.getTextContent());

            jgen.writeObjectFieldStart(childName);
            GeospatialJSONHandler.serialize(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 {
                    if (typeAttr.getTextContent().startsWith("Edm.")) {
                        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());
                    } else {
                        // enum
                        out = child.getTextContent();
                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE,
                                typeAttr.getTextContent());
                    }
                }
                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();
                        DOMTreeUtils.writeSubtree(client, jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

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

                DOMTreeUtils.writeSubtree(client, jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtilsV3.java

/**
 * Serializes DOM content as JSON./*from  w w w  . ja v  a  2  s  . com*/
 *
 * @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(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();
                        DOMTreeUtils.writeSubtree(client, jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

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

                DOMTreeUtils.writeSubtree(client, jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.json.GeospatialJSONHandler.java

private static void serializeCrs(final JsonGenerator jgen, final Node node) throws IOException {
    if (node.getAttributes().getNamedItem(ODataConstants.ATTR_SRSNAME) != null) {
        final String srsName = node.getAttributes().getNamedItem(ODataConstants.ATTR_SRSNAME).getTextContent();
        final int prefIdx = srsName.indexOf(ODataConstants.JSON_GIS_URLPREFIX);
        final String crsValue = srsName.substring(prefIdx + ODataConstants.JSON_GIS_URLPREFIX.length());

        jgen.writeObjectFieldStart(ODataConstants.JSON_CRS);
        jgen.writeStringField(ODataConstants.ATTR_TYPE, ODataConstants.NAME);
        jgen.writeObjectFieldStart(ODataConstants.PROPERTIES);
        jgen.writeStringField(ODataConstants.NAME, "EPSG:" + crsValue);
        jgen.writeEndObject();// w  w w .  j a  v  a 2  s  . com
        jgen.writeEndObject();
    }
}

From source file:com.jaspersoft.jasperserver.ws.xml.Unmarshaller.java

private static ResourceProperty readResourceProperty(Node rpNode) {

    ResourceProperty rp = new ResourceProperty(null);
    NamedNodeMap nodeAttributes = rpNode.getAttributes();

    if (nodeAttributes.getNamedItem("name") != null)
        rp.setName(nodeAttributes.getNamedItem("name").getNodeValue());

    NodeList childsOfChild = rpNode.getChildNodes();
    for (int c_count = 0; c_count < childsOfChild.getLength(); c_count++) {
        Node child_child = (Node) childsOfChild.item(c_count);

        if (child_child.getNodeType() == Node.ELEMENT_NODE && child_child.getNodeName().equals("value")) {
            rp.setValue(readPCDATA(child_child));
        } else if (child_child.getNodeType() == Node.ELEMENT_NODE
                && child_child.getNodeName().equals("resourceProperty")) {
            rp.getProperties().add(readResourceProperty(child_child));
        }//from w w  w  .  ja v a 2  s  .  c  o  m
    }
    return rp;
}

From source file:Main.java

/**
 * * Convenience method to transfer a node (and all of its children) from one
 * * DOM XML document to another./*from   w w  w . j av a2s  . co  m*/
 * *
 * * Note: this method is recursive.
 * *
 * * @param current the current Element to append the transfer to
 * * @param target the target document for the transfer
 * * @param n the Node to transfer
 * * @return Element the current element.
 */
public static Element transferNode(Element current, Document target, Node n) {
    String name = n.getNodeName();
    String value = n.getNodeValue();
    short type = n.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        // create a new element for this node in the target document
        Element e = target.createElement(name);

        // move all the attributes over to the target document
        NamedNodeMap attrs = n.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node a = attrs.item(i);
            e.setAttribute(a.getNodeName(), a.getNodeValue());
        }

        // get the children for this node
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            // transfer each of the children to the new document
            transferNode(e, target, children.item(i));
        }

        // append the node to the target document element
        current.appendChild(e);
    } else if (type == Node.TEXT_NODE) {
        Text text = target.createTextNode(value);
        current.appendChild(text);
    }

    return current;
}

From source file:Main.java

public static Node cloneNode(Document d, Node n) {
    Node r = null;//from   w w  w.  j a  v a  2  s  .c om
    switch (n.getNodeType()) {
    case Node.TEXT_NODE:
        r = d.createTextNode(((Text) n).getData());
        break;
    case Node.CDATA_SECTION_NODE:
        r = d.createCDATASection(((CDATASection) n).getData());
        break;
    case Node.ELEMENT_NODE:
        r = d.createElement(((Element) n).getTagName());
        NamedNodeMap map = n.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ((Element) r).setAttribute(((Attr) map.item(i)).getName(), ((Attr) map.item(i)).getValue());
        }
        break;
    }
    return r;
}

From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtils.java

/**
 * Serializes DOM content as JSON.//from  www.  j  a  v a 2  s .  co  m
 *
 * @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 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(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 = new ODataPrimitiveValue.Builder().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(jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

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

                writeSubtree(jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}

From source file:Main.java

/**
 * Method to convert a NODE XML to a string.
 * @param n node XML to input./* w  w  w  .  j ava 2s.c  o m*/
 * @return string of the node n.
 */
public static String convertElementToString(Node n) {
    String name = n.getNodeName();
    short type = n.getNodeType();
    if (Node.CDATA_SECTION_NODE == type) {
        return "<![CDATA[" + n.getNodeValue() + "]]&gt;";
    }
    if (name.startsWith("#")) {
        return "";
    }
    StringBuilder sb = new StringBuilder();
    sb.append('<').append(name);
    NamedNodeMap attrs = n.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\"");
        }
    }
    String textContent;
    NodeList children = n.getChildNodes();
    if (children.getLength() == 0) {
        if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) {
            sb.append(textContent).append("</").append(name).append('>');
            //;
        } else {
            sb.append("/>").append('\n');
        }
    } else {
        sb.append('>').append('\n');
        boolean hasValidChildren = false;
        for (int i = 0; i < children.getLength(); i++) {
            String childToString = convertElementToString(children.item(i));
            if (!"".equals(childToString)) {
                sb.append(childToString);
                hasValidChildren = true;
            }
        }
        if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) {
            sb.append(textContent);
        }
        sb.append("</").append(name).append('>');
    }
    return sb.toString();
}