Java read XML file generic tool class (recursive call)

Source: Internet
Author: User

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

Related 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.