domwriter.java
来自「xstream是一个把java object序列化成xml文件的开源库,轻便好用」· Java 代码 · 共 59 行
JAVA
59 行
package com.thoughtworks.xstream.io.xml;import com.thoughtworks.xstream.io.HierarchicalStreamWriter;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;/** * @author Michael Kopp */public class DomWriter implements HierarchicalStreamWriter { private final Document document; private Element current; public DomWriter(Document document) { this.document = document; this.current = document.getDocumentElement(); } public DomWriter(Element rootElement) { document = rootElement.getOwnerDocument(); current = rootElement; } public void startNode(String name) { final Element child = document.createElement(name); if (current == null) { document.appendChild(child); } else { current.appendChild(child); } current = child; } public void addAttribute(String name, String value) { current.setAttribute(name, value); } public void setValue(String text) { current.appendChild(document.createTextNode(text)); } public void endNode() { Node parent = current.getParentNode(); current = parent instanceof Element ? (Element)parent : null; } public void flush() { // don't need to do anything } public void close() { // don't need to do anything } public HierarchicalStreamWriter underlyingWriter() { return this; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?