Java common tool class for reading XML files (recursive call) and xml tool class

Source: Internet
Author: User

Java common tool class for reading XML files (recursive call) and xml tool class

Original: java read XML file common tool class (recursive call) source code: http://www.zuidaima.com/share/1550463285480448.htm

Java reads the XML file and obtains 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; /*************************************** **************************************** * common xml tool * @ author www.zuidaima.com */@ SuppressWarnings ("uncheck Ed ") public class XMLUtils {public static String xml =" "; public static void init () {xml = "";} /*************************************** * *********************************** get the specified name all text content under the node, including nodes (inverse) <ignore node attributes for the moment> ** @ param doc * xml Document Object * @ param e * The Node object to be obtained * @ param javasttag * Name of the node to be excluded * @ 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 () + ">" ;}} xml + = "</" + e. getName () + ">";} else {xml + = "<" + e. getName () + ">" + e. getTextTrim () + "</" + e. getName () + ">" ;}} return xml;} public static void main (String [] args) throws FileNotFoundException, JDOMException, IOException {// if there is any exception, throw SAXBuilder sb = new SAXBuilder (); // newly created constructor Document doc = null; doc = sb. build (new FileInputStream ("D: \ test. xml "); // read 6. xmlElement root = doc. getRootElement (); // get the root node // Element e = root. getChild ("apptype1"); // System. out. println (e); System. out. println (getChildAllText (doc, root ));}}

Java reads xml

XStream stream = new XStream ();
// <Student> the correspondence between the root node and class indicates that student is regarded as the Student class.
Stream. alias ("student", Student. class );
// You can use the xml file filePath as the xml file path.
Student student = (Student) stream. fromXML (new FileInputStream (filePath ));
// You can use a string (xml string). xml is a string.
Student student = (Student) stream. fromXML (xml );
This sample file or string becomes an object, and the attributes defined in the object and the attributes defined in the file will be automatically copied.
Two jar packages: xpp3_min-1.1.3.4.O.jar and xstream-1.3.1.jar

Here is an example of how to read data from multiple nodes in xml in java,

In our program, there are usually some variables determined according to the host environment. for example, the database access user name and password may be set differently on different hosts. you only need to change the XML configuration file to run properly.

Localhost

Sqlname

Username

Password

The above myenv. xml configuration file is generally placed in the tomcat WEB-INF/classes directory.

We compile a Java program to directly read and extract the dbhost dbuser dbpassword for other programs to access the database.

Currently, there are many methods of using SAX. The main difference with DOM is that it reads XML files in a row for analysis. It is suitable for large files. DOM is a one-time read into the memory and obviously cannot deal with large files. here we use SAX parsing. Due to the continuous development of the SAX Parser, many articles on the Internet are aimed at the old version. if you use JDK1.4, you can refer to the article about processing XML documents with SAX. the program here is developed based on its improvements and after practical debugging.

Java program read from myenv. xml above:

Import org. xml. sax. Attributes;

Import org. xml. sax. helpers. DefaultHandler;

Import org. xml. sax. SAXException;

Import java. util. Properties;

// The advantage of using DefaultHandler is that you do not have to display all the methods,

Public class ConfigParser extends DefaultHandler {

/// Define a Properties to store the value of dbhost dbuser dbpassword

Private Properties props;

Private String currentSet;

Private String currentName;

Private StringBuffer currentValue = new StringBuffer ();

// The Builder initializes props.

Public ConfigParser (){

This. props = new Properties ();

}

Public Properties getProps (){

Return this. props;

}

// Define the method for starting element parsing. Here, the name xxx is extracted.

Public void startElement (String uri, String localName, String qName, Attributes attributes)

Throws SAXException {

CurrentValue. delete (0, currentValue. length ());

This. currentName = qName;

}

// Add the value to currentValue

Public void characters (char [] ch, int start, int length) throws SAXException {

CurrentValue. append (ch, start, length );

}

// Save the previous names and values in props one by one after the end

Public void endElemen... the remaining full text>
 

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.