package cn.liuning.test;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Demo1
{
@Test//dom生成XML文档
public void createdXmlbyDom() throws ParserConfigurationException, TransformerException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
//加入根结点
Element rootElement = document.createElement("xml");
//新建子结点
Element toUserName = document.createElement("ToUserName");
//设置属性
toUserName.setAttribute("id", "id_1");
//设置内容
String str = "<![CDATA[gh_1c586f95bd68]]>";
toUserName.setTextContent(str);
Element fromUserName = document.createElement("FromUserName");
fromUserName.setAttribute("id", "id_2");
String str1 = "<![CDATA[ogNtkuMTj4uOx-DCvLfu8mUDmubM]]>";
fromUserName.setTextContent(str1);
Element createTime = document.createElement("CreateTime");
createTime.setAttribute("id", "id_2");
String str2 = "1403313142";
createTime.setTextContent(str2);
Element createTime1 = document.createElement("CreateTime");
createTime1.setAttribute("id", "id_2");
String str3 = "Test";
createTime1.setTextContent(str3);
rootElement.appendChild(toUserName);
rootElement.appendChild(fromUserName);
rootElement.appendChild(createTime);
rootElement.appendChild(createTime1);
document.appendChild(rootElement);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source xmlSource = new DOMSource(document);
Result outputTarget = new StreamResult(new File("src/book1.xml"));
transformer.transform(xmlSource, outputTarget);
System.out.println("成功");
}
@Test//dom解析XML文档
public void read() throws ParserConfigurationException, Exception, IOException
{
//1.新建工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.得到dom解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//3.得到代表文档的document上
Document document = builder.parse("src/book1.xml");
System.out.println(document.getInputEncoding());//得到文档编码
//通过标签名获取标签,得到的是一个list集合
//NodeList list = document.getElementsByTagName("CreateTime");
NodeList list = document.getElementsByTagName("xml");
//常用方法
System.out.println(list.getLength());//可以输出list的长度
Node node = list.item(0); //得到第几个结点
//node方法
System.out.println(node.getNodeName());//得到结点名称,输出CreateTime
System.out.println(node.hasAttributes());//是否有属性,输出true
System.out.println(node.hasChildNodes());//是否有孩子,输出true
String content = node.getTextContent();//获取结点内容
System.out.println(content);
}
}