Example usage for org.w3c.dom Element setAttribute

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

Introduction

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

Prototype

public void setAttribute(String name, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:Main.java

/**
 * adds an Element with two attributs to a DOM-tree.
 * //from  w w  w.  j  ava 2s.  c  o  m
 * @param document
 *            Document: the DOM-tree to add to
 * @param father
 *            Element: the new element will be inserted directly under this
 *            Element in the tree
 * @param name
 *            String: the name of the new element
 * @param attOneName
 *            String: the name of the 1st attribut
 * @param attOneValue
 *            String: the value of the 1st attribut
 * @param attTwoName
 *            String: the name of the 2nd attribut
 * @param attTwoValue
 *            String: the value of the 2nd attribut
 */
public static void addElement(Document document, Element father, String name, String attOneName,
        String attOneValue, String attTwoName, String attTwoValue) {
    Element element = document.createElement(name);
    element.setAttribute(attOneName, attOneValue);
    element.setAttribute(attTwoName, attTwoValue);
    father.appendChild(element);
}

From source file:de.betterform.connector.file.FileURIResolver.java

/**
 * Returns a plain file listing as a document.
 *
 * @param directory the directory to list.
 * @return a plain file listing as a document.
 *//* ww w .j  a  v  a 2 s .co m*/
public static Document buildDirectoryListing(File directory) {
    Document dirList = DOMUtil.newDocument(false, false);
    Element root = dirList.createElement("dir");
    root.setAttribute("path", directory.toURI().toString());
    root.setAttribute("parentDir", directory.getParentFile().toURI().toString());

    File[] fileList = directory.listFiles();
    File file;
    Element element;
    for (int i = 0; i < fileList.length; i++) {
        file = fileList[i];

        if (file.isDirectory()) {
            element = dirList.createElement("dir");
        } else {
            element = dirList.createElement("file");
        }

        element.setAttribute("name", file.getName());
        element.setAttribute("path", file.toURI().toString());
        root.appendChild(element);
    }

    dirList.appendChild(root);
    return dirList;
}

From source file:Main.java

/**
 * Creates an XIInclude element with a link to a file
 *
 * @param doc  The DOM Document to create the xi:include for.
 * @param file The file name/path to link to.
 * @return An xi:include element that can be used to include content from another file.
 *///w  ww  .  j  a  va2  s.c om
public static Element createXIInclude(final Document doc, final String file) {
    try {
        // Encode the file reference
        final StringBuilder fixedFileRef = new StringBuilder();
        final String[] split = file.split("/");
        for (int i = 0; i < split.length; i++) {
            if (i != 0) {
                fixedFileRef.append("/");
            }

            final String uriPart = split[i];
            if (uriPart.isEmpty() || uriPart.equals("..") || uriPart.equals(".")) {
                fixedFileRef.append(uriPart);
            } else {
                fixedFileRef.append(URLEncoder.encode(uriPart, "UTF-8"));
            }
        }

        // If the file ref ended with "/" then it wouldn't have been added as their was no content after it
        if (file.endsWith("/")) {
            fixedFileRef.append("/");
        }

        final Element xiInclude = doc.createElementNS("http://www.w3.org/2001/XInclude", "xi:include");
        xiInclude.setAttribute("href", fixedFileRef.toString());
        xiInclude.setAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");

        return xiInclude;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static synchronized void setAttribute(Element e, String name, String value) {
    if (e == null || name == null || name.length() == 0 || value == null || value.length() == 0) {
        return;//from w  w  w  .  j a v  a 2s  .com
    } else {
        e.setAttribute(name, value);
    }
}

From source file:Main.java

/**
 * Given a certain parent {@link Element} and {@link Document}, append a
 * child {@link Element} with Tag Name (which cannot be null), Node
 * Attribute Name, Node Attribute Value (which both are null at the same
 * time or none is null at all), Node Value (which can be null).
 * /*from  w ww .  j ava 2 s .co  m*/
 * @param nodeTagName
 *            Node Tag Name.
 * @param nodeAttributeName
 *            Node Attribute Name.
 * @param nodeAttributeValue
 *            Node Attribute Value.
 * @param nodeValue
 *            Node Value.
 * @param elementToBeApppendedTo
 *            Parent {@link Element} which the new created {@link Element}
 *            will be appended to.
 * @param document
 *            {@link Document} which everything belongs to.
 * 
 * @return Returns the created {@link Element}.
 */
private static Element appendNewElementToNode(String nodeTagName, String nodeAttributeName,
        String nodeAttributeValue, String nodeValue, Element elementToBeApppendedTo, Document document) {
    // create element and append it
    Element element = document.createElement(nodeTagName);
    if ((nodeAttributeName != null) && (nodeAttributeValue != null)) {
        element.setAttribute(nodeAttributeName, nodeAttributeValue);
    }
    if (nodeValue != null) {
        element.setTextContent(nodeValue);
    }
    elementToBeApppendedTo.appendChild(element);

    return element;
}

From source file:Main.java

/**
 * Sets a named attribute of an Element, or removes the attribute if the value is null;
 * @param element The element to search//from www .jav  a 2s  . c o  m
 * @param attr The name of the attribute to set or remove
 * @param value The value to assign, or NULL to remove
 */
public synchronized static void setOrRemoveAttribute(Element element, String attr, String value) {
    if (value == null) {
        element.removeAttribute(attr);
    } else {
        element.setAttribute(attr, value);
    }
}

From source file:Main.java

public static Element createElement(String name, HashMap<String, Object> attributes) {

    Element e = document.createElement(name);

    for (String a : attributes.keySet())
        if (attributes.get(a) instanceof String)
            e.setAttribute(a, (String) attributes.get(a));

    return e;//www. j av a 2 s.c om
}

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

private static Element buildAttribute(Document doc, String attributeName, String attributeValue) {
    Element attribute = doc.createElement(ENTRY_ATTRIBUTE_NODE);
    attribute.setAttribute(ENTRY_QUALIFIEDNAME, attributeName);
    Element value = doc.createElement(ENTRY_ATTRIBUTE_VALUE);
    value.setTextContent(attributeValue);
    attribute.appendChild(value);//  www .ja v  a 2s  . co  m
    return attribute;
}

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

private static Element buildAttributeCollection(Document doc, String localName, Collection<?> values) {
    Element attribute = doc.createElement(ENTRY_ATTRIBUTE_NODE);
    attribute.setAttribute(ENTRY_QUALIFIEDNAME, localName);
    for (Object value : values) {
        Element valueElement = doc.createElement(ENTRY_ATTRIBUTE_VALUE);
        valueElement.setTextContent(value.toString());
        attribute.appendChild(valueElement);
    }/*from   ww w  .jav  a2 s .  co m*/
    return attribute;
}

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

public static Document buildEntry(QName type, String objectId, Map<QName, Serializable> properties,
        List<AssociationBean> list) {
    // Document entry = new Document();
    DocumentBuilder documentBuilder = null;
    try {/* w  ww . j a  va 2s .  com*/
        documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        throw new RuntimeException("Couldn't create a document builder.");
    }
    Document doc = documentBuilder.newDocument();

    Element root = doc.createElement(ENTRY_ROOTNODE);
    root.setAttribute(ENTRY_QUALIFIEDNAME, type.getLocalName());
    root.setAttribute(ENTRY_ID, objectId);
    Element attributesE = buildAttributes(doc, properties);
    Element associationsE = buildAssociations(doc, list);
    root.appendChild(attributesE);
    root.appendChild(associationsE);

    doc.appendChild(root);
    return doc;
}