文档对象模型写excel东西。
package DomXmlDemo;
import org.junit.Test;
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 javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 java.io.File;
import java.io.IOException;
public class DomXmlDem {
@Test
public void test() throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document documentu=db.parse("src\\DomXmlDemo\\stu.xml");
NodeList nodeList=documentu.getChildNodes();
for(int i=0; i<nodeList.getLength(); i++){
Node nodj=nodeList.item(i);
NodeList nodelistj=nodj.getChildNodes();
// System.out.println(i+ "--node-name:"+nodj.getNodeName()+" "+nodj.getTextContent());
for(int j=0; j<nodelistj.getLength(); j++)
{
Node nodek=nodelistj.item(j);
NodeList nodeListk=nodek.getChildNodes();
// System.out.println(j+ "--node-name:"+nodek.getNodeName()+" "+nodek.getTextContent());
if(nodek.getNodeType()==Node.ELEMENT_NODE)
{
for(int k=0; k<nodeListk.getLength(); k++)
{
Node node=nodeListk.item(k);
System.out.println(k+ "--node-name:"+node.getNodeName()+" "+node.getTextContent());
}
}
}
}
}
@Test
public void test2() throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document document=db.parse("src\\DomXmlDemo\\stu.xml");
Element element=document.getDocumentElement();
NodeList nodeListk=element.getElementsByTagName("book");
for(int k=0; k<nodeListk.getLength(); k++)
{
Node node=nodeListk.item(k);
System.out.println(k+ "--node-name:"+node.getNodeName()+" "+node.getTextContent());
}
}
@Test
public void test3() throws ParserConfigurationException, TransformerException {
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document document=db.newDocument();
if(document!=null){
Element element=document.createElement("student");
Element age=document.createElement("age");
age.appendChild(document.createTextNode("text"));
Element love=document.createElement("game");
love.appendChild(document.createTextNode("make love"));
element.setAttribute("sex", "man");
element.setAttribute("name", "jack");
element.appendChild(age);
element.appendChild(love);
document.appendChild(element);
TransformerFactory trf=TransformerFactory.newInstance();
Transformer tf=trf.newTransformer();
DOMSource dom=new DOMSource(document);
File f=new File("student.xml");
StreamResult srt=new StreamResult(f);
tf.transform(dom, srt);
}
}
}