XML
dom解析
读取节点
public class DOMDemo {
public static void main(String[] args) {
//1. 建立DocumentBuilderFactory 用于获取DocumentBuilder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2. 通过DocumentBuilderFactory 取得DocumentBuilder
DocumentBuilder builder = null;
try{
builder = factory.newDocumentBuilder();
}catch(Exception e){
e.printStackTrace();
}
//3. 定义document接口对象,通过DocumentBuilder类进行DOM树的转换操作
Document doc = null;
try {
System.out.println(File.separator);
//读取指定路径的XML文件
doc = builder.parse("d:" + File.separator + "NewFile.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//查找name的节点
NodeList nl = doc.getElementsByTagName("name");
System.out.println("姓名:" + nl.item(0).getFirstChild().getNodeValue());
}
}
public static void main(String[] args) {
//1. 建立DocumentBuilderFactory 用于获取DocumentBuilder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2. 通过DocumentBuilderFactory 取得DocumentBuilder
DocumentBuilder builder = null;
try{
builder = factory.newDocumentBuilder();
}catch(Exception e){
e.printStackTrace();
}
//3. 定义document接口对象,通过DocumentBuilder类进行DOM树的转换操作
Document doc = null;
try {
System.out.println(File.separator);
//读取指定路径的XML文件
doc = builder.parse("d:" + File.separator + "NewFile.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//查找name的节点
NodeList nl = doc.getElementsByTagName("name");
System.out.println("姓名:" + nl.item(0).getFirstChild().getNodeValue());
}
}
读取多个
NodeList nl = doc.getElementsByTagName("linkman");
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element)nl.item(i);
System.out.println("姓名:" + e.getElementsByTagName("name").item(0).
getFirstChild().getNodeValue());
System.out.println("电话:" + e.getElementsByTagName("tel").item(0).
getFirstChild().getNodeValue());
}
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element)nl.item(i);
System.out.println("姓名:" + e.getElementsByTagName("name").item(0).
getFirstChild().getNodeValue());
System.out.println("电话:" + e.getElementsByTagName("tel").item(0).
getFirstChild().getNodeValue());
}
将生成的XML文件输出到文件中
//3. 定义document接口对象,通过DocumentBuilder类进行DOM树的转换操作
Document doc = null;
doc = builder.newDocument();
//4.建立各个操作节点
Element add = doc.createElement("add");
Element linkman = doc.createElement("linkman");
Element name = doc.createElement("name");
Element email = doc.createElement("email");
//5. 设置节点的文本内容,即为没一个节点添加文本节点
name.appendChild(doc.createTextNode("jordan"));
email.appendChild(doc.createTextNode(" jordan_513@163.com"));
//6.设置节点关系
linkman.appendChild(name);
linkman.appendChild(email);
add.appendChild(linkman);
doc.appendChild(add);
//7.输出文档到文件中
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = null;
try {
t= tf.newTransformer();
} catch (TransformerConfigurationException e1) {
e1.printStackTrace();
}
t.setOutputProperty(OutputKeys.ENCODING,"GBK");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("d:" + File.separator
+ "b.xml"));
try {
t.transform(source,result);
} catch (TransformerException e1) {
e1.printStackTrace();
}
Document doc = null;
doc = builder.newDocument();
//4.建立各个操作节点
Element add = doc.createElement("add");
Element linkman = doc.createElement("linkman");
Element name = doc.createElement("name");
Element email = doc.createElement("email");
//5. 设置节点的文本内容,即为没一个节点添加文本节点
name.appendChild(doc.createTextNode("jordan"));
email.appendChild(doc.createTextNode(" jordan_513@163.com"));
//6.设置节点关系
linkman.appendChild(name);
linkman.appendChild(email);
add.appendChild(linkman);
doc.appendChild(add);
//7.输出文档到文件中
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = null;
try {
t= tf.newTransformer();
} catch (TransformerConfigurationException e1) {
e1.printStackTrace();
}
t.setOutputProperty(OutputKeys.ENCODING,"GBK");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("d:" + File.separator
+ "b.xml"));
try {
t.transform(source,result);
} catch (TransformerException e1) {
e1.printStackTrace();
}
区别 | DOM解析 | SAX解析 |
操作 | 将所有文件读取到内存中形成DOM树,如果文件两过大,则无法使用 | 顺序读入所需要的文件内容,不会一次性全部读取,不受文件大小的限制 |
访问限制 | DOM树在内存中形成,可以随意存放或读取文件树的任何部分,无次数限制 | 由于采用部分读取,只能对文件按顺序解析,不支持对文件的随意存放 |
修改 | 可以任意修改文件树 | 只能读取,不能修改 |
复杂度 | 易于理解,易于开发 | 开发上麻烦,还要自己定义事件处理器 |
对象模型 | 系统为使用者自动建立DOM树,XML对象模型由系统提供 | 灵活点 可以用SAX建立自己的XML对象模型 |
DOM适合对文件进行修改和随机存取的操作 不适合对大型文件的操作
SAX可处理大型文件 可由用户自己建立自己的对象模型
传说中最牛X的解析器 JDOM 综合两个的优点
public class JDOMDemo {
public static void main(String[] args) {
Element add = new Element("add");
Element linkman = new Element("linkman");
Element name = new Element("name");
Element email = new Element("email");
Attribute id = new Attribute("id","lxh");
Document doc = new Document(add);
name.setText("jordan");
email.setText("Michael");
linkman.addContent(name);
linkman.addContent(email);
add.addContent(linkman);
XMLOutputter out = new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("GBK"));
try {
out.output(doc,new FileOutputStream("D:"+File.separator+ "c.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Element add = new Element("add");
Element linkman = new Element("linkman");
Element name = new Element("name");
Element email = new Element("email");
Attribute id = new Attribute("id","lxh");
Document doc = new Document(add);
name.setText("jordan");
email.setText("Michael");
linkman.addContent(name);
linkman.addContent(email);
add.addContent(linkman);
XMLOutputter out = new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("GBK"));
try {
out.output(doc,new FileOutputStream("D:"+File.separator+ "c.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
读取xml文件里的内容
public static void main(String[] args) throws Exception {
SAXBuilder builder = new SAXBuilder();
Document read_doc = builder.build("D:"+File.separator+ "c.xml");
Element stu = read_doc.getRootElement();
List list = stu.getChildren("linkman");
for(int i = 0; i<list.size();i++){
Element e = (Element)list.get(i);
String name = e.getChildText("name");
String id = e.getChild("name").getAttribute("id").getValue();
String email = e.getChildText("email");
System.out.println("------联系人----------");
System.out.println("姓名:" + name + ",编号:" + id);
System.out.println("Email:" + email);
System.out.println("------------");
System.out.println();
}
}
SAXBuilder builder = new SAXBuilder();
Document read_doc = builder.build("D:"+File.separator+ "c.xml");
Element stu = read_doc.getRootElement();
List list = stu.getChildren("linkman");
for(int i = 0; i<list.size();i++){
Element e = (Element)list.get(i);
String name = e.getChildText("name");
String id = e.getChild("name").getAttribute("id").getValue();
String email = e.getChildText("email");
System.out.println("------联系人----------");
System.out.println("姓名:" + name + ",编号:" + id);
System.out.println("Email:" + email);
System.out.println("------------");
System.out.println();
}
}