List of usage examples for org.w3c.dom Element setAttribute
public void setAttribute(String name, String value) throws DOMException;
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*ww w .j a va2 s.c o m*/ factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); element.setAttribute("newAttrName", "attrValue"); element.setAttribute("newAttrName", "<>&\"'"); element.removeAttribute("value"); boolean has = element.hasAttribute("value"); // true String attrValue = element.getAttribute("value"); // mydefault }
From source file:Main.java
public static void main(String[] args) throws Throwable { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);// www . ja v a 2 s . co m DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElement("root"); root.setAttribute("xmlns:m", "http://www.java2s.com/blog"); root.setAttribute("xmlns:rt", "http://www.java2s.com/forum"); doc.appendChild(root); Element elt = doc.createElement("simple"); elt.setAttribute("m:Path", "false"); elt.setAttribute("m:Content", "false"); elt.setAttribute("rt:file", "false"); root.appendChild(doc.createTextNode("\n\t")); root.appendChild(elt); root.appendChild(doc.createTextNode("\n")); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(System.out)); }
From source file:MainClass.java
public static void main(String args[]) { Document dom = DOMImplementation.createDocument(null, null, null); Element root = dom.createElement("games"); Element child1 = dom.createElement("game"); root.appendChild(child1);/*from www . j a va 2s.c om*/ child1.setAttribute("A", "a"); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); Element book = document.createElement("book"); book.setAttribute("id", "javanut4"); document.appendChild(book);/*www. j a v a 2s . c o m*/ for (int i = 1; i <= 3; i++) { Element chapter = document.createElement("chapter"); Element title = document.createElement("title"); title.appendChild(document.createTextNode("Chapter " + i)); chapter.appendChild(title); chapter.appendChild(document.createElement("para")); book.appendChild(chapter); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); }
From source file:Main.java
public static void main(String[] args) throws Exception { userdom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = userdom.createElement("users"); Node adoptNode = userdom.adoptNode(root); userdom.appendChild(adoptNode);// w w w . ja v a 2s. c o m Element e = userdom.createElement("user"); e.setAttribute("id", "blah"); e.setAttribute("username", "kermit"); e.setAttribute("password", "bunnies in the air"); e.setAttribute("login", "kermmi"); userdom.getFirstChild().appendChild(e); System.out.println(e); System.out.println(userdom.getFirstChild() + "|" + userdom.getFirstChild().getFirstChild()); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); System.out.println(userdom.getFirstChild() + "|" + userdom.getFirstChild().getFirstChild()); DOMSource ds = new DOMSource(userdom); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { StreamResult sr = new StreamResult(baos); trans.transform(ds, sr); System.out.println(new String(baos.toByteArray())); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document doc = impl.createDocument(null, null, null); Element e1 = doc.createElement("api"); doc.appendChild(e1);/*from ww w .ja v a2s.c o m*/ Element e2 = doc.createElement("java"); e1.appendChild(e2); e2.setAttribute("url", "http://www.domain.com"); DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w w w . j a v a 2s .c o m DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.newDocument(); String docNS = "http://www.my-company.com"; Element order = document.createElementNS(docNS, "order"); document.appendChild(order); order.setAttribute("xmlns", docNS); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document doc = impl.createDocument(null, null, null); Element e1 = doc.createElement("api"); doc.appendChild(e1);/*w ww. j a v a 2 s . co m*/ Element e2 = doc.createElement("java"); e1.appendChild(e2); e2.setAttribute("url", "http://www.java2s.com"); //transform the DOM for showing the result in console DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document doc = impl.createDocument(null, null, null); Element e1 = doc.createElement("api"); doc.appendChild(e1);//from ww w . j a v a 2 s . co m Element e2 = doc.createElement("java"); e1.appendChild(e2); e2.setAttribute("url", "http://www.java2s.com"); // transform the DOM for showing the result in console DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); }
From source file:MainClass.java
public static void main(String args[]) throws IOException { Document dom = DOMImplementation.createDocument(null, null, null); Element root = dom.createElement("A"); Element child1 = dom.createElement("B"); child1.appendChild(dom.createTextNode("C")); child1.setAttribute("A", "a"); root.appendChild(child1);//from w ww . j ava 2 s.c o m dom.appendChild(root); XMLSerializer serial = new XMLSerializer(System.out, null); serial.serialize(dom.getDocumentElement()); }