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