-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathDOMReadAndPrintDocument.java
31 lines (24 loc) · 1.45 KB
/
DOMReadAndPrintDocument.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Element; import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Node; import javax.xml.transform.TransformerFactory;
import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File; import javax.xml.transform.dom.DOMSource;
public class DOMReadAndPrintDocument {
public static void main(final String argv[]) throws Throwable {
Document document; Node n, m;
document = DocumentBuilderFactory.newInstance() // read the course example
.newDocumentBuilder()// document as DOM tree
.parse("../xml/course.xml");
n = document.createElement("myNewElement"); // create and add a new element
document.getDocumentElement().appendChild(n);
m = document.createElement("myNewSubElement"); // create and add a sub-element
n.appendChild(m);
n = document.createAttribute("myAttribute"); // create a new attribute
n.setNodeValue("myAttributeValue"); // add the attribute
m.getAttributes().setNamedItem(n);
TransformerFactory.newInstance()
.newTransformer()
.transform(new DOMSource(document),
new StreamResult(System.out)); // print to stdout
}
}