book.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="001">
<name>love you</name>
<price>20.5</price>
</book>
<book id="002">
<name>hurt my heart</name>
<price>27.5</price>
</book>
</books>
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="001">
<name>love you</name>
<price>20.5</price>
</book>
<book id="002">
<name>hurt my heart</name>
<price>27.5</price>
</book>
</books>
java文件
package com.test;
import java.io.File;
import java.io.IOException;
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;
/**
* DOM解析XML
* 通过获取子节点方法
* @author kai
*/
public class DomXML {
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
System.out.println(dbf.getClass().getName());
try {
DocumentBuilder db=dbf.newDocumentBuilder();
//读取文件
Document doc=db.parse(new File("book.xml"));
//获取子节点
NodeList nl=doc.getElementsByTagName("book");
int len=nl.getLength();
//获取节点数
System.out.println("节点数:"+len);
for(int i=0;i<len;i++)
{
Element eltStu=(Element)nl.item(i);
Node eltName=eltStu.getElementsByTagName("name").item(0);
Node eltAge=eltStu.getElementsByTagName("price").item(0);
String name=eltName.getFirstChild().getNodeValue();
String price=eltAge.getFirstChild().getNodeValue();
System.out.println("name: "+name);
System.out.println("price: "+price);
System.out.println("-------------------------");
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.test;
import java.io.File;
import java.io.IOException;
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;
/**
* DOM解析XML
* 通过获取子节点方法
* @author kai
*/
public class DomXML {
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
System.out.println(dbf.getClass().getName());
try {
DocumentBuilder db=dbf.newDocumentBuilder();
//读取文件
Document doc=db.parse(new File("book.xml"));
//获取子节点
NodeList nl=doc.getElementsByTagName("book");
int len=nl.getLength();
//获取节点数
System.out.println("节点数:"+len);
for(int i=0;i<len;i++)
{
Element eltStu=(Element)nl.item(i);
Node eltName=eltStu.getElementsByTagName("name").item(0);
Node eltAge=eltStu.getElementsByTagName("price").item(0);
String name=eltName.getFirstChild().getNodeValue();
String price=eltAge.getFirstChild().getNodeValue();
System.out.println("name: "+name);
System.out.println("price: "+price);
System.out.println("-------------------------");
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}