XML parsing and addition, deletion, modification, and query operations 2

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.