使用DOM方式,Java解析XML基本步骤:
首先,我们需要建立一个解析器工厂。
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
然后可以利用这个工厂来获得一个具体的解析对象。
DocumentBuilder builder=dbf.newDocumentBuilder();
DocumentBuilder的Parse()方法接受一个XML文档名作为输入参数,返回一个Document对象。Document对象代表了 一个XML文档的树模型。
Document doc=builder.parse(”candiate.xml”);
使用Document对象的getElementsByTagName()方法,我们可以得到一个NodeList对象,他是XML文档中的标签元素 列表,可以使用NodeList对象的item()方法来得列表中的每一个Node对象。
NodeList nl=doc.getElementsByTagName(”PERSON”);
Element node=(Element)nl.item(i);
最后,我们会使用Node对象的getNodeValue()方法提取某个标签内的内容。
node.getElementsByTagName(”NAME”).item(0).getFirstChild().getNodeValue() 更多…
写XML文件代码
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
public class Writexml {
private Document document;
private String filename;
public Writexml(String name) throws ParserConfigurationException {
filename = name;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
}
public void toWrite(String mytitle, String mycontent, String myprice) {
Element root = document.createElement(”WorkShop”);
document.appendChild(root);
Element title = document.createElement(”Title”);
title.appendChild(document.createTextNode(mytitle));
root.appendChild(title);
content.appendChild(document.createTextNode(mycontent));
root.appendChild(content);
Element price = document.createElement(”Price”);
price.appendChild(document.createTextNode(myprice));
root.appendChild(price);
}
public void toSave() {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, “GB2312″);
transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
} catch (TransformerException mye) {
mye.printStackTrace();
} catch (IOException exp) {
exp.printStackTrace();
}
}
public static void main(String args[]) {
try {
Writexml myxml = new Writexml(”f:\\test.xml”);
myxml.toWrite(”中文题目”, “中文内容”, “jiangtao”);
myxml.toSave();
System.out.print(”Your writing is successful.”);
} catch (ParserConfigurationException exp) {
exp.printStackTrace();
System.out.print(”Your writing is failed.”);
}
}
}
读取XML文件代码
import org.w3c.dom.*;
public class dom {
public static void main(String args[]){
String uri=args[0];
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();//建立一个解析器工厂。
DocumentBuilder builder=factory.newDocumentBuilder();//获得一个具体的解析对象。
Document doc=builder.parse(uri);//返回一个Document对象。
System.out.println(doc.getImplementation());
NodeList nl =doc.getElementsByTagName(”PERSON”);//得到一个NodeList对象。
for (int i=0;i<nl.getLength();i++){
Element node=(Element) nl.item(i);//得列表中的每一个Node对象。
System.out.print(”NAME: “);
System.out.println (node.getElementsByTagName(”NAME”).item(0).getFirstChild().getNodeValue());
System.out.print(”ADDRESS: “);
System.out.println (node.getElementsByTagName(”ADDRESS”).item(0).getFirstChild().getNodeValue());
System.out.print(”TEL: “);
System.out.println (node.getElementsByTagName(”TEL”).item(0).getFirstChild().getNodeValue());
System.out.print(”FAX: “);
System.out.println (node.getElementsByTagName(”FAX”).item(0).getFirstChild().getNodeValue());
System.out.print(”EMAIL: “);
System.out.println (node.getElementsByTagName(”EMAIL”).item(0).getFirstChild().getNodeValue());
System.out.println();
}
}catch(Exception e){
e.printStackTrace();
}
}
}