Example usage for org.w3c.dom Element getAttributes

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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:com.espertech.esper.client.ConfigurationParser.java

private static void handleXMLDOM(String name, Configuration configuration, Element xmldomElement) {
    String rootElementName = getRequiredAttribute(xmldomElement, "root-element-name");
    String rootElementNamespace = getOptionalAttribute(xmldomElement, "root-element-namespace");
    String schemaResource = getOptionalAttribute(xmldomElement, "schema-resource");
    String schemaText = getOptionalAttribute(xmldomElement, "schema-text");
    String defaultNamespace = getOptionalAttribute(xmldomElement, "default-namespace");
    String resolvePropertiesAbsoluteStr = getOptionalAttribute(xmldomElement,
            "xpath-resolve-properties-absolute");
    String propertyExprXPathStr = getOptionalAttribute(xmldomElement, "xpath-property-expr");
    String eventSenderChecksRootStr = getOptionalAttribute(xmldomElement, "event-sender-validates-root");
    String xpathFunctionResolverClass = getOptionalAttribute(xmldomElement, "xpath-function-resolver");
    String xpathVariableResolverClass = getOptionalAttribute(xmldomElement, "xpath-variable-resolver");
    String autoFragmentStr = getOptionalAttribute(xmldomElement, "auto-fragment");
    String startTimestampProperty = getOptionalAttribute(xmldomElement, "start-timestamp-property-name");
    String endTimestampProperty = getOptionalAttribute(xmldomElement, "end-timestamp-property-name");

    ConfigurationEventTypeXMLDOM xmlDOMEventTypeDesc = new ConfigurationEventTypeXMLDOM();
    xmlDOMEventTypeDesc.setRootElementName(rootElementName);
    xmlDOMEventTypeDesc.setSchemaResource(schemaResource);
    xmlDOMEventTypeDesc.setSchemaText(schemaText);
    xmlDOMEventTypeDesc.setRootElementNamespace(rootElementNamespace);
    xmlDOMEventTypeDesc.setDefaultNamespace(defaultNamespace);
    xmlDOMEventTypeDesc.setXPathFunctionResolver(xpathFunctionResolverClass);
    xmlDOMEventTypeDesc.setXPathVariableResolver(xpathVariableResolverClass);
    xmlDOMEventTypeDesc.setStartTimestampPropertyName(startTimestampProperty);
    xmlDOMEventTypeDesc.setEndTimestampPropertyName(endTimestampProperty);
    if (resolvePropertiesAbsoluteStr != null) {
        xmlDOMEventTypeDesc/*from  w  w  w  .  ja  v  a2s .co  m*/
                .setXPathResolvePropertiesAbsolute(Boolean.parseBoolean(resolvePropertiesAbsoluteStr));
    }
    if (propertyExprXPathStr != null) {
        xmlDOMEventTypeDesc.setXPathPropertyExpr(Boolean.parseBoolean(propertyExprXPathStr));
    }
    if (eventSenderChecksRootStr != null) {
        xmlDOMEventTypeDesc.setEventSenderValidatesRoot(Boolean.parseBoolean(eventSenderChecksRootStr));
    }
    if (autoFragmentStr != null) {
        xmlDOMEventTypeDesc.setAutoFragment(Boolean.parseBoolean(autoFragmentStr));
    }
    configuration.addEventType(name, xmlDOMEventTypeDesc);

    DOMElementIterator propertyNodeIterator = new DOMElementIterator(xmldomElement.getChildNodes());
    while (propertyNodeIterator.hasNext()) {
        Element propertyElement = propertyNodeIterator.next();
        if (propertyElement.getNodeName().equals("namespace-prefix")) {
            String prefix = getRequiredAttribute(propertyElement, "prefix");
            String namespace = getRequiredAttribute(propertyElement, "namespace");
            xmlDOMEventTypeDesc.addNamespacePrefix(prefix, namespace);
        }
        if (propertyElement.getNodeName().equals("xpath-property")) {
            String propertyName = getRequiredAttribute(propertyElement, "property-name");
            String xPath = getRequiredAttribute(propertyElement, "xpath");
            String propertyType = getRequiredAttribute(propertyElement, "type");
            QName xpathConstantType;
            if (propertyType.toUpperCase().equals("NUMBER")) {
                xpathConstantType = XPathConstants.NUMBER;
            } else if (propertyType.toUpperCase().equals("STRING")) {
                xpathConstantType = XPathConstants.STRING;
            } else if (propertyType.toUpperCase().equals("BOOLEAN")) {
                xpathConstantType = XPathConstants.BOOLEAN;
            } else if (propertyType.toUpperCase().equals("NODE")) {
                xpathConstantType = XPathConstants.NODE;
            } else if (propertyType.toUpperCase().equals("NODESET")) {
                xpathConstantType = XPathConstants.NODESET;
            } else {
                throw new IllegalArgumentException("Invalid xpath property type for property '" + propertyName
                        + "' and type '" + propertyType + '\'');
            }

            String castToClass = null;
            if (propertyElement.getAttributes().getNamedItem("cast") != null) {
                castToClass = propertyElement.getAttributes().getNamedItem("cast").getTextContent();
            }

            String optionaleventTypeName = null;
            if (propertyElement.getAttributes().getNamedItem("event-type-name") != null) {
                optionaleventTypeName = propertyElement.getAttributes().getNamedItem("event-type-name")
                        .getTextContent();
            }

            if (optionaleventTypeName != null) {
                xmlDOMEventTypeDesc.addXPathPropertyFragment(propertyName, xPath, xpathConstantType,
                        optionaleventTypeName);
            } else {
                xmlDOMEventTypeDesc.addXPathProperty(propertyName, xPath, xpathConstantType, castToClass);
            }
        }
    }
}

From source file:org.sakaiproject.iclicker.logic.IClickerLogic.java

/**
 * Attempt to decode the XML into readable values in a map
 *
 * @param xml XML/*from w  w w. j  av a  2  s  . c om*/
 * @return the map of attributes from S student record
 * @throws IllegalArgumentException if the xml is invalid or blank
 * @throws RuntimeException if there is an internal failure in the XML parser
 */
public Map<String, String> decodeGetRegisteredForClickerMACResult(String xml) {
    /*
    <StudentEnrol>
    <S StudentId="testgoqait99" FirstName="testgoqait99" LastName="testgoqait99" MiddleName="" WebClickerId="C570BF0C2154"/>
    </StudentEnrol>
     */
    if (xml == null || "".equals(xml)) {
        throw new IllegalArgumentException("xml must be set");
    }
    // read the xml (try to anyway)
    DocumentBuilder db;
    try {
        db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        throw new RuntimeException("XML parser failure: " + e, e);
    }
    Document doc;
    try {
        doc = db.parse(new ByteArrayInputStream(xml.getBytes()));
    } catch (SAXException e) {
        e.printStackTrace();
        throw new RuntimeException("XML read failure: " + e, e);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("XML IO failure: " + e, e);
    }
    HashMap<String, String> m = new HashMap<String, String>();
    try {
        doc.getDocumentElement().normalize();
        NodeList users = doc.getElementsByTagName("S");
        if (users.getLength() == 0) {
            throw new IllegalArgumentException("Invalid XML, no S element");
        }
        Node userNode = users.item(0);
        if (userNode.getNodeType() == Node.ELEMENT_NODE) {
            Element user = (Element) userNode;
            NamedNodeMap attributes = user.getAttributes();
            for (int j = 0; j < attributes.getLength(); j++) {
                String name = attributes.item(j).getNodeName();
                String value = attributes.item(j).getNodeValue();
                m.put(name, value);
            }
        } else {
            throw new IllegalArgumentException("Invalid user node in XML: " + userNode);
        }
    } catch (DOMException e) {
        e.printStackTrace();
        throw new RuntimeException("XML DOM parsing failure: " + e, e);
    }
    return m;
}

From source file:org.solmix.runtime.support.spring.AbstractRootBeanDefinitionParser.java

protected void parseAttributes(Element element, ParserContext parserContext, String id,
        RootBeanDefinition beanDefinition) {
    Set<String> props = new HashSet<String>();
    ManagedMap<String, Object> parameters = null;
    for (Method setter : this.type.getMethods()) {
        String name = setter.getName();
        if (name.length() > 3 && name.startsWith("set") && Modifier.isPublic(setter.getModifiers())
                && setter.getParameterTypes().length == 1) {
            Class<?> type = setter.getParameterTypes()[0];
            String property = StringUtils
                    .camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
            props.add(property);//from w  ww . ja va2 s.co m
            Method getter = null;
            try {
                getter = this.type.getMethod("get" + name.substring(3), new Class<?>[0]);
            } catch (NoSuchMethodException e) {
                try {
                    getter = this.type.getMethod("is" + name.substring(3), new Class<?>[0]);
                } catch (NoSuchMethodException e2) {
                }
            }
            if (getter == null || !Modifier.isPublic(getter.getModifiers())
                    || !type.equals(getter.getReturnType())) {
                continue;
            }
            if ("properties".equals(property)) {
                parameters = parseProperties(element.getChildNodes(), beanDefinition);
            } else if ("arguments".equals(property)) {
                parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
            } else {
                String value = element.getAttribute(property);
                if (value != null && value.trim().length() > 0) {
                    value = value.trim();
                    parserValue(beanDefinition, property, type, value, parserContext);
                }
            }
        }
    }
    NamedNodeMap attributes = element.getAttributes();
    int len = attributes.getLength();
    for (int i = 0; i < len; i++) {
        Node node = attributes.item(i);
        String name = node.getLocalName();
        if (!props.contains(name)) {
            if (parameters == null) {
                parameters = new ManagedMap<String, Object>();
            }
            String value = node.getNodeValue();
            parameters.put(name, new TypedStringValue(value, String.class));
        }
    }
    if (parameters != null) {
        beanDefinition.getPropertyValues().addPropertyValue("properties", parameters);
    }
}

From source file:de.escidoc.core.test.EscidocRestSoapTestBase.java

/**
 * Creates a new element node for the provided document. The created element is an element that refers to another
 * resource, i.e. it has xlink attributes in case of REST or objid attribute in case of SOAP.<br/> This method takes
 * the provided transport definition to decide which attributes have to be sent.
 *
 * @param transport        Specifies the transport, must be one of {@link Constants.TRANSPORT_REST} or {@link
 *                         Constants.TRANSPORT_SOAP}.
 * @param doc              The document for that the node shall be created.
 * @param namespaceUri     The name space uri of the node to create. This may be null.
 * @param prefix           The prefix to use.
 * @param tagName          The tag name of the node.
 * @param xlinkPrefix      The prefix to use for the xlink attributes.
 * @param title            The title of the referencing element (=xlink:title)
 * @param href             The href of the referencing element (=xlink:href). The objid attribute value is extracted
 *                         from this href.
 * @param withRestReadOnly Flag indicating if the parent-ous element shall contain the REST specific read only
 *                         attributes.//ww  w  .  j  a va2s .com
 * @return Returns the created node.
 * @throws Exception Thrown if anything fails.
 */
public static Element createReferencingElementNode(final int transport, final Document doc,
        final String namespaceUri, final String prefix, final String tagName, final String xlinkPrefix,
        final String title, final String href, final boolean withRestReadOnly) throws Exception {

    assertTransport(transport);

    Element newElement;
    if (transport == Constants.TRANSPORT_REST) {
        newElement = createElementNodeWithXlink(doc, namespaceUri, prefix, tagName, xlinkPrefix, title, href,
                withRestReadOnly);
    } else {
        newElement = createElementNode(doc, namespaceUri, prefix, tagName, null);
        Attr objidAttr = createAttributeNode(doc, null, null, NAME_OBJID, getObjidFromHref(href));
        newElement.getAttributes().setNamedItemNS(objidAttr);
    }

    return newElement;
}

From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java

private synchronized String duplicate(final Document originalDom, final Element originalRootElement,
        final Element patchElement) throws Exception {

    boolean isdone = false;
    Element parentElement = null;

    DuplicateChildElementObject childElementObject = isChildElement(originalRootElement, patchElement);
    if (!childElementObject.isNeedDuplicate()) {
        isdone = true;/*from  w ww. j  a v a2  s.co m*/
        parentElement = childElementObject.getElement();
    } else if (childElementObject.getElement() != null) {
        parentElement = childElementObject.getElement();
    } else {
        parentElement = originalRootElement;
    }

    String son_name = patchElement.getNodeName();

    Element subITEM = null;
    if (!isdone) {
        subITEM = originalDom.createElement(son_name);

        if (patchElement.hasChildNodes()) {
            if (patchElement.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                subITEM.setTextContent(patchElement.getTextContent());

            }
        }

        if (patchElement.hasAttributes()) {
            NamedNodeMap attributes = patchElement.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                String attribute_name = attributes.item(i).getNodeName();
                String attribute_value = attributes.item(i).getNodeValue();
                subITEM.setAttribute(attribute_name, attribute_value);
            }
        }
        parentElement.appendChild(subITEM);
    } else {
        subITEM = parentElement;
    }

    NodeList sub_messageItems = patchElement.getChildNodes();
    int sub_item_number = sub_messageItems.getLength();
    logger.debug("patchEl: " + DomUtils.elementToString(patchElement) + "length: " + sub_item_number);
    if (sub_item_number == 0) {
        isdone = true;
    } else {
        for (int j = 0; j < sub_item_number; j++) {
            if (sub_messageItems.item(j).getNodeType() == Node.ELEMENT_NODE) {
                Element sub_messageItem = (Element) sub_messageItems.item(j);
                logger.debug("node name: " + DomUtils.elementToString(subITEM) + " node type: "
                        + subITEM.getNodeType());
                duplicate(originalDom, subITEM, sub_messageItem);
            }

        }
    }

    return (parentElement != null) ? DomUtils.elementToString(parentElement) : "";
}

From source file:lcmc.data.VMSXML.java

/** Modify xml of some device element. */
private void modifyXML(final Node domainNode, final String domainName, final Map<String, String> tagMap,
        final Map<String, String> attributeMap, final Map<String, String> parametersMap, final String path,
        final String elementName, final VirtualHardwareComparator vhc) {
    final String configName = namesConfigsMap.get(domainName);
    if (configName == null) {
        return;// w w  w.j  ava  2s  .  c o m
    }
    //final Node domainNode = getDomainNode(domainName);
    if (domainNode == null) {
        return;
    }
    final XPath xpath = XPathFactory.newInstance().newXPath();
    final Node devicesNode = getDevicesNode(xpath, domainNode);
    if (devicesNode == null) {
        return;
    }
    try {
        final NodeList nodes = (NodeList) xpath.evaluate(path, domainNode, XPathConstants.NODESET);
        Element hwNode = vhc.getElement(nodes, parametersMap);
        if (hwNode == null) {
            hwNode = (Element) devicesNode
                    .appendChild(domainNode.getOwnerDocument().createElement(elementName));
        }
        for (final String param : parametersMap.keySet()) {
            final String value = parametersMap.get(param);
            if (!tagMap.containsKey(param) && attributeMap.containsKey(param)) {
                /* attribute */
                final Node attributeNode = hwNode.getAttributes().getNamedItem(attributeMap.get(param));
                if (attributeNode == null) {
                    if (value != null && !"".equals(value)) {
                        hwNode.setAttribute(attributeMap.get(param), value);
                    }
                } else if (value == null || "".equals(value)) {
                    hwNode.removeAttribute(attributeMap.get(param));
                } else {
                    attributeNode.setNodeValue(value);
                }
                continue;
            }
            Element node = (Element) getChildNode(hwNode, tagMap.get(param));
            if ((attributeMap.containsKey(param) || "True".equals(value)) && node == null) {
                node = (Element) hwNode
                        .appendChild(domainNode.getOwnerDocument().createElement(tagMap.get(param)));
            } else if (node != null && !attributeMap.containsKey(param)
                    && (value == null || "".equals(value))) {
                hwNode.removeChild(node);
            }
            if (attributeMap.containsKey(param)) {
                final Node attributeNode = node.getAttributes().getNamedItem(attributeMap.get(param));
                if (attributeNode == null) {
                    if (value != null && !"".equals(value)) {
                        node.setAttribute(attributeMap.get(param), value);
                    }
                } else {
                    if (value == null || "".equals(value)) {
                        node.removeAttribute(attributeMap.get(param));
                    } else {
                        attributeNode.setNodeValue(value);
                    }
                }
            }
        }
        final Element hwAddressNode = (Element) getChildNode(hwNode, HW_ADDRESS);
        if (hwAddressNode != null) {
            hwNode.removeChild(hwAddressNode);
        }
    } catch (final javax.xml.xpath.XPathExpressionException e) {
        Tools.appError("could not evaluate: ", e);
        return;
    }
}

From source file:loci.formats.in.LIFReader.java

private void populateOriginalMetadata(Element root, Deque<String> nameStack) {
    String name = root.getNodeName();
    if (root.hasAttributes() && !name.equals("Element") && !name.equals("Attachment")
            && !name.equals("LMSDataContainerHeader")) {
        nameStack.push(name);/*  w  w w  .  j a  v a 2  s.  c om*/

        String suffix = root.getAttribute("Identifier");
        String value = root.getAttribute("Variant");
        if (suffix == null || suffix.trim().length() == 0) {
            suffix = root.getAttribute("Description");
        }
        StringBuffer key = new StringBuffer();
        final Iterator<String> nameStackIterator = nameStack.descendingIterator();
        while (nameStackIterator.hasNext()) {
            final String k = nameStackIterator.next();
            key.append(k);
            key.append("|");
        }
        if (suffix != null && value != null && suffix.length() > 0 && value.length() > 0
                && !suffix.equals("HighInteger") && !suffix.equals("LowInteger")) {
            addSeriesMetaList(key.toString() + suffix, value);
        } else {
            NamedNodeMap attributes = root.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                Attr attr = (Attr) attributes.item(i);
                if (!attr.getName().equals("HighInteger") && !attr.getName().equals("LowInteger")) {
                    addSeriesMeta(key.toString() + attr.getName(), attr.getValue());
                }
            }
        }
    }

    NodeList children = root.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Object child = children.item(i);
        if (child instanceof Element) {
            populateOriginalMetadata((Element) child, nameStack);
        }
    }

    if (root.hasAttributes() && !name.equals("Element") && !name.equals("Attachment")
            && !name.equals("LMSDataContainerHeader")) {
        nameStack.pop();
    }
}

From source file:com.twinsoft.convertigo.engine.translators.WebServiceTranslator.java

private SOAPElement addSoapElement(Context context, SOAPEnvelope se, SOAPElement soapParent, Node node)
        throws Exception {
    String prefix = node.getPrefix();
    String namespace = node.getNamespaceURI();
    String nodeName = node.getNodeName();
    String localName = node.getLocalName();
    String value = node.getNodeValue();

    boolean includeResponseElement = true;
    if (context.sequenceName != null) {
        includeResponseElement = ((Sequence) context.requestedObject).isIncludeResponseElement();
    }/*from   w w w . j  a  va2s .  c  o m*/

    SOAPElement soapElement = null;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;

        boolean toAdd = true;
        if (!includeResponseElement && "response".equalsIgnoreCase(localName)) {
            toAdd = false;
        }
        if ("http://schemas.xmlsoap.org/soap/envelope/".equals(element.getParentNode().getNamespaceURI())
                || "http://schemas.xmlsoap.org/soap/envelope/".equals(namespace)
                || nodeName.toLowerCase().endsWith(":envelope") || nodeName.toLowerCase().endsWith(":body")
        //element.getParentNode().getNodeName().toUpperCase().indexOf("NS0:") != -1 ||
        //nodeName.toUpperCase().indexOf("NS0:") != -1
        ) {
            toAdd = false;
        }

        if (toAdd) {
            if (prefix == null || prefix.equals("")) {
                soapElement = soapParent.addChildElement(nodeName);
            } else {
                soapElement = soapParent.addChildElement(se.createName(localName, prefix, namespace));
            }
        } else {
            soapElement = soapParent;
        }

        if (soapElement != null) {
            if (soapParent.equals(se.getBody()) && !soapParent.equals(soapElement)) {
                if (XsdForm.qualified == context.project.getSchemaElementForm()) {
                    if (soapElement.getAttribute("xmlns") == null) {
                        soapElement.addAttribute(se.createName("xmlns"), context.project.getTargetNamespace());
                    }
                }
            }

            if (element.hasAttributes()) {
                String attrType = element.getAttribute("type");
                if (("attachment").equals(attrType)) {
                    if (context.requestedObject instanceof AbstractHttpTransaction) {
                        AttachmentDetails attachment = AttachmentManager.getAttachment(element);
                        if (attachment != null) {
                            byte[] raw = attachment.getData();
                            if (raw != null)
                                soapElement.addTextNode(Base64.encodeBase64String(raw));
                        }

                        /* DON'T WORK YET *\
                        AttachmentPart ap = responseMessage.createAttachmentPart(new ByteArrayInputStream(raw), element.getAttribute("content-type"));
                        ap.setContentId(key);
                        ap.setContentLocation(element.getAttribute("url"));
                        responseMessage.addAttachmentPart(ap);
                        \* DON'T WORK YET */
                    }
                }

                if (!includeResponseElement && "response".equalsIgnoreCase(localName)) {
                    // do not add attributes
                } else {
                    NamedNodeMap attributes = element.getAttributes();
                    int len = attributes.getLength();
                    for (int i = 0; i < len; i++) {
                        Node item = attributes.item(i);
                        addSoapElement(context, se, soapElement, item);
                    }
                }
            }

            if (element.hasChildNodes()) {
                NodeList childNodes = element.getChildNodes();
                int len = childNodes.getLength();
                for (int i = 0; i < len; i++) {
                    Node item = childNodes.item(i);
                    switch (item.getNodeType()) {
                    case Node.ELEMENT_NODE:
                        addSoapElement(context, se, soapElement, item);
                        break;
                    case Node.CDATA_SECTION_NODE:
                    case Node.TEXT_NODE:
                        String text = item.getNodeValue();
                        text = (text == null) ? "" : text;
                        soapElement.addTextNode(text);
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        if (prefix == null || prefix.equals("")) {
            soapElement = soapParent.addAttribute(se.createName(nodeName), value);
        } else {
            soapElement = soapParent.addAttribute(se.createName(localName, prefix, namespace), value);
        }
    }
    return soapElement;
}

From source file:de.escidoc.core.test.EscidocTestBase.java

/**
 * Creates a new element node for the provided document. The created element is an element that that has xlink
 * attributes, but does not have an objid attribute.
 * /* w w  w  .j  a  v a2  s  .  c  o  m*/
 * @param doc
 *            The document for that the node shall be created.
 * @param namespaceUri
 *            The name space uri of the node to create. This may be null.
 * @param prefix
 *            The prefix to use.
 * @param tagName
 *            The tag name of the node.
 * @param xlinkPrefix
 *            The prefix to use for the xlink attributes.
 * @param title
 *            The title of the referencing element (=xlink:title)
 * @param href
 *            The href of the referencing element (=xlink:href). The objid attribute value is extracted from this
 *            href.
 * @return Returns the created node.
 * @throws Exception
 *             Thrown if anything fails.
 */
public static Element createElementNodeWithXlink(final Document doc, final String namespaceUri,
        final String prefix, final String tagName, final String xlinkPrefix, final String title,
        final String href) throws Exception {

    Element newElement = createElementNode(doc, namespaceUri, prefix, tagName, null);
    Attr xlinkTypeAttr = createAttributeNode(doc, XLINK_NS_URI, xlinkPrefix, NAME_TYPE, "simple");
    Attr xlinkTitleAttr = createAttributeNode(doc, XLINK_NS_URI, xlinkPrefix, NAME_TITLE, title);
    Attr xlinkHrefAttr = createAttributeNode(doc, XLINK_NS_URI, xlinkPrefix, NAME_HREF, href);
    newElement.getAttributes().setNamedItemNS(xlinkTypeAttr);
    newElement.getAttributes().setNamedItemNS(xlinkTitleAttr);
    newElement.getAttributes().setNamedItemNS(xlinkHrefAttr);

    return newElement;
}

From source file:de.escidoc.core.test.EscidocTestBase.java

/**
 * Creates a new element node for the provided document. The created element is an element that refers to another
 * resource, i.e. it has xlink attributes and an objid attribute.
 * /* w  w w  .ja v  a 2 s .  c o  m*/
 * @param doc
 *            The document for that the node shall be created.
 * @param namespaceUri
 *            The name space uri of the node to create. This may be null.
 * @param prefix
 *            The prefix to use.
 * @param tagName
 *            The tag name of the node.
 * @param xlinkPrefix
 *            The prefix to use for the xlink attributes.
 * @param title
 *            The title of the referencing element (=xlink:title)
 * @param href
 *            The href of the referencing element (=xlink:href). The objid attribute value is extracted from this
 *            href.
 * @return Returns the created node.
 * @throws Exception
 *             Thrown if anything fails.
 */
public Element createReferencingElementNode(final Document doc, final String namespaceUri, final String prefix,
        final String tagName, final String xlinkPrefix, final String title, final String href)
        throws Exception {

    Element newElement = createElementNodeWithXlink(doc, namespaceUri, prefix, tagName, xlinkPrefix, title,
            href);

    Attr objidAttr = createAttributeNode(doc, null, null, NAME_OBJID, getObjidFromHref(href));
    newElement.getAttributes().setNamedItemNS(objidAttr);

    return newElement;
}