string strings are converted to XML format _ strings to each other into XML

Source: Internet
Author: User

In NC interfaces, string strings need to be converted to XML format, and XML-formatted files need to be converted to string strings;

Package com.accord.test;
Import Java.io.ByteArrayInputStream;
Import Java.io.File;
Import Java.io.FileWriter;

Import java.io.IOException;
Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import Org.dom4j.io.OutputFormat;
Import Org.dom4j.io.SAXReader;

Import Org.dom4j.io.XMLWriter;
		public class Xmlutil {public static void main (string[] args) throws Exception {Xmlutil Xu = new Xmlutil ();
		String xmlstring = xu.xmlchangestring ("E:\\3.xml");
	System.out.println (xmlstring); public string xmlchangestring (string fileName) {try {saxreader saxreader = new Saxreader ();/New Resolution class//docum ent tempdocument = Saxreader.read (XmlUtil.class.getClassLoader (). getResourceAsStream (FileName))//read a file document
			Tempdocument = Saxreader.read (new File (FileName));
		return Tempdocument.asxml ();
		catch (Documentexception e) {e.printstacktrace ();
	return null; }//Convert string type to XML file public void Strchangexml (String str) throws IOException {Saxreader Saxreader = new Saxreader ();
		Document document;
			try {document = Saxreader.read (New Bytearrayinputstream (Str.getbytes ("UTF-8"));
			OutputFormat format = Outputformat.createprettyprint ();
			/** writes the contents of document to the file/XMLWriter writer = new XMLWriter (New FileWriter ("E:\\11111.xml"), format);
			Writer.write (document);
		Writer.close ();
		catch (Documentexception e) {e.printstacktrace (); }
	}
}
Need Dom4j.jar

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.