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:
DomXmlParser.java - Parse XML File with DOM API
How to parse an XML file with the DOM API?
✍: FYIcenter
If you want to parse an XML file with the DOM 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 parse() method on the DocumentBuilder object to parse the XML file into a Document object:
Document d = b.parse(new File(args[0]));
System.out.println("Implementation class:\n "+d.getClass().getName());
3. Retrieve any information from the XML document:
Node n = d.getDocumentElement();
Here is a complete example of parsing an XML file with DOM API, DomXmlParser.java:
// Copyright (c) 2017 FYIcenter.com
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class DomXmlParser {
static String dot = "............................................................";
public static void main(String[] args) throws Exception {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document d = b.parse(new File(args[0]));
System.out.println("Implementation class:\n "+d.getClass().getName());
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, DomXmlParser.java:
>\fyicenter\jdk-1.8.0\bin\javac DomXmlParser.java >\fyicenter\jdk-1.8.0\bin\java DomXmlParser user.xml Implementation class: com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl DOM object elements and text contents: .User: ..#text: ..ID: ...#text:101 ..#text: ..BirthDate: ...#text:1970-01-01+00:01 ..#text: ..Name: ...#text:Frank Y. Ivy ..#text: ..Sex: ...#text:Male ..#text:
Note that whitespaces between elements are also parsed as TextNode objects. See the next tutorial on how to handle whitespaces.
⇒ DomXmlParserWhitespace.java - Parse XML File without Whitespaces
⇐ DomDocumentTree.java - Build DOM Document Tree
2017-09-08, ∼2247🔥, 0💬
Popular Posts:
JDK 17 jdk.jdi.jmod is the JMOD file for JDK 17 JDI (Java Debug Interface) tool. JDK 17 JDI tool com...
What Is jsse.jar (JDK 6) Java Secure Socket Extension? jsse.jar, Java Secure Socket Extension, is Ja...
JDK 17 jdk.localedata.jmod is the JMOD file for JDK 17 Localedata module. JDK 17 Locale Data module ...
JDK 17 java.naming.jmod is the JMOD file for JDK 17 Naming module. JDK 17 Naming module compiled cla...
JDK 11 jdk.internal.vm.compiler .jmodis the JMOD file for JDK 11 Internal VM Compiler module. JDK 11...