I wrote a node operation interface INodeAction and directly pasted the Code: The getpreviussibling method is to get the last adjacent node, and getNextSibling is to get the next adjacent node. The getBeifen method is mainly used for document-based purposes. If it is the root node, the system returns 0. The printSpace method is based on the generation of nodes.
I wrote a node operation interface INodeAction and directly pasted the Code: The getpreviussibling method is to get the last adjacent node, and getNextSibling is to get the next adjacent node. The getBeifen method is mainly used for document-based purposes. If it is the root node, the system returns 0. The printSpace method is based on the generation of nodes.
I wrote a node operation interface INodeAction and directly pasted the Code:
The getpreviussibling method is used to obtain the last adjacent node, and getNextSibling is used to obtain the next adjacent node.
The getBeifen method is mainly used for document formatting. If it is the root node, 0 is returned.
The printSpace method is formatted when writing documents based on the generation of nodes.
Then write the Node's base class Node:
Import java. io. serializable; import java. util. list;/*** xml document Node base class, which can be common nodes, comment nodes, and text nodes **/public abstract class Node implements Serializable, INodeAction {/*** serialization version number */private static final long serialVersionUID = 410789701720999153l;/*** parent node. specify that the parent node of the root node is empty **/protected Element father;/*** the node domFather directly added by the document Object is assigned the Document Object **/protected document domFather; @ Override public Integer getBeifen () {Integer n; if (father = null) {n = 0; return n;} Node node = this; n = 0; while (node. father! = Null) {n ++; node = node. father; if (node. father = null) {return n ;}} if (! (Node. father = null) {return null;} return n;} public Element getFather () {return father ;}@ Override public String printSpace () {StringBuffer sb = new StringBuffer (); Integer n = getBeifen (); if (n = null) {return sb. toString () ;}for (int I = 0; I <n; I ++) {sb. append ("");} return sb. toString ();} public Document getDomFather () {return domFather;} @ Override public Node getpreviussibli Ng () {if (father = null) {if (domFather = null) {DocumentUtil. throwException ("the node has not been added to the document! ");} Else {List
NodeList = domFather. getNodeList (); int idx = nodeList. indexOf (this); return idx = 0? Null: nodeList. get (idx-1);} List
NodeList = father. getNodeList (); int idx = nodeList. indexOf (this); return idx = 0? Null: nodeList. get (idx-1) ;}@ Override public Node getNextSibling () {if (father = null) {if (domFather = null) {DocumentUtil. throwException ("the node has not been added to the document! ");} Else {List
NodeList = domFather. getNodeList (); int idx = nodeList. indexOf (this); return idx = nodeList. size ()-1? Null: nodeList. get (idx + 1) ;}} List
NodeList = father. getNodeList (); int idx = nodeList. indexOf (this); return idx = nodeList. size ()-1? Null: nodeList. get (idx + 1 );}}
The key lies in the idea of programming. Everything can be achieved with an idea. The following is the specific implementation class of the write node. Another article.