MyDOM.java
资源名称:xml001.rar [点击查看]
上传用户:zhaokuo188
上传日期:2022-05-16
资源大小:989k
文件大小:7k
源码类别:
xml/soap/webservice
开发平台:
Java
- package com.waytojob.xml;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.List;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerConfigurationException;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.TransformerFactoryConfigurationError;
- import javax.xml.transform.stream.StreamSource;
- import org.dom4j.Comment;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.dom4j.Node;
- import org.dom4j.io.DocumentResult;
- import org.dom4j.io.DocumentSource;
- import org.dom4j.io.OutputFormat;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
- public class MyDOM {
- public static void main(String[] args) {
- MyDOM dom = new MyDOM();
- Document doc1 = dom.createDOM();
- dom.saveDOM(doc1, "E:/exam_xml/bs1.xml");
- doc1 = dom.readDOM("E:/exam_xml/bs1.xml");
- Document doc2 = dom.addElement(doc1);
- dom.saveDOM(doc2, "E:/exam_xml/bs2.xml");
- doc2 = dom.readDOM("E:/exam_xml/bs2.xml");
- Document doc3 = dom.delElement(doc2);
- dom.saveDOM(doc3, "E:/exam_xml/bs3.xml");
- doc3 = dom.readDOM("E:/exam_xml/bs3.xml");
- Document doc4 = dom.modifyElement(doc3);
- dom.saveDOM(doc4, "E:/exam_xml/bs4.xml");
- doc4 = dom.readDOM("E:/exam_xml/bs4.xml");
- System.out.println("-----------------------");
- System.out.println(dom.findElement(doc4));
- System.out.println("-----------------------");
- doc4 = dom.readDOM("E:/exam_xml/bs4.xml");
- Document doc5 = dom.addAttr(doc4);
- dom.saveDOM(doc5, "E:/exam_xml/bs5.xml");
- doc5 = dom.readDOM("E:/exam_xml/bs5.xml");
- Document doc6 = dom.modifyAttr(doc5);
- dom.saveDOM(doc6, "E:/exam_xml/bs6.xml");
- doc6 = dom.readDOM("E:/exam_xml/bs6.xml");
- Document doc7 = dom.delAttr(doc6);
- dom.saveDOM(doc7, "E:/exam_xml/bs7.xml");
- doc7 = dom.readDOM("E:/exam_xml/bs7.xml");
- Document doc8 = dom.modifyComment(doc7);
- dom.saveDOM(doc8, "E:/exam_xml/bs8.xml");
- dom.xlst();
- }
- public Document createDOM() {
- Document doc = DocumentHelper.createDocument();
- Element bookStore = doc.addElement("bookStore");
- bookStore.addAttribute("name", "新华书店");
- bookStore.addComment("新街口的那家");
- Element cate = bookStore.addElement("category");
- cate.addAttribute("name", "信息技术");
- Element book = cate.addElement("book");
- Element title = book.addElement("title");
- title.setText("精通JAVA");
- Element author = book.addElement("author");
- author.setText("James Gosling");
- Element price = book.addElement("price");
- price.setText("60.0");
- book = cate.addElement("book");
- title = book.addElement("title");
- title.setText("精通Database");
- author = book.addElement("author");
- author.setText("E.F. Codd");
- price = book.addElement("price");
- price.setText("100.0");
- System.out.println("create doc");
- return doc;
- }
- public void saveDOM(Document doc, String path) {
- FileOutputStream fos = null;
- XMLWriter xw = null;
- try {
- // 指定保存路径
- fos = new FileOutputStream(path);
- // 设保存格式1) createCompactFormat压缩格式,2)createPrettyPrint漂亮格式
- OutputFormat format = OutputFormat.createPrettyPrint();
- format.setEncoding("gbk");
- xw = new XMLWriter(fos, format);
- xw.write(doc);
- System.out.println("save dom to " + path);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (xw != null) {
- try {
- xw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public Document readDOM(String path) {
- SAXReader sr = new SAXReader();
- Document doc = null;
- try {
- doc = sr.read(new File(path));
- } catch (DocumentException e) {
- e.printStackTrace();
- }
- System.out.println("read dom from " + path);
- return doc;
- }
- public Document addElement(Document doc) {
- // 得到root元素
- Element bookStore = doc.getRootElement();
- Element cate = bookStore.addElement("category");
- cate.addAttribute("name", "经济学");
- Element book = cate.addElement("book");
- Element title = book.addElement("title");
- title.setText("宏观经济学");
- Element author = book.addElement("author");
- author.setText("Gregory Mankiw");
- Element price = book.addElement("price");
- price.setText("80.0");
- System.out.println("add element");
- return doc;
- }
- public Document delElement(Document doc) {
- // XPATH
- List<Node> nodes = doc.selectNodes("/bookStore/category/book/title");
- for (Node node : nodes) {
- if ("精通Database".equals(node.getText())) {
- node.getParent().getParent().remove(node.getParent());
- System.out.println("del element** ");
- break;
- }
- }
- return doc;
- }
- public Document modifyElement(Document doc) {
- List<Element> nodes = doc.selectNodes("/bookStore/category/book");
- for (Element node : nodes) {
- if ("精通JAVA".equals(node.element("title").getText())) {
- node.element("price").setText("30.0");
- System.out.println("modify element** ");
- break;
- }
- }
- return doc;
- }
- public String findElement(Document doc) {
- List<Element> nodes = doc.selectNodes("/bookStore/category/book");
- for (Element node : nodes) {
- if ("精通JAVA".equals(node.element("title").getText())) {
- return node.asXML();
- }
- }
- return null;
- }
- public Document addAttr(Document doc) {
- List<Element> nodes = doc
- .selectNodes("/bookStore/category[@name='经济学']");
- for (Element node : nodes) {
- // if ("经济学".equals(node.attribute("name").getText())) {
- node.addAttribute("new", "True");
- // System.out.println("add attr");
- // break;
- // }
- }
- return doc;
- }
- public Document modifyAttr(Document doc) {
- List<Element> nodes = doc
- .selectNodes("/bookStore/category[@name='信息技术']");
- for (Element node : nodes) {
- // if ("经济学".equals(node.attribute("name").getText())) {
- node.attribute("name").setText("IT");
- // System.out.println("add attr");
- // break;
- // }
- }
- return doc;
- }
- public Document delAttr(Document doc) {
- List<Element> nodes = doc
- .selectNodes("/bookStore/category[@name='经济学']");
- for (Element node : nodes) {
- // if ("经济学".equals(node.attribute("name").getText())) {
- node.remove(node.attribute("new"));
- // System.out.println("add attr");
- // break;
- // }
- }
- return doc;
- }
- public Document modifyComment(Document doc) {
- List<Comment> nodes = doc.selectNodes("/bookStore/comment()");
- for (Comment node : nodes) {
- node.setText("山西路");
- }
- return doc;
- }
- public void xlst() {
- try {
- Transformer trans = TransformerFactory.newInstance()
- .newTransformer(new StreamSource("E:/exam_xml/bs.xsl"));
- MyDOM dom = new MyDOM();
- DocumentSource ds = new DocumentSource(dom.readDOM("E:/exam_xml/bs2.xml"));
- DocumentResult dr = new DocumentResult();
- trans.transform(ds, dr);
- dom.saveDOM(dr.getDocument(), "E:/exam_xml/bs2.html");
- } catch (TransformerConfigurationException e) {
- e.printStackTrace();
- } catch (TransformerFactoryConfigurationError e) {
- e.printStackTrace();
- } catch (TransformerException e) {
- e.printStackTrace();
- }
- }
- }