Example usage for org.w3c.dom Node getNodeValue

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

Introduction

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

Prototype

public String getNodeValue() throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:Main.java

private static void reorganizePrimitiveToArray(Node parentNode, JsonObject upperJson, JsonObject childJson,
        Node childNode, JsonElement existing) {
    upperJson.remove(parentNode.getNodeName());
    JsonArray arrayJson = new JsonArray();
    arrayJson.add(existing);//from  w w w.  jav  a  2s . c om
    arrayJson.add(new JsonPrimitive(childNode.getNodeValue()));
    upperJson.add(parentNode.getNodeName(), arrayJson);
    REORGANIZED.put(parentNode.hashCode() + EMPTY, childJson);
}

From source file:Main.java

public static String getId(Node node) {
    try {//from  w  w w  .  ja  va  2 s  . c  o m
        NamedNodeMap nnm = node.getAttributes();
        Node attrib = nnm.getNamedItem("Id");
        Object ID;
        if (attrib.hasChildNodes()) {
            ID = attrib.getChildNodes().item(0).getNodeValue();
        } else {
            ID = attrib.getNodeValue();
        }
        return ID.toString();
    } catch (Exception ex) {
        return "";
    }
}

From source file:Main.java

/**
 * This method will return the content of this particular <code>element</code>.
 * For example,/*  w w  w. ja va  2  s .c  o  m*/
 *
 * <pre>
 *    <result>something_1</result>
 * </pre>
 * When the {@link org.w3c.dom.Element} <code>&lt;result&gt;</code> is passed in as
 * argument (<code>element</code> to this method, it returns the content of it,
 * namely, <code>something_1</code> in the example above.
 * 
 * @return
 */
public static String getContent(Element element) {
    StringBuffer paramValue = new StringBuffer();
    NodeList childNodes = element.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node currentNode = childNodes.item(j);
        if (currentNode != null && currentNode.getNodeType() == Node.TEXT_NODE) {
            String val = currentNode.getNodeValue();
            if (val != null) {
                paramValue.append(val.trim());
            }
        }
    }
    String val = paramValue.toString().trim();
    return val;
}

From source file:Main.java

public static String getNamespaceURI(final org.w3c.dom.Node n, final String prefix) {
    final Node prefixDeclaration = n.getAttributes().getNamedItem("xmlns:" + prefix);
    if (prefixDeclaration != null) {
        // we have found the good NameSpace
        return prefixDeclaration.getNodeValue();
    }//from   w ww .j  a  va2 s.  c  om
    // we have found the good NameSpace
    // we look for the NameSpace in the parent Node
    return getNamespaceURI(n.getParentNode(), prefix);
}

From source file:Main.java

/**
 * Copy one node to another node./* w  w  w.j a v a2 s.co m*/
 * @param source source Node
 * @param dest destination Node
 * @return destination Node
 */
public static synchronized Node copyNode(Node source, Node dest) {
    if (source.getNodeType() == Node.TEXT_NODE) {
        Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue());
        return tn;
    }

    Node attr = null;
    NamedNodeMap attrs = source.getAttributes();

    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            attr = attrs.item(i);
            ((Element) dest).setAttribute(attr.getNodeName(), attr.getNodeValue());
        }
    }

    Node child = null;
    NodeList list = source.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        child = list.item(i);
        if (!(child instanceof Text)) {
            Element en = dest.getOwnerDocument().createElementNS(child.getNamespaceURI(), child.getNodeName());

            if (child.getNodeValue() != null) {
                en.setNodeValue(child.getNodeValue());
            }

            Node n = copyNode(child, en);
            dest.appendChild(n);
        } else if (child instanceof CDATASection) {
            CDATASection cd = dest.getOwnerDocument().createCDATASection(child.getNodeValue());
            dest.appendChild(cd);
        } else {
            Text tn = dest.getOwnerDocument().createTextNode(child.getNodeValue());
            dest.appendChild(tn);
        }
    }
    return dest;
}

From source file:Main.java

protected static void getTextFromNode(Node node, StringBuffer buffer, boolean addSpace) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        buffer.append(node.getNodeValue());
        if (addSpace)
            buffer.append(" ");
    }/*from w w  w  . ja v a2s. com*/
    Node child = node.getFirstChild();
    while (child != null) {
        getTextFromNode(child, buffer, addSpace);
        child = child.getNextSibling();
    }
}

From source file:DomUtil.java

/**
 * Extract the textual content from a Node.
 * This is rather like the XPath value of a Node.
 * @param node The node to extract the text from
 * @return The textual value of the node
 *//* w  ww . j  a  v  a2 s  .c  o m*/
public static String getText(Node node) {
    StringBuffer reply = new StringBuffer();

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);

        if ((child instanceof CharacterData && !(child instanceof Comment))
                || child instanceof EntityReference) {
            reply.append(child.getNodeValue());
        } else if (child.getNodeType() == Node.ELEMENT_NODE) {
            reply.append(getText(child));
        }
    }

    return reply.toString();
}

From source file:Main.java

/**
 * Retrieves the String of a Node//from w  w  w  .j  a  va  2s .  c o m
 * 
 * @param n the Node
 * @return the String
 **/
public final static String toString(final Node n) {
    if (n == null) {
        return null;
    }

    return n instanceof Text ? n.getNodeValue() : n.getNodeName();
}

From source file:org.aectann.postage.TrackingStatusRefreshTask.java

public static TrackingInfo syncRequest(Context context, String tracking) throws FactoryConfigurationError {
    TrackingInfo result = null;/*from w  w  w  .j av a 2 s  .com*/
    if (tracking != null && tracking.length() > 0) {
        tracking = tracking.toUpperCase();
        HttpClient client = new DefaultHttpClient();
        try {
            HttpResponse response = client.execute(new HttpGet("http://prishlo.li/" + tracking + ".xml"));
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream content = response.getEntity().getContent();
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.parse(content);
                String weight = getFirstVaueOrNull(document, "weight");
                String from = getFirstVaueOrNull(document, "from");
                String kind = getFirstVaueOrNull(document, "kind");
                TrackingInfo old = TrackingStorageUtils.loadStoredTrackingInfo(tracking, context);
                result = new TrackingInfo(old != null ? old.getName() : null, tracking, weight, kind, from);
                NodeList checkpoints = document.getElementsByTagName("checkpoint");

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                for (int i = 0; i < checkpoints.getLength(); i++) {
                    Node current = checkpoints.item(i);
                    NamedNodeMap attributes = current.getAttributes();
                    Node state = attributes.getNamedItem("state");
                    Node attribute = attributes.getNamedItem("attribute");
                    Node date = attributes.getNamedItem("date");

                    String dateString = date.getNodeValue();
                    String attributeString = attribute.getNodeValue();
                    String stateString = state.getNodeValue();
                    String locationString = current.getFirstChild().getNodeValue();
                    result.addStatus(new TrackingStatus(stateString, dateFormat.parse(dateString),
                            attributeString, locationString));
                }
            }
        } catch (Exception e) {
            if (result == null) {
                result = new TrackingInfo(null, tracking, null, null, null);
            }
        }
    }
    return result;
}

From source file:Main.java

public static String getValueXPath(String srcXmlString, String xPath) {
    String value = null;/*from w ww.  j  a v a 2  s .  c  o  m*/
    try {
        Object result = execXpathGetNode(srcXmlString, xPath);
        Node node = (Node) result;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            value = node.getTextContent();
        } else {
            value = node.getNodeValue();
        }
        logger.debug(xPath + " = " + value);
    } catch (Exception ex) {
        logger.error(ex.getMessage() + " Could not extract any value using xpath: " + xPath);
    }
    return value;
}