Example usage for org.w3c.dom Attr setValue

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

Introduction

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

Prototype

public void setValue(String value) throws DOMException;

Source Link

Document

On retrieval, the value of the attribute is returned as a string.

Usage

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);//from  w  w  w .  j  a va2  s  . c o m

    // 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:GetNameAsAttr.java

public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    Attr result = (Attr) xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NODE);
    System.out.println(result.getValue());

    result.setValue("The Colbert Report");

}

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);
    root.setAttributeNode(attr);// w  w w .  java2  s.c  om
}

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);
    element.setAttributeNode(attr);/*from  w w  w.ja va  2  s .c o  m*/
}

From source file:Main.java

public static void addAttribute(Document doc, Element node, String name, String value) {
    Attr attribute = doc.createAttribute(name);
    attribute.setValue(value);
    node.getAttributes().setNamedItem(attribute);
}

From source file:Main.java

public static void addAttribute(Document document, Node parent, String attrName, String value) {
    Attr attr = document.createAttribute(attrName);
    attr.setValue(value);
    NamedNodeMap map = parent.getAttributes();
    map.setNamedItem(attr);//  ww  w .  j  a  v a  2  s  .  co  m
}

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);

    return attr_action_type;
}

From source file:Main.java

public static void addAttributeToElement(Document doc, Element projectElement, String attributeName,
        String attributeValue) {/*  ww w.  j  a v a2s.co  m*/
    Attr xmlnsAttr = doc.createAttribute(attributeName);
    xmlnsAttr.setValue(attributeValue);
    projectElement.setAttributeNode(xmlnsAttr);
}

From source file:Main.java

public static void addAttribute(Document document, Node node, String name, String value) {
    if (value != null) {
        Attr attribute = document.createAttribute(name);
        attribute.setValue(value);
        node.getAttributes().setNamedItem(attribute);
    }//  www  .  j  a v a  2  s. co  m
}

From source file:Main.java

/**
 * Add the first node with a namespace attribute as below:
 * <asset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 * @nodeName: name of the element/*from  w  w  w.j  av a  2  s .c  o m*/
 * @
 * */
public static Document addNodeWithNSAttribute(Document document, String nodeName, String attrName,
        String namespace) {
    Element node = document.createElement(nodeName);
    Attr attr = document.createAttribute(attrName);
    attr.setValue(namespace);
    node.setAttributeNodeNS(attr);
    document.appendChild(node);
    return document;
}