Example usage for org.w3c.dom Document createAttribute

List of usage examples for org.w3c.dom Document createAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Document createAttribute.

Prototype

public Attr createAttribute(String name) throws DOMException;

Source Link

Document

Creates an Attr of the given name.

Usage

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    Attr attr = xmlDoc.createAttribute("text");

}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    // root elements
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("company");
    doc.appendChild(rootElement);// w  ww.j av  a 2  s  . com

    // staff elements
    Element staff = doc.createElement("Staff");
    rootElement.appendChild(staff);

    // set attribute to staff element
    Attr attr = doc.createAttribute("id");
    attr.setValue("1");
    staff.setAttributeNode(attr);

    // shorten way
    // staff.setAttribute("id", "1");

    // firstname elements
    Element firstname = doc.createElement("firstname");
    firstname.appendChild(doc.createTextNode("A"));
    staff.appendChild(firstname);

    // lastname elements
    Element lastname = doc.createElement("lastname");
    lastname.appendChild(doc.createTextNode("B"));
    staff.appendChild(lastname);

    // nickname elements
    Element nickname = doc.createElement("nickname");
    nickname.appendChild(doc.createTextNode("C"));
    staff.appendChild(nickname);

    // salary elements
    Element salary = doc.createElement("salary");
    salary.appendChild(doc.createTextNode("100000"));
    staff.appendChild(salary);

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("C:\\file.xml"));

    // Output to console for testing
    // StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);

    System.out.println("File saved!");
}

From source file:Main.java

public static Attr createAttribute(Document document, String name, String value) {
    Attr attr = document.createAttribute(name);
    attr.setTextContent(value);//from  w  ww.j  a v a  2  s .co m
    return attr;
}

From source file:Main.java

private static void addAttribute(Document doc, Element root, String param, String value) {
    Attr attr = doc.createAttribute(param);
    attr.setValue(value);//from   ww  w. j  a  v  a  2  s.co  m
    root.setAttributeNode(attr);
}

From source file:Main.java

private static Attr getOrCreateAttribute(Element parent, String name) {
    Attr a = parent.getAttributeNode(name);
    if (a == null) {
        Document doc = parent.getOwnerDocument();
        a = doc.createAttribute(name);
        parent.setAttributeNode(a);//from w ww  . j  av a  2  s  . c  om
    }
    return a;
}

From source file:Main.java

public static void addAttribute(Document doc, Element element, String name, String value) {
    Attr attr = doc.createAttribute(name);
    attr.setValue(value);/*from  w  w w.j  a  va 2 s .co  m*/
    element.setAttributeNode(attr);
}

From source file:Main.java

public static Node createAttributeNode(Document document, String str, String str2) {
    Node createAttribute = document.createAttribute(str);
    if (createAttribute != null) {
        createAttribute.setNodeValue(str2);
    }/*from  w w  w .j  a  v a2 s.  c o  m*/
    return createAttribute;
}

From source file:Main.java

public static Attr getActionTypeAttribute(Document doc, String attrName, String action) {
    Attr attr_action_type = doc.createAttribute(attrName);
    attr_action_type.setValue(action);/* www  .  java  2  s.  co m*/

    return attr_action_type;
}

From source file:Main.java

/**
 * Adds a {@link Attr} to the parent {@link Element}. 
 *
 * @param doc/*w ww  .  j a  v a2 s .  com*/
 * @param parent
 * @param name
 * @return
 */
public static Attr addAttr(Document doc, Element parent, String name) {
    final Attr attr = doc.createAttribute(name);
    parent.setAttributeNode(attr);
    return attr;
}

From source file:Main.java

/**
 * Get the attribute.//from  ww  w .j a v a2 s . c  o m
 * 
 * @param attributeName
 *          the attribute name
 * @param attributeValue
 *          the attribute value
 * @param doc
 *          the document
 * @return the attribute
 */
private static Attr getAttribute(String attributeName, String attributeValue, Document doc) {
    Attr attr = doc.createAttribute(attributeName);
    attr.setNodeValue(attributeValue);
    return attr;
}