package com.test;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.NodeList;
import org.xml.sax.InputSource;
public class xmlHandle {
private Document document;
private String xmlStr;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
xmlHandle handler = new xmlHandle();
try {
handler.geneXmlByDom();
handler.getXmlFromString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void geneXmlByDom() throws Exception {
//step1:获得一个DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//step2:获得一个DocumentBuilder
DocumentBuilder db = factory.newDocumentBuilder();
//step3:新建一个Document对象
document = db.newDocument();
//step4:创建一个根节点
Element rootElement = document.createElement("Persons");
for (int i=0;i<5;i++) {
//step5:创建一个节点
Element person = document.createElement("person");
//step6:为该节点设定属性
person.setAttribute("id", "id_"+i);
Element name = document.createElement("name");
//为节点设定文本内容
name.setTextContent("name_"+i);
Element address = document.createElement("address");
address.setTextContent("address_"+i);
Element email = document.createElement("email");
email.setTextContent("email_"+i);
person.appendChild(name);
person.appendChild(address);
person.appendChild(email);
//step7:为某一元素节点设立子节点
rootElement.appendChild(person);
}
//step8:把刚刚建立的根节点添加到document对象中
document.appendChild(rootElement);
//step9:获得一个TransformerFactory对象
TransformerFactory transformerFactory = TransformerFactory.newInstance();
//step10:获得一个Transformer对象
Transformer transformer = transformerFactory.newTransformer();
//step11:把document对象用一个DOMSource对象包装起来
transformer.setOutputProperty("encoding", "utf-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
transformer.transform(new DOMSource(document), new StreamResult(bos));
xmlStr = bos.toString();
System.out.println(xmlStr);
}
public void getXmlFromString() throws Exception{
StringReader sr = new StringReader(xmlStr);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document = builder.parse(is);
NodeList nodeList = document.getElementsByTagName("person");
for (int i=0;i
//因为这里我知道它就是一个Element对象,所以进行了强转
Element person = (Element)nodeList.item(i);
//获得Element对象的属性
String id = person.getAttribute("id");
//因为这里我知道只有一个对象,所以就直接item(0)了,
//因为Dom是把每个对象都看做是一个节点的,所以如果在item(0)的时候直接取getNodeValue()是取不到值的,
//只有是TextNode的时候用那个方法才可以取到值,如果想直接取值就用getTextContent()
String name = person.getElementsByTagName("name").item(0).getTextContent();
String address = person.getElementsByTagName("address").item(0).getTextContent();
String email = person.getElementsByTagName("email").item(0).getTextContent();
System.out.println(" id = "+id+" \r\n name = "+name+" \r\n address = "+address+" \r\n email = "+email);
System.out.println("-------------------------------------");
}
}
}