Java 怎么插入替换xml node.gettexContent()

2019年8月3日23:41:49Java 怎么插入替换xml node.gettexContent()已关闭评论 513 1066字阅读3分33秒

1.简介

Java 怎么插入替换xml node.gettexContent()?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    
     
    public static String getText(Node nd) {

        short type = nd.getNodeType();

        
        
        
        switch (type) {

        case Node.DOCUMENT_NODE: 
        case Node.DOCUMENT_TYPE_NODE: 
        case Node.NOTATION_NODE: 
            return null;
        }

        StringBuilder sb = new StringBuilder();
        getText(nd, sb);
        return sb.toString();
    }

    
    private static void getText(Node nd, StringBuilder buf) {

        short type = nd.getNodeType();

        switch (type) {

        case Node.ELEMENT_NODE: 
        case Node.ENTITY_NODE: 
        case Node.ENTITY_REFERENCE_NODE: 
        case Node.DOCUMENT_FRAGMENT_NODE:
            NodeList childs = nd.getChildNodes();
            for (int i = 0; i < childs.getLength(); i++) {
                Node child = childs.item(i);
                short childType = child.getNodeType();
                if (childType != Node.COMMENT_NODE
                        && childType != Node.PROCESSING_INSTRUCTION_NODE) {
                    getText(child, buf);
                }
            }
            break;

        case Node.ATTRIBUTE_NODE: 
            
        case Node.TEXT_NODE: 
        case Node.CDATA_SECTION_NODE: 
        case Node.COMMENT_NODE: 
        case Node.PROCESSING_INSTRUCTION_NODE: 
            buf.append(nd.getNodeValue());
            break;

        case Node.DOCUMENT_NODE: 
        case Node.DOCUMENT_TYPE_NODE: 
        case Node.NOTATION_NODE: 
        default:
            

        }
    }
}

 
继续阅读
编程十万个怎么办