Example usage for org.w3c.dom Attr getTextContent

List of usage examples for org.w3c.dom Attr getTextContent

Introduction

In this page you can find the example usage for org.w3c.dom Attr getTextContent.

Prototype

public String getTextContent() throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:Main.java

public static String getNamespaceUriDeclaration(Element ele) {
    NamedNodeMap attribs = ele.getAttributes();

    for (int i = 0; i < attribs.getLength(); i++) {
        Attr attr = (Attr) attribs.item(i);
        if ("xmlns".equals(attr.getLocalName()) || XMLConstants.XML_NS_URI.equals(attr.getNamespaceURI())) {
            return attr.getTextContent();
        }//  www  .  j  ava  2 s .  co m
    }

    return "";
}

From source file:Main.java

/**
 * @return A String[] of length two, [prefix, URI].
 *///from   w w  w.jav  a2  s .  c  o  m
public static String[] getNamespaceDeclaration(Element ele, String prefixHint) {
    String[] ns = new String[2]; // prefix, URI
    NamedNodeMap attribs = ele.getAttributes();

    for (int i = 0; i < attribs.getLength(); i++) {
        Attr attr = (Attr) attribs.item(i);
        if (attr.getName().startsWith("xmlns")) {
            if ((prefixHint != null && attr.getName().endsWith(prefixHint)) || attr.getName().equals("xmlns")) {
                ns[0] = attr.getLocalName(); // prefix
                ns[1] = attr.getTextContent(); // URI

                // catch default namespace change
                if (ns[0] == "xmlns") {
                    ns[0] = UUID.randomUUID().toString();
                }
            }
        }
    }

    if (ns[1] == null) {
        return getNamespaceDeclaration((Element) ele.getParentNode(), prefixHint);
    } else {
        return ns;
    }
}

From source file:Main.java

static boolean canBeMerged(Node node1, Node node2, String requiredTagName) {
    if (node1.getNodeType() != Node.ELEMENT_NODE || node2.getNodeType() != Node.ELEMENT_NODE)
        return false;

    Element element1 = (Element) node1;
    Element element2 = (Element) node2;

    if (!equals(requiredTagName, element1.getTagName()) || !equals(requiredTagName, element2.getTagName()))
        return false;

    NamedNodeMap attributes1 = element1.getAttributes();
    NamedNodeMap attributes2 = element2.getAttributes();

    if (attributes1.getLength() != attributes2.getLength())
        return false;

    for (int i = 0; i < attributes1.getLength(); i++) {
        final Attr attr1 = (Attr) attributes1.item(i);
        final Attr attr2;
        if (isNotEmpty(attr1.getNamespaceURI()))
            attr2 = (Attr) attributes2.getNamedItemNS(attr1.getNamespaceURI(), attr1.getLocalName());
        else/*  w  w w . j a v  a  2 s .c  o m*/
            attr2 = (Attr) attributes2.getNamedItem(attr1.getName());

        if (attr2 == null || !equals(attr1.getTextContent(), attr2.getTextContent()))
            return false;
    }

    return true;
}

From source file:com.evolveum.midpoint.prism.parser.DomParser.java

private static <T> T parsePrimitiveAttrValue(Attr attr, QName typeName) throws SchemaException {
    if (DOMUtil.XSD_QNAME.equals(typeName)) {
        return (T) DOMUtil.getQNameValue(attr);
    }//  ww  w . j a  v a  2  s . c o m
    if (XmlTypeConverter.canConvert(typeName)) {
        String stringValue = attr.getTextContent();
        return XmlTypeConverter.toJavaValue(stringValue, typeName);
    } else {
        throw new SchemaException("Cannot convert attribute '" + attr + "' to " + typeName);
    }
}

From source file:com.evolveum.midpoint.prism.lex.dom.DomLexicalProcessor.java

private static <T> T parsePrimitiveAttrValue(Attr attr, QName typeName, XNodeProcessorEvaluationMode mode)
        throws SchemaException {
    if (DOMUtil.XSD_QNAME.equals(typeName)) {
        try {//from  w w w.ja va 2  s. co  m
            return (T) DOMUtil.getQNameValue(attr);
        } catch (IllegalArgumentException e) {
            return processIllegalArgumentException(attr.getTextContent(), typeName, e, mode); // primitive way of ensuring compatibility mode
        }
    }
    if (XmlTypeConverter.canConvert(typeName)) {
        String stringValue = attr.getTextContent();
        try {
            return XmlTypeConverter.toJavaValue(stringValue, typeName);
        } catch (IllegalArgumentException e) {
            return processIllegalArgumentException(attr.getTextContent(), typeName, e, mode); // primitive way of ensuring compatibility mode
        }
    } else {
        throw new SchemaException("Cannot convert attribute '" + attr + "' to " + typeName);
    }
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityXmlParser.java

/**
 * Resolves DOM node contained textual data and formats it using provided locator.
 *
 * @param locator//from   w ww  .  j a va2 s. c  o m
 *            locator instance to alter using XML attributes contained data type, format and units used to format
 *            resolved value
 * @param node
 *            DOM node to collect textual data
 * @return resolved textual value formatted based on the locator's formatting properties
 * @throws ParseException
 *             if exception occurs applying locator format properties to specified value
 */
protected static Object getTextContent(ActivityFieldLocator locator, Node node) throws ParseException {
    String strValue = node.getTextContent();
    Node attrsNode = node;

    if (node instanceof Attr) {
        Attr attr = (Attr) node;

        attrsNode = attr.getOwnerElement();
    }

    // Get list of attributes and their values for
    // current element
    NamedNodeMap attrsMap = attrsNode == null ? null : attrsNode.getAttributes();

    Node attr;
    String attrVal;
    ActivityFieldLocator locCopy = locator.clone();
    if (attrsMap != null && attrsMap.getLength() > 0) {
        attr = attrsMap.getNamedItem(DATA_TYPE_ATTR);
        attrVal = attr == null ? null : attr.getTextContent();
        if (StringUtils.isNotEmpty(attrVal)) {
            locCopy.setDataType(ActivityFieldDataType.valueOf(attrVal));
        }

        attr = attrsMap.getNamedItem(FORMAT_ATTR);
        attrVal = attr == null ? null : attr.getTextContent();
        if (StringUtils.isNotEmpty(attrVal)) {
            attr = attrsMap.getNamedItem(LOCALE_ATTR);
            String attrLVal = attr == null ? null : attr.getTextContent();

            locCopy.setFormat(attrVal, StringUtils.isEmpty(attrLVal) ? locator.getLocale() : attrLVal);
        }

        attr = attrsMap.getNamedItem(UNITS_ATTR);
        attrVal = attr == null ? null : attr.getTextContent();
        if (StringUtils.isNotEmpty(attrVal)) {
            locCopy.setUnits(attrVal);
        }
    }

    return locCopy.formatValue(strValue.trim());
}

From source file:org.opencredo.esper.integration.config.xml.InboundChannelAdapterParser.java

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    String channelRef = element.getAttribute("channel");
    Assert.hasText(channelRef, "channel attribute must be provided");
    builder.addConstructorArgReference(channelRef);

    Element eplElements = DomUtils.getChildElementByTagName(element, "epl");

    if (eplElements != null) {
        builder.addConstructorArgValue(eplElements.getTextContent());
    } else {//from  w  ww.j  a  v a2  s .  co  m
        builder.addConstructorArgValue(null);
    }

    Attr templateNameNode = element.getAttributeNode("template-name");
    if (templateNameNode != null) {
        builder.addPropertyValue("templateName", templateNameNode.getTextContent());
    }
}

From source file:edu.cornell.mannlib.semservices.service.impl.LCSHService.java

protected List<String> getConceptURIFromXML(String rdf) {
    List<String> uris = new ArrayList<String>();
    String conceptUri = new String();
    try {/*from  w  w  w . ja va2 s  . c o  m*/
        Document doc = XMLUtils.parse(rdf);
        NodeList nodes = doc.getElementsByTagName("search:result");
        int len = nodes.getLength();
        int i;
        for (i = 0; i < len; i++) {
            Node node = nodes.item(i);
            NamedNodeMap attrs = node.getAttributes();
            Attr idAttr = (Attr) attrs.getNamedItem("uri");
            conceptUri = idAttr.getTextContent();
            log.debug("concept uri is " + conceptUri);
            uris.add(conceptUri);
        }

    } catch (IOException e) {
        log.error("error occurred in parsing " + rdf, e);
    } catch (SAXException e) {
        log.error("error occurred in parsing " + rdf, e);
    } catch (ParserConfigurationException e) {
        log.error("error occurred in parsing " + rdf, e);

    }
    return uris;

}

From source file:com.evolveum.midpoint.util.DOMUtil.java

public static QName getQNameValue(Attr attr) {
    return resolveQName(attr, attr.getTextContent());
}

From source file:edu.cornell.mannlib.semservices.service.impl.AgrovocService.java

protected String getConceptURIFromRDF(String rdf) {
    String conceptUri = new String();
    try {/*from   ww  w  .j a  va2 s  .c  o  m*/
        Document doc = XMLUtils.parse(rdf);
        NodeList nodes = doc.getElementsByTagName("skos:Concept");
        Node node = nodes.item(0);

        NamedNodeMap attrs = node.getAttributes();
        Attr idAttr = (Attr) attrs.getNamedItem("rdf:about");
        conceptUri = idAttr.getTextContent();
    } catch (IOException e) {
        e.printStackTrace();
        System.err.println("rdf: " + rdf);
    } catch (SAXException e) {
        e.printStackTrace();
        System.err.println("rdf: " + rdf);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        System.err.println("rdf: " + rdf);
    }
    return conceptUri;

}