Original: Java read XML file generic tool class (recursive call) source code download address: http://www.zuidaima.com/share/1550463285480448.htm
The Java implementation reads the XML file and gets all the text content under the specified name node, including the node (inverse)
Package com.zuidaima.xml;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.util.List;
Import org.jdom.Document;
Import org.jdom.Element;
Import org.jdom.JDOMException;
Import Org.jdom.input.SAXBuilder; /******************************************************************************* * XML Generic tool class * @author
www.zuidaima.com */@SuppressWarnings ("Unchecked") public class Xmlutils {public static String XML = "";
public static void Init () {xml = ""; /*************************************************************************** * Gets all text content under the specified name node, including node (inverse) < No consideration of node properties > * @param doc * XML Document Object * @param e * The Node object to get * @param excepttag *
Node name to exclude * @return */public static String Getchildalltext (Document doc, Element e) {if (e!= null) {
if (E.getchildren ()!= null) {list<element> List = E.getchildren (); XML + + "<" + E.getname () + ">";
for (Element el:list) {if (El.getchildren (). Size () > 0) {getchildalltext (doc, el); else {XML + + "<" + el.getname () + ">" + el.gettexttrim () + "</" + el.getname () + "&G"
t; ";}}
XML + = "</" + e.getname () + ">";
else {XML + + "<" + e.getname () + ">" + e.gettexttrim () + "</" + e.getname () + ">";
} return XML;
public static void Main (string[] args) throws FileNotFoundException, Jdomexception, IOException {//If any exception is thrown Saxbuilder sb = new Saxbuilder ();
New builder Document doc = null; doc = sb. Build (New FileInputStream ("D:\\test.xml")); Read 6.xml Element root = doc.getrootelement ();
Gets the root node//Element E = Root.getchild ("Apptype1");
System.out.println (e);
System.out.println (Getchildalltext (doc, Root)); }
}
Tags: xml read tools recursive call java topic: Text Parsing and file processing scripts and tools