Example usage for org.w3c.dom Element getFirstChild

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

Introduction

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

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:no.dusken.aranea.admin.control.ImportStvMediaController.java

/**
* I take a xml element and the tag name, look for the tag and get the text
* content i.e for <employee><name>John</name></employee> xml snippet if
* the Element points to employee node and tagName is name I will return John
*//*www.  j ava  2  s .  com*/
private String getTextValue(Element ele, String tagName) {
    String textVal = null;

    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * Appends the specified value as a text node to the element. If the
 * value is <code>null</code>, the element's first child node will be
 * removed.//from w ww  .j  a v a  2 s  .c  o  m
 *
 * @param element the element.
 * @param value   the element's value.
 */
public static void setElementValue(Element element, String value) {
    Node child = element.getFirstChild();

    if (value != null) {
        if (child == null) {
            child = element.getOwnerDocument().createTextNode("");
            element.appendChild(child);
        }

        child.setNodeValue(value);
    } else {
        if (child != null) {
            element.removeChild(child);
        }
    }
}

From source file:net.frontlinesms.plugins.forms.FormsPluginController.java

/**
 * Fabaris_a.zanchi This method extracts from the xml the value of a
 * FormField//  w w  w  .j  a  v a  2 s. co  m
 *
 * @param xmlContent String containing the entire xml
 * @param formField object of type {@link FormField}
 * @return a String with the value of the respective FormField extracted
 * from the xmlConent
 * @throws Exception
 */
public static String getFormDataXml(String xmlContent, FormField formField) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    InputSource inStream = new InputSource();
    inStream.setCharacterStream(new StringReader(xmlContent));
    org.w3c.dom.Document xmlDoc = docBuilder.parse(inStream);
    xmlDoc.getDocumentElement().normalize();
    String fieldValue = null;
    if (formField.getType() != FormFieldType.REPEATABLES) {
        if (formField.getType() != FormFieldType.CURRENCY_FIELD) {
            //30.10.2013
            //NodeList nodeList = xmlDoc.getElementsByTagName(String.valueOf(formField.getId()).trim() + "_" + formField.getPositionIndex());

            NodeList nodeList = xmlDoc
                    .getElementsByTagName(formField.getName().trim() + "_" + formField.getPositionIndex());
            if (nodeList.getLength() == 0) // check if the field exsists in xml
            {
                return "";
            }
            Node dataNode = nodeList.item(0);
            Element elem = (Element) dataNode;
            if (elem.getFirstChild() == null) {
                return "";
            }
            fieldValue = elem.getFirstChild().getNodeValue();
        } else {
            // search for import value
            //30.10.2013
            //NodeList nodeList = xmlDoc.getElementsByTagName(String.valueOf(formField.getId()).trim() + "_" + formField.getPositionIndex());

            NodeList nodeList = xmlDoc
                    .getElementsByTagName(formField.getName().trim() + "_" + formField.getPositionIndex());
            if (nodeList.getLength() == 0) // check if the field exsists in
            // xml
            {
                return "";
            }
            Node dataNode = nodeList.item(0);
            Element elem = (Element) dataNode;
            if (elem.getFirstChild() == null) {
                return "";
            }
            fieldValue = elem.getFirstChild().getNodeValue();
            // Search for currency
            //30.10.2013
            //NodeList nodeList2 = xmlDoc.getElementsByTagName(String.valueOf(formField.getId()).trim() + "_" + formField.getPositionIndex() + "_curr");

            NodeList nodeList2 = xmlDoc.getElementsByTagName(
                    formField.getName().trim() + "_" + formField.getPositionIndex() + "_curr");
            Node dataNode2 = nodeList2.item(0);
            Element currencyElem = (Element) dataNode2;
            if (currencyElem.getFirstChild() != null) {
                fieldValue = fieldValue + currencyElem.getFirstChild().getNodeValue();
            }
        }
    } else {
        // In this case we have to search "deeper"
        //30.10.2013
        //NodeList nodeList = xmlDoc.getElementsByTagName(String.valueOf(formField.getId()).trim() + "_" + formField.getPositionIndex());

        NodeList nodeList = xmlDoc
                .getElementsByTagName(formField.getName().trim() + "_" + formField.getPositionIndex());
        Node dataNode = nodeList.item(0);
        Element elem = (Element) dataNode;
        //30.10.2013
        //NodeList repNodeList = elem.getElementsByTagName(formField.getId_flsmsId().trim());

        NodeList repNodeList = elem.getElementsByTagName(formField.getName().trim());
        Node repNode = repNodeList.item(0);
        Element repEelem = (Element) repNode;
        if (repEelem.getFirstChild() == null) {
            return "";
        }
        fieldValue = repEelem.getFirstChild().getNodeValue();
    }
    return fieldValue;
}

From source file:adapter.sos.BasicSensorObservationServiceClient.java

private void examineReponse(HttpEntity entity) throws SOSException {
    try {// ww w.  j  av  a  2s . c o  m
        if (entity != null) {
            String xmlString = EntityUtils.toString(entity);

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(xmlString));
            Document doc = db.parse(inStream);

            String value;

            NodeList nlExc = doc.getElementsByTagName("ows:ExceptionText");
            for (int i = 0; i < nlExc.getLength(); i++) {
                if (nlExc.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                    org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nlExc.item(i);
                    value = nameElement.getFirstChild().getNodeValue().trim();
                    throw new SOSException(value, "Error in response from SOS service.");
                }
            }

            NodeList nl;
            nl = doc.getElementsByTagName("sos:AssignedObservationId");
            for (int i = 0; i < nl.getLength(); i++) {
                if (nl.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                    org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nl.item(i);
                    value = nameElement.getFirstChild().getNodeValue().trim();
                    logger.info("AssignedObservationId [" + value + "]");
                }
            }
        }
    } catch (ParseException | DOMException | ParserConfigurationException | IOException | SAXException e) {
        e.printStackTrace();
    }
}

From source file:com.amalto.core.history.accessor.UnaryFieldAccessor.java

@Override
public String get() {
    Element element = getElement();
    Node textChild = element.getFirstChild();
    if (textChild != null) {
        if (textChild instanceof Text) {
            return element.getTextContent();// get node value can not handle bracket well
        } else {//from   w ww  .j  a v a  2  s .  co m
            return textChild.getNodeValue();
        }
    } else {
        return StringUtils.EMPTY;
    }
}

From source file:com.eucalyptus.objectstorage.pipeline.WalrusSoapUserAuthenticationHandler.java

public void handle(MappingHttpRequest httpRequest, Document doc) throws AuthenticationException {
    NodeList childNodes = doc.getChildNodes();
    Element bodyElem = doc.getDocumentElement();
    Node operationElem = bodyElem.getFirstChild();
    String operationName = operationElem.getNodeName();
    if (operationName.length() > 0) {
        NodeList authNodes = operationElem.getChildNodes();
        String queryId = null, timestamp = null, signature = null;
        for (int i = 0; i < authNodes.getLength(); ++i) {
            Node node = authNodes.item(i);
            if (node.getNodeName().equals(WalrusProperties.RequiredSOAPTags.AWSAccessKeyId.toString())) {
                queryId = node.getFirstChild().getNodeValue().trim();
            } else if (node.getNodeName().equals(WalrusProperties.RequiredSOAPTags.Timestamp.toString())) {
                timestamp = node.getFirstChild().getNodeValue().trim();
            } else if (node.getNodeName().equals(WalrusProperties.RequiredSOAPTags.Signature.toString())) {
                signature = node.getFirstChild().getNodeValue().trim();
            }/* w ww.j  av a  2s  . co  m*/
        }
        if (queryId == null)
            throw new AuthenticationException("Unable to parse access key id");
        if (signature == null)
            throw new AuthenticationException("Unable to parse signature");
        if (timestamp == null)
            throw new AuthenticationException("Unable to parse timestamp");
        //check timestamp
        verifyTimestamp(timestamp);
        String data = "AmazonS3" + operationName + timestamp;
        //check signature
        authenticate(httpRequest, queryId, signature, data);
    } else {
        throw new AuthenticationException("Invalid operation specified");
    }
}

From source file:eionet.cr.util.xml.ConversionsParser.java

/**
 *
 * @param conversionElement/*from  www . ja v  a 2 s  .  c  o  m*/
 */
private void readConversion(Element conversionElement) {

    if (conversionElement != null) {

        boolean isRDFConversion = false;
        NodeList nl = conversionElement.getElementsByTagName("result_type");
        if (nl != null && nl.getLength() > 0) {
            Element element = (Element) nl.item(0);
            Text text = (Text) element.getFirstChild();
            if (text != null) {
                String resultType = text.getData();
                if (resultType != null && resultType.equals("RDF"))
                    isRDFConversion = true;
            }
        }

        if (isRDFConversion && (rdfConversionId == null || rdfConversionId.length() == 0)) {

            nl = conversionElement.getElementsByTagName("convert_id");
            if (nl != null && nl.getLength() > 0) {
                Element element = (Element) nl.item(0);
                Text text = (Text) element.getFirstChild();
                if (text != null) {
                    this.rdfConversionId = text.getData();
                }
            }

            nl = conversionElement.getElementsByTagName("xsl");
            if (nl != null && nl.getLength() > 0) {
                Element element = (Element) nl.item(0);
                Text text = (Text) element.getFirstChild();
                if (text != null) {
                    this.rdfConversionXslFileName = text.getData();
                }
            }
        }
    }
}

From source file:com.sap.research.connectivity.gw.parsers.MetadataXMLParser.java

private void getRelationsOfEntityNode(Element remoteEntityEle) throws Exception {
    NodeList relationNodes = remoteEntityEle.getElementsByTagName("navproperty");
    for (int i = 0; i < relationNodes.getLength(); i++) {
        Element relationNode = (Element) relationNodes.item(i);

        //String relationshipId = getTextValue(relationNode,"relationship_id");
        String relationField = getTextValue(relationNode, "navpath");

        Element relationMultiplicityEnd1Node = getSubNodeByName(relationNode, "end1");
        String relationMultiplicityEnd1 = relationMultiplicityEnd1Node.getFirstChild().getNodeValue();
        String relationMultiplicityEnd1Attr = getNodeAttributeValue(relationMultiplicityEnd1Node,
                "multiplicity");

        Element relationMultiplicityEnd2Node = getSubNodeByName(relationNode, "end2");
        String relationMultiplicityEnd2 = relationMultiplicityEnd2Node.getFirstChild().getNodeValue();
        String relationMultiplicityEnd2Attr = getNodeAttributeValue(relationMultiplicityEnd2Node,
                "multiplicity");

        String[] relationProperties = { relationMultiplicityEnd1, relationMultiplicityEnd1Attr,
                relationMultiplicityEnd2, relationMultiplicityEnd2Attr };
        relationships.put(relationField, relationProperties);
    }/*from w ww  .j  a  va  2 s  .  co  m*/
}

From source file:org.mobicents.charging.server.ratingengine.http.HTTPClientSbb.java

private String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }/*from   w  w  w  .j  av a  2s .c om*/
    return "?";
}