您的位置:首页 > 其它

DOM创建及解析XML文件

2013-06-24 00:00 357 查看
public interface XmlDocument {
/***
* 创建XML文件
* @param filePath 文件路径
*/
public void createXml(String filePath);

/***
* 解析XML文件
* @param filePath 文件路径
*/
public void analyzeXml(String filePath);
}
import java.io.FileOutputStream;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;
public class DomDemo implements XmlDocument {
private Document document;
private String filePath;
public void init()
{
try {
DocumentBuilderFactory df=DocumentBuilderFactory.newInstance();
DocumentBuilder db=df.newDocumentBuilder();
this.document=db.newDocument();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void createXml(String filePath) {
Element root=this.document.createElement("employees");
this.document.appendChild(root);
Element employee=this.document.createElement("employee");
Element name=this.document.createElement("name");
name.appendChild(this.document.createTextNode("你好"));
employee.appendChild(name);
Element sex=this.document.createElement("sex");
sex.appendChild(this.document.createTextNode("m"));
employee.appendChild(sex);
Element age = this.document.createElement("age");
age.appendChild(this.document.createTextNode("22"));
employee.appendChild(age);
root.appendChild(employee);
try {
TransformerFactory tff=TransformerFactory.newInstance();
Transformer tf=tff.newTransformer();
DOMSource doms=new DOMSource(document);
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw=new PrintWriter(new FileOutputStream(filePath));
StreamResult sr=new StreamResult(pw);
tf.transform(doms, sr);
System.out.println("XML创建成功!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void analyzeXml(String filePath) {
try {
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document dt=db.parse(filePath);
NodeList employees=dt.getChildNodes();
for(int i=0;i<employees.getLength();i++)
{
Node employee=employees.item(i);
NodeList employeeInfo=employee.getChildNodes();
for(int j=0;j<employeeInfo.getLength();j++)
{
Node node=employeeInfo.item(j);
NodeList employeeMeta=node.getChildNodes();
for(int k=0;k<employeeMeta.getLength();k++)
{
System.out.println(employeeMeta.item(k).getNodeName()
+ ":" + employeeMeta.item(k).getTextContent());
}
}
}
System.out.println("XML解析成功!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
//DomDemo dd=new DomDemo();
//dd.init();
//dd.createXml("F:/dom123.xml");
//dd.parserXml("F:/dom123.xml");

}

注意事项:创建XML文件,如果为UTF-8编码,要把eclipse编码设置为UTF-8,否则创建的XML文件编码会有问题,解析也会出问题
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  解析XML DOM XML 解析