import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.inputstream;
import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import javax.xml.parsers.parserconfigurationexception;
import org.w3c.dom.document;
import org.w3c.dom.element;
import org.w3c.dom.node;
import org.w3c.dom.nodelist;
import org.xml.sax.saxexception;
public class domparse {
public domparse() {
documentbuilderfactory domfac = documentbuilderfactory.newinstance();
try {
documentbuilder dombuilder = domfac.newdocumentbuilder();
inputstream is = new fileinputstream("bin/library.xml");
document doc = dombuilder.parse(is);
element rootelement = doc.getdocumentelement();
system.out.println(rootelement);
nodelist childnodes = rootelement.getchildnodes();
system.out.println("子节点"+childnodes);
if (childnodes != null) {
for (int i = 0; i < childnodes.getlength(); i++) {
node node_index = childnodes.item(i);
system.out.println("节点名称"+node_index.getnodename());
if (node_index.getnodetype() == node.element_node) {
string email = node_index.getattributes()
.getnameditem("email").getnodevalue();
system.out.println("email="+email);
for (node node = node_index.getfirstchild(); node != null; node = node
.getnextsibling())
{
if (node.getnodetype() == node.element_node)
if (node.getnodename().equals("name"))
string name = node.getnodevalue();
string name1 = node.getfirstchild()
.getnodevalue();
system.out.println("name="+name);
system.out.println("name child="+name1);
}
if (node.getnodename().equals("price"))
string price = node.getfirstchild()
system.out.println("price="+price);
} catch (parserconfigurationexception e) {
e.printstacktrace();
} catch (filenotfoundexception e) {
} catch (saxexception e) {
} catch (ioexception e) {
public static void main(string[] args) {
new domparse();
<?xml version="1.0" encoding="gb2312"?>
<books name="hello">
<book email="[email protected]">
<name>name_name</name>
<price>price_price</price>
</book>
</books>