Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
DomDocumentTree.java - Build DOM Document Tree
How to build an XML document tree with the DOM API?
✍: FYIcenter
If you want to build an XML document tree with the DOM (Document Object Model) API,
you can follow these suggestions:
1. Use the factory class to create DocumentBuilder object:
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
2. Call the newDocument() method on the DocumentBuilder object to create a new Document object:
Document d = b.newDocument();
System.out.println("Implementation class:\n "+d.getClass().getName());
3. Call the createElement() method on the Document object to create a new Element object. Then add it to the Document as the root element:
Element e = d.createElement("User");
d.appendChild(e);
3. Call the createElement() method on the Document object repeatedly to create new Element objects. Then add them to parent elements to build the XML document tree:
Element c = d.createElement("ID");
c.appendChild(d.createTextNode("202"));
e.appendChild(c);
Here is a complete example of parsing an XML file with DOM API, DomXmlParser.java:
// Copyright (c) 2017 FYIcenter.com
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DomDocumentTree {
static String dot = "............................................................";
public static void main(String[] args) throws Exception {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document d = b.newDocument();
System.out.println("Implementation class:\n "+d.getClass().getName());
Element e = d.createElement("User");
d.appendChild(e);
Element c = d.createElement("ID");
c.appendChild(d.createTextNode("202"));
e.appendChild(c);
c = d.createElement("Name");
c.appendChild(d.createTextNode("Frank Y. Ivy"));
e.appendChild(c);
c = d.createElement("BirthDate");
c.appendChild(d.createTextNode("1970-01-01"));
e.appendChild(c);
System.out.println("DOM object elements and text contents:");
Node n = d.getDocumentElement();
printText(n, 1);
}
public static void printText(Node n, int l) {
String v = "";
if (n.getNodeType()==Node.TEXT_NODE) v = n.getTextContent();
System.out.println(dot.substring(0,l)+n.getNodeName()+":"+v);
NodeList c = n.getChildNodes();
for (int i=0; i<c.getLength(); i++) {
printText(c.item(i),l+1);
}
}
}
Compile and run the example program, DomDocumentTree.java:
>\fyicenter\jdk-1.8.0\bin\javac DomDocumentTree.java >\fyicenter\jdk-1.8.0\bin\java DomDocumentTree Implementation class: com.sun.org.apache.xerces.internal.dom.DocumentImpl DOM object elements and text contents: .User: ..ID: ...#text:202 ..Name: ...#text:Frank Y. Ivy ..BirthDate: ...#text:1970-01-01
⇒ DomXmlParser.java - Parse XML File with DOM API
⇐ Override Apache Xerces Included in Java SE 8
2017-09-08, ∼2418🔥, 0💬
Popular Posts:
The Java Naming and Directory Interface (JNDI) is part of the Java platform, providing applications ...
JDK 17 jdk.internal.le.jmod is the JMOD file for JDK 17 Internal Line Editing module. JDK 17 Interna...
How to display XML element type information with the jaxp\TypeInfoWriter.java provided in the Apache...
maven-settings-builder-3 .8.6.jaris the JAR file for Apache Maven 3.8.6 Settings Builder module. Apa...
JDK 17 jdk.crypto.cryptoki.jmod is the JMOD file for JDK 17 Crypto Cryptoki module. JDK 17 Crypto KI...