DOM是把XML整个文件都载入到内存,生成一个树,然后通过遍历来获取需要的标签及对应的值。
对于需要遍历所有节点的话,这样做速度会比较快,一次载入就可以了。
如果是只关心其中的某个节点,那这样的效率是很低的。
接下代码如下:
xml很简单:
对于需要遍历所有节点的话,这样做速度会比较快,一次载入就可以了。
如果是只关心其中的某个节点,那这样的效率是很低的。
接下代码如下:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
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;
import com.zhouxf.javabase.xml.XmlDocument;
public class DomXmlDocument implements XmlDocument {
private Document document;
public void init() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
this.document = builder.newDocument();
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
}
}
public void createXml(String fileName) {
// 创建根目标标签
Element root = this.document.createElement("users");
this.document.appendChild(root);
// 创建子标签
Element employee = this.document.createElement("user");
Element name = this.document.createElement("name");
name.appendChild(this.document.createTextNode("zhouxf"));
employee.appendChild(name);
Element age = this.document.createElement("age");
age.appendChild(this.document.createTextNode("29"));
employee.appendChild(age);
root.appendChild(employee);
// 创建转换源数据为树数据的工具的工厂
TransformerFactory tf = TransformerFactory.newInstance();
try {
// 获取一个转化工具
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
// 是否允许空格
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// 输出文件
PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
StreamResult result = new StreamResult(pw);
// 转换输出结果
transformer.transform(source, result);
System.out.println("生成XML文件成功!");
} catch (TransformerConfigurationException e) {
System.out.println(e.getMessage());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (TransformerException e) {
System.out.println(e.getMessage());
}
}
public void parserXml(String fileName) {
try {
// 创建build 工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 通过工厂创建一个builder
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fileName);
Element documentElement = document.getDocumentElement();
NodeList users = documentElement.getChildNodes();
// NodeList users = document.getChildNodes();
for (int i = 0; i < users.getLength(); i++) {
Node user = users.item(i);
NodeList userInfo = user.getChildNodes();
for (int j = 0; j < userInfo.getLength(); j++) {
Node node = userInfo.item(j);
NodeList userMeta = node.getChildNodes();
for (int k = 0; k < userMeta.getLength(); k++) {
System.out.println(userMeta.item(k).getNodeName() + ":"
+ userMeta.item(k).getTextContent());
}
}
}
System.out.println("解析完毕");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
} catch (SAXException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public class DomTest {
public static void main(String[] args) {
DomXmlDocument document = new DomXmlDocument();
URL resource = DomTest.class.getClassLoader().getResource(
"./xml/test.xml");
try {
if (resource != null) {
System.out.println(resource.toURI().getPath());
document.parserXml(resource.toURI().getPath());
} else {
System.out.println("null..........");
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
xml很简单:
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<name>
zhouxf
</name>
<age>
28
</age>
</user>
</users>