Java Operations XML Tools class: Xmlutil__ personal Works

Source: Internet
Author: User
One, code:
The public class Xmlutil {/** * outputs the XML file to the specified path * * @param doc * @param fileName * @throws Exception/Public static void Outputxml (Document doc, String fileName) throws Exception {transformerfactory tf = Transformerfactory.ne
		Winstance ();
		Transformer Transformer = Tf.newtransformer ();
		Domsource Source = new Domsource (DOC);
		Transformer.setoutputproperty (outputkeys.encoding, "UTF-8");
		Transformer.setoutputproperty (outputkeys.indent, "yes");
		PrintWriter pw = new PrintWriter (new FileOutputStream (FileName));
		Streamresult result = new Streamresult (PW);
		Transformer.transform (source, result);
	SYSTEM.OUT.PRINTLN ("Generate XML File Success!"); /** * Generates XML * * @param IP * @return/public static Document generatexml (String IP) {Document doc = Nu
		ll
		Element root = null;
			try {documentbuilderfactory factory = documentbuilderfactory. newinstance ();
			Documentbuilder builder = Factory.newdocumentbuilder ();
			doc = Builder.newdocument (); root = dOc.createelement ("errordevices");
		Doc.appendchild (root);
			catch (Exception e) {e.printstacktrace ();
		Return null;//If an exception occurs, the element element is no longer executed;
		element = Doc.createelement ("Errordevice");
		Element.setattribute ("IP", IP);
		Element.setattribute ("Date", Stringutil.formatdate (new Date (), "Yyyy-mm-dd hh:mm:ss"));
		Element.setattribute ("Status", "1");
		Root.appendchild (Element);
	return doc; /** * NEW XML node * * @param IP * @param fileName * @return * @throws filenotfoundexception * @throws Trans Formerexception */public static void Towrite (string filename, string IP) throws FileNotFoundException, Transformere
		xception {String date = stringutil.formatdate (new date (), "Yyyy-mm-dd hh:mm:ss");
		Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
		Documentbuilder builder = null;
		Document doc = null;
			try {builder = Factory.newdocumentbuilder ();
		doc = Builder.parse (new File (filename)); catch (ParserconfiGurationexception e) {e.printstacktrace ();
		catch (Saxexception e) {e.printstacktrace ();
		catch (IOException e) {e.printstacktrace ();
		} NodeList links = doc.getelementsbytagname ("Errordevice");
				if (links.getlength () > 0) {for (int i = 0; i < links.getlength (); i++) {Node nd = Links.item (i);
				Node catparent = Nd.getparentnode ();
				Element ele = (element) nd;
				String url = ele.getattribute ("IP");
					if (Url.equals (IP)) {//Ele.setattribute ("date", date);
				Catparent.removechild (ND);
		The element element = Doc.createelement ("Errordevice");
		Element.setattribute ("IP", IP);
		Element.setattribute ("Date", Stringutil.formatdate (new Date (), "Yyyy-mm-dd hh:mm:ss"));
		Element.setattribute ("Status", "1");
		Doc.getdocumentelement (). appendchild (Element);
		Transformerfactory tf = Transformerfactory.newinstance ();
		Transformer Transformer = Tf.newtransformer ();
		Domsource Source = new Domsource (DOC); Transformer.setoutpuTproperty (outputkeys.encoding, "UTF-8");
		Transformer.setoutputproperty (outputkeys.indent, "yes");
		PrintWriter pw = new PrintWriter (new FileOutputStream (filename));
		Streamresult result = new Streamresult (PW);
		Transformer.transform (source, result);
	SYSTEM.OUT.PRINTLN ("New XML node succeeded!"); /** * Read XML * * @param filename * @return/public static list<map> readXml (String filename) {do
		Cumentbuilderfactory factory = Documentbuilderfactory.newinstance ();
		Documentbuilder builder = null;
		Document doc = null;
			try {builder = Factory.newdocumentbuilder ();
		doc = Builder.parse (new File (filename));
		catch (Parserconfigurationexception e) {e.printstacktrace ();
		catch (Saxexception e) {e.printstacktrace ();
		catch (IOException e) {e.printstacktrace ();
		} NodeList links = doc.getelementsbytagname ("Errordevice");
		list<map> list = new arraylist<map> (); for (int i = 0; i< links.getlength (); i + +) {Element node = (Element) links.item (i);
            Map map = new HashMap ();
            Map.put (Node.getattribute ("IP"), Node.getattribute ("date"));
		List.add (map);
	} return list; }
}
Second, demo XML

<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?>
<errorDevices>
	<errordevice date= " 2017-03-13 12:54:16 "ip=" 20.100.156.42 "status=" 1 "/> <errordevice date=" 2017-03-13 12:54:56 "ip="
	20.100.156.41 "status=" 1 "/>
</errorDevices>

third, the final effect chart
Four, what is XML XML (Extensible Markup Language), born in 1998, the data driven by the consortium, is not a trendy but very practical language.

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.