package com.xrosstools.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlHelper {
	public static boolean isValidNode(Node node) {
		return !node.getNodeName().equals("#text");
	}
	
	public static boolean isValidNode(Node node, String name) {
		return node.getNodeName().equals(name);
	}
	
	public static Node getFirstValidNode(Node node) {
		NodeList children = node.getChildNodes();
		for(int i = 0; i < children.getLength(); i++){
			if(isValidNode(children.item(i)))
				return children.item(i);
		}
		
		return null;
	}
	
	public static String format(Document doc) throws Exception {
    	ByteArrayOutputStream out = new ByteArrayOutputStream();
    	TransformerFactory tFactory =TransformerFactory.newInstance();
    	Transformer transformer = tFactory.newTransformer();
    	DOMSource source = new DOMSource(doc);
    	StreamResult result = new StreamResult(out);
    	transformer.transform(source, result);
    	
    	// To make well formated document
    	SAXReader reader = new SAXReader();
    	org.dom4j.Document document = reader.read(new ByteArrayInputStream(out.toByteArray()));
    	
    	XMLWriter writer = null;
        StringWriter stringWriter = new StringWriter();  
        OutputFormat format = new OutputFormat(" ", true);  
        writer = new XMLWriter(stringWriter, format);  
        writer.write(document);  
        writer.flush();  
        return stringWriter.toString();
    }

}

@@ -133,16 +133,69 @@
 Writer;%0D
+%0Aimport java.util.ArrayList;%0D%0Aimport java.util.List;%0D
 %0A%0D%0Aimpor
@@ -1039,32 +1039,319 @@
 n null;%0D%0A%09%7D%0D%0A%09%0D%0A
+%09public static List%3CNode%3E getValidChildNodes(Node node) %7B%0D%0A%09%09List%3CNode%3E nl = new ArrayList%3C%3E();%0D%0A%09%09NodeList nodeList = node.getChildNodes();%0D%0A%09%09for(int i = 0; i %3C nodeList.getLength(); i++)%7B%0D%0A%09%09%09if(isValidNode(nodeList.item(i)))%0D%0A%09%09%09%09nl.add(nodeList.item(i));%0D%0A%09%09%7D%0D%0A%09%09return nl;%0D%0A%09%7D%0D%0A%09%0D%0A
 %09public static S

revise XmlHelper